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

namespace QuantEngine.Web.Pages.Admin.Collection;

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

    public string? RunId { get; set; }
    public List<CollectionErrorRecord>? Errors { get; set; }

    public ErrorsModel(ICollectionReadRepository collectionRepository, ILogger<ErrorsModel> logger)
    {
        _collectionRepository = collectionRepository;
        _logger = logger;
    }

    public async Task OnGetAsync(string runId)
    {
        RunId = runId;
        try
        {
            Errors = await _collectionRepository.GetRunErrorsAsync(runId, limit: 100);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to load collection errors");
            Errors = [];
        }
    }
}
