using AsadiTools.Data;
using AsadiTools.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;

namespace AsadiTools.Pages.Admin.Products;

[Authorize(AuthenticationSchemes = "AdminCookie")]
public class ProductsIndexModel(AppDbContext db) : PageModel
{
    public List<Product> Products { get; private set; } = [];

    public async Task OnGetAsync() =>
        Products = await db.Products.OrderByDescending(p => p.Id).ToListAsync();

    public async Task<IActionResult> OnPostDeleteAsync(int id)
    {
        var p = await db.Products.FindAsync(id);
        if (p is not null) { p.IsActive = false; await db.SaveChangesAsync(); }
        return RedirectToPage();
    }
}
