using JobsMedical.Web.Data;
using JobsMedical.Web.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;

namespace JobsMedical.Web.Pages.Admin;

[Authorize(Roles = "Admin")]
public class OverviewModel : PageModel
{
    private readonly AppDbContext _db;
    public OverviewModel(AppDbContext db) => _db = db;

    public int Users, Employers, Staff, Banned;
    public int Facilities, VerifiedFacilities, PendingFacilities;
    public int OpenShifts, OpenJobs, Applies;
    public int PushSubs, QueueNew, QueueFlagged, OpenReports;

    public async Task OnGetAsync()
    {
        var today = DateOnly.FromDateTime(DateTime.UtcNow);
        Users = await _db.Users.CountAsync();
        Employers = await _db.Users.CountAsync(u => u.Role == UserRole.FacilityAdmin);
        Staff = await _db.Users.CountAsync(u => u.Role == UserRole.Doctor);
        Banned = await _db.Users.CountAsync(u => u.IsBanned);
        Facilities = await _db.Facilities.CountAsync();
        VerifiedFacilities = await _db.Facilities.CountAsync(f => f.IsVerified);
        PendingFacilities = Facilities - VerifiedFacilities;
        OpenShifts = await _db.Shifts.CountAsync(s => s.Status == ShiftStatus.Open && s.Date >= today);
        OpenJobs = await _db.JobOpenings.CountAsync(j => j.Status == ShiftStatus.Open);
        Applies = await _db.InterestEvents.CountAsync(e => e.EventType == InterestEventType.Apply);
        PushSubs = await _db.WebPushSubscriptions.CountAsync();
        QueueNew = await _db.RawListings.CountAsync(r => r.Status == RawListingStatus.New);
        QueueFlagged = await _db.RawListings.CountAsync(r => r.Status == RawListingStatus.Flagged);
        OpenReports = await _db.Reports.CountAsync(r => r.Status == ReportStatus.Open);
    }
}
