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

namespace QuantEngine.Web.Pages.Admin.Collection;

[Authorize(AuthenticationSchemes = AdminAuthDefaults.Scheme)]
public class IndexModel : PageModel
{
    private readonly ICollectionReadModelService _collectionReadModelService;
    private readonly ILogger<IndexModel> _logger;

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

    public IndexModel(ICollectionReadModelService collectionReadModelService, ILogger<IndexModel> logger)
    {
        _collectionReadModelService = collectionReadModelService;
        _logger = logger;
    }

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