using AsadiTools.Models;
using AsadiTools.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace AsadiTools.Pages.Cart;

public class CartIndexModel(CartService cart) : PageModel
{
    public List<CartItem> Items { get; private set; } = [];
    public decimal Total { get; private set; }

    public void OnGet()
    {
        Items = cart.GetItems();
        Total = cart.Total;
    }

    public IActionResult OnPostUpdateQty(int productId, int qty)
    {
        cart.UpdateQty(productId, qty);
        return RedirectToPage();
    }

    public IActionResult OnPostRemove(int productId)
    {
        cart.RemoveItem(productId);
        return RedirectToPage();
    }

    public IActionResult OnPostClear()
    {
        cart.Clear();
        return RedirectToPage();
    }
}
