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 AnalyticsModel : PageModel
{
    private readonly AppDbContext _db;
    public AnalyticsModel(AppDbContext db) => _db = db;

    public int Users { get; private set; }
    public int Facilities { get; private set; }
    public int VerifiedFacilities { get; private set; }
    public int OpenShifts { get; private set; }
    public int OpenJobs { get; private set; }
    public int Applications { get; private set; }
    public int Reviews { get; private set; }
    public int NewUsers7 { get; private set; }
    public int NewApps7 { get; private set; }

    public record DayBar(DateOnly Day, int Count);
    public List<DayBar> ApplyByDay { get; private set; } = new();
    public int MaxBar { get; private set; } = 1;

    public async Task OnGetAsync()
    {
        var today = DateOnly.FromDateTime(DateTime.UtcNow);
        Users = await _db.Users.CountAsync();
        Facilities = await _db.Facilities.CountAsync();
        VerifiedFacilities = await _db.Facilities.CountAsync(f => f.IsVerified);
        OpenShifts = await _db.Shifts.CountAsync(s => s.Status == ShiftStatus.Open && s.Date >= today);
        OpenJobs = await _db.JobOpenings.CountAsync(j => j.Status == ShiftStatus.Open);
        Applications = await _db.InterestEvents.CountAsync(e => e.EventType == InterestEventType.Apply);
        Reviews = await _db.Reviews.CountAsync();

        var since7 = DateTime.UtcNow.AddDays(-7);
        NewUsers7 = await _db.Users.CountAsync(u => u.CreatedAt >= since7);
        NewApps7 = await _db.InterestEvents.CountAsync(e => e.EventType == InterestEventType.Apply && e.CreatedAt >= since7);

        var since14 = DateTime.UtcNow.Date.AddDays(-13);
        var stamps = await _db.InterestEvents
            .Where(e => e.EventType == InterestEventType.Apply && e.CreatedAt >= since14)
            .Select(e => e.CreatedAt).ToListAsync();
        var byDay = stamps.GroupBy(d => DateOnly.FromDateTime(d.Date)).ToDictionary(g => g.Key, g => g.Count());
        for (var i = 0; i < 14; i++)
        {
            var day = DateOnly.FromDateTime(since14).AddDays(i);
            ApplyByDay.Add(new DayBar(day, byDay.GetValueOrDefault(day)));
        }
        MaxBar = Math.Max(1, ApplyByDay.Count > 0 ? ApplyByDay.Max(b => b.Count) : 1);
    }
}
