using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;
using QuantEngine.Core.Interfaces;
using QuantEngine.Web.Services;

namespace QuantEngine.Web.Pages.Admin.Collection;

[Authorize(AuthenticationSchemes = AdminAuthDefaults.Scheme)]
public class DetailModel : PageModel
{
    private readonly ICollectionRepository _collectionRepository;
    private readonly ILogger<DetailModel> _logger;

    public CollectionRunRecord? Run { get; set; }

    public DetailModel(ICollectionRepository collectionRepository, ILogger<DetailModel> logger)
    {
        _collectionRepository = collectionRepository;
        _logger = logger;
    }

    public async Task OnGetAsync(string runId)
    {
        try
        {
            var runs = await _collectionRepository.GetRecentRunsAsync(limit: 100);
            Run = runs.FirstOrDefault(r => r.RunId == runId);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to load collection run detail");
        }
    }
}
