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

    public List<CollectionRunRecord>? Runs { get; set; }
    public string? Message { get; set; }

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

    public async Task OnGetAsync()
    {
        try
        {
            Runs = await _collectionRepository.GetRecentRunsAsync(limit: 20);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Collection runs loading failed");
            Message = "데이터 수집 현황을 불러올 수 없습니다.";
        }
    }
}
