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 SnapshotsModel : PageModel
{
    private readonly ICollectionReadRepository _collectionRepository;
    private readonly ILogger<SnapshotsModel> _logger;

    public string? RunId { get; set; }
    public List<CollectionSnapshotRecord>? Snapshots { get; set; }

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

    public async Task OnGetAsync(string runId)
    {
        RunId = runId;
        try
        {
            Snapshots = await _collectionRepository.GetRunSnapshotsAsync(runId);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to load collection snapshots");
            Snapshots = [];
        }
    }
}
