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

namespace JobsMedical.Web.Pages.Admin;

/// <summary>Every crawled item with its outcome (queued / published / flagged / discarded),
/// filterable by status and source — the full audit trail of ingestion.</summary>
[Authorize(Roles = "Admin")]
public class IngestedModel : PageModel
{
    private readonly AppDbContext _db;
    public IngestedModel(AppDbContext db) => _db = db;

    public List<RawListing> Items { get; private set; } = new();
    public int Total { get; private set; }
    public Dictionary<RawListingStatus, int> Counts { get; private set; } = new();

    [BindProperty(SupportsGet = true)] public string? Status { get; set; }   // new|flagged|published|discarded|all
    [BindProperty(SupportsGet = true)] public string? Source { get; set; }

    public async Task OnGetAsync()
    {
        Counts = await _db.RawListings.GroupBy(r => r.Status)
            .Select(g => new { g.Key, C = g.Count() }).ToDictionaryAsync(x => x.Key, x => x.C);

        var q = _db.RawListings.AsNoTracking().AsQueryable();

        var st = Status?.ToLowerInvariant() switch
        {
            "new" => (RawListingStatus?)RawListingStatus.New,
            "flagged" => RawListingStatus.Flagged,
            "published" => RawListingStatus.Normalized,
            "discarded" => RawListingStatus.Discarded,
            _ => null,
        };
        if (st is not null) q = q.Where(r => r.Status == st);
        if (!string.IsNullOrWhiteSpace(Source)) q = q.Where(r => r.SourceChannel.Contains(Source));

        Total = await q.CountAsync();
        Items = await q.OrderByDescending(r => r.FetchedAt).Take(200).ToListAsync();
    }
}
