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

namespace Server.Pages.Panel.Users;

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

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

        UpdateViewModel =
            await usersApplication.GetAsync(id);

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

        return Page();
    }

    public async Task<IActionResult> OnPostAsync()
    {
        if (ModelState.IsValid)
        {
            await usersApplication.UpdateAsync(UpdateViewModel);
        }

        return RedirectToPage("Index");
    }
}
