using Application.Aggregates.Stores;
using Application.Aggregates.Stores.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Server.Pages.Panel.Stores;

public class UpdateModel(StoresApplication storesApplication) : PageModel
{
    [BindProperty]
    public StoreViewModel UpdateViewModel { get; set; }

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

        UpdateViewModel=
            await storesApplication.GetAsync(id);

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

        return Page();
    }

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

        return RedirectToPage("Index");
    }
}
