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

namespace Server.Pages.Panel.Products;

public class UpdateModel(ProductsApplication productsApplication) : PageModel
{
    [BindProperty]
    public ProductViewModel UpdateViewModel { get; set; }

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

        UpdateViewModel =
            await productsApplication.GetAsync(id);

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

        return Page();
    }

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

        return RedirectToPage("Index");
    }
}
