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

namespace JobsMedical.Web.Pages;

public class IndexModel : PageModel
{
    private readonly AppDbContext _db;
    private readonly RecommendationService _recs;
    private readonly InterestService _interest;

    public IndexModel(AppDbContext db, RecommendationService recs, InterestService interest)
    {
        _db = db;
        _recs = recs;
        _interest = interest;
    }

    public List<Recommendation> Recommendations { get; private set; } = new();
    public bool HasPersonalization { get; private set; }
    public List<Shift> LatestShifts { get; private set; } = new();
    public List<JobOpening> LatestJobs { get; private set; } = new();
    public List<TalentListing> LatestTalent { get; private set; } = new();
    public List<City> Cities { get; private set; } = new();
    public List<Role> Roles { get; private set; } = new();
    public int OpenShiftCount { get; private set; }
    public int FacilityCount { get; private set; }
    public int CityCount { get; private set; }

    public async Task OnGetAsync()
    {
        var today = DateOnly.FromDateTime(DateTime.UtcNow);

        Recommendations = await _recs.GetForVisitorAsync(6);
        // "Personalized" = we actually used a signal (prefs or behavior), not just cold-start freshness.
        HasPersonalization = (await _interest.GetPreferencesAsync())?.HasAny == true
                             || (await _interest.RecentEventsAsync(1)).Count > 0;

        LatestShifts = await _db.Shifts
            .Include(s => s.Facility).ThenInclude(f => f.City)
            .Include(s => s.Role)
            .Where(s => s.Status == ShiftStatus.Open && s.Date >= today)
            .OrderBy(s => s.Date).ThenBy(s => s.StartTime)
            .Take(6)
            .ToListAsync();

        LatestJobs = await _db.JobOpenings
            .Include(j => j.Facility).ThenInclude(f => f.City)
            .Include(j => j.Facility).ThenInclude(f => f.District)
            .Include(j => j.Role)
            .Where(j => j.Status == ShiftStatus.Open
                && j.CreatedAt >= JobsMedical.Web.Services.Scraping.ListingPolicy.JobCutoffUtc)
            .OrderByDescending(j => j.CreatedAt)
            .Take(3)
            .ToListAsync();

        LatestTalent = await _db.TalentListings
            .Include(t => t.City).Include(t => t.District).Include(t => t.Role)
            .Where(t => t.Status == ShiftStatus.Open
                && t.CreatedAt >= JobsMedical.Web.Services.Scraping.ListingPolicy.TalentCutoffUtc)
            .OrderByDescending(t => t.CreatedAt)
            .Take(3)
            .ToListAsync();

        Cities = await _db.Cities.Where(c => c.IsActive).OrderBy(c => c.Name).ToListAsync();
        Roles = await _db.Roles.Where(r => r.IsActive).OrderBy(r => r.SortOrder).ToListAsync();
        OpenShiftCount = await _db.Shifts.CountAsync(s => s.Status == ShiftStatus.Open && s.Date >= today);
        FacilityCount = await _db.Facilities.CountAsync();
        CityCount = await _db.Cities.CountAsync(c => c.IsActive);
    }
}
