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 DeleteModel(ProductsApplication productsApplication) : PageModel
{
    [BindProperty]
    public ProductViewModel DeleteViewModel { get; set; } = new();

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

        DeleteViewModel =
            await productsApplication.GetAsync(id);

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

        return Page();
    }

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

        return RedirectToPage("Index");
    }
}
