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

namespace JobsMedical.Web.Pages.Shifts;

public class DetailsModel : PageModel
{
    private readonly AppDbContext _db;
    private readonly InterestService _interest;
    private readonly JobsMedical.Web.Services.Scraping.SettingsService _settings;

    public DetailsModel(AppDbContext db, InterestService interest,
        JobsMedical.Web.Services.Scraping.SettingsService settings)
    {
        _db = db;
        _interest = interest;
        _settings = settings;
    }

    public Shift? Shift { get; private set; }
    public List<Shift> MoreAtFacility { get; private set; } = new();
    public string? MapKey { get; private set; }

    // Set after the visitor taps "interested" — reveals the facility contact (handoff model).
    public bool ShowContact { get; private set; }
    public bool Saved { get; private set; }
    public bool Reported { get; private set; }

    public async Task<IActionResult> OnGetAsync(int id)
    {
        await LoadAsync(id);
        if (Shift is null) return NotFound();
        MapKey = (await _settings.GetAsync()).NeshanMapKey;
        Reported = Request.Query["reported"] == "1";
        await _interest.LogAsync(InterestEventType.View, id); // behavioral signal for recommendations
        return Page();
    }

    public async Task<IActionResult> OnPostInterestAsync(int id)
    {
        await LoadAsync(id);
        if (Shift is null) return NotFound();
        await _interest.LogAsync(InterestEventType.Apply, id);
        ShowContact = true; // MVP handoff: reveal contact. Records an Application once auth lands.
        return Page();
    }

    public async Task<IActionResult> OnPostSaveAsync(int id)
    {
        await LoadAsync(id);
        if (Shift is null) return NotFound();
        await _interest.LogAsync(InterestEventType.Save, id);
        Saved = true;
        return Page();
    }

    public async Task<IActionResult> OnPostDismissAsync(int id)
    {
        await _interest.LogAsync(InterestEventType.Dismiss, id);
        return RedirectToPage("/Shifts/Index"); // not interested → back to the list
    }

    private async Task LoadAsync(int id)
    {
        Shift = await _db.Shifts
            .Include(s => s.Facility).ThenInclude(f => f.City)
            .Include(s => s.Role)
            .Include(s => s.Contacts)
            .FirstOrDefaultAsync(s => s.Id == id);

        if (Shift is not null)
        {
            var today = DateOnly.FromDateTime(DateTime.UtcNow);
            MoreAtFacility = await _db.Shifts
                .Include(s => s.Facility).ThenInclude(f => f.City)
                .Include(s => s.Role)
                .Where(s => s.FacilityId == Shift.FacilityId && s.Id != id
                            && s.Status == ShiftStatus.Open && s.Date >= today)
                .OrderBy(s => s.Date).Take(3).ToListAsync();
        }
    }
}
