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.Orders;

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

    public async Task OnGetAsync() =>
        Orders = await db.Orders
            .Include(o => o.Items)
            .OrderByDescending(o => o.Id)
            .ToListAsync();

    public async Task<IActionResult> OnPostUpdateStatusAsync(int id, OrderStatus status)
    {
        var order = await db.Orders.FindAsync(id);
        if (order is not null)
        {
            order.Status = status;
            await db.SaveChangesAsync();
        }
        return RedirectToPage();
    }
}
