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;

[Authorize(AuthenticationSchemes = "AdminCookie")]
public class AdminIndexModel(AppDbContext db) : PageModel
{
    public int TotalOrders { get; private set; }
    public int PendingOrders { get; private set; }
    public decimal TotalRevenue { get; private set; }
    public int ActiveProducts { get; private set; }
    public List<Order> RecentOrders { get; private set; } = [];

    public async Task<IActionResult> OnGetAsync()
    {
        TotalOrders    = await db.Orders.CountAsync();
        PendingOrders  = await db.Orders.CountAsync(o => o.Status == OrderStatus.Pending);
        TotalRevenue   = await db.Orders.Where(o => o.Status != OrderStatus.Cancelled).SumAsync(o => o.Total);
        ActiveProducts = await db.Products.CountAsync(p => p.IsActive);
        RecentOrders   = await db.Orders.OrderByDescending(o => o.Id).Take(6).ToListAsync();
        return Page();
    }
}
