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;

[Authorize(Roles = "Admin")] // secured by the OTP-auth Admin role
public class IndexModel : PageModel
{
    private readonly AppDbContext _db;
    public IndexModel(AppDbContext db) => _db = db;

    public List<RawListing> Queue { get; private set; } = new();
    public int PublishedShifts { get; private set; }
    public int PublishedJobs { get; private set; }

    [BindProperty] public string? SourceChannel { get; set; }
    [BindProperty] public string? RawText { get; set; }

    public async Task OnGetAsync() => await LoadAsync();

    public async Task<IActionResult> OnPostAddAsync()
    {
        if (!string.IsNullOrWhiteSpace(RawText))
        {
            _db.RawListings.Add(new RawListing
            {
                SourceChannel = string.IsNullOrWhiteSpace(SourceChannel) ? "ورود دستی" : SourceChannel.Trim(),
                RawText = RawText.Trim(),
                Status = RawListingStatus.New,
            });
            await _db.SaveChangesAsync();
        }
        return RedirectToPage();
    }

    private async Task LoadAsync()
    {
        Queue = await _db.RawListings
            .Where(r => r.Status == RawListingStatus.New)
            .OrderByDescending(r => r.FetchedAt).ToListAsync();
        PublishedShifts = await _db.Shifts.CountAsync(s => s.Source != ShiftSource.Direct);
        PublishedJobs = await _db.JobOpenings.CountAsync();
    }
}
