using Application.Aggregates.Users;
using Application.Aggregates.Users.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Server.Infrastructure;

namespace Server.Pages.Panel.Users;

public class DeleteModel
    (UsersApplication usersApplication) : BasePageModel
{
    [BindProperty]
    public UserViewModel DeleteViewModel { get; set; } = new();

    public async Task<IActionResult> OnGetAsync(Guid id)
    {
        if (id == Guid.Empty)
        {
            return RedirectToPage("Index");
        }

        DeleteViewModel =
            await usersApplication.GetAsync(id);

        if (DeleteViewModel == null)
        {
            return RedirectToPage("Index");
        }

        return Page();
    }

    public async Task<IActionResult> OnPostAsync()
    {
        await usersApplication.DeleteAsync(DeleteViewModel.Id);

        return RedirectToPage("Index");
    }
}
