using FastEndpoints;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Endpoints.AdminDashboard;

public class GetUpcomingFilingsEndpoint : Endpoint<UpcomingFilingsQuery, UpcomingFilingsResponse>
{
    private readonly TaxFilingService _taxFilingService;

    public GetUpcomingFilingsEndpoint(TaxFilingService taxFilingService)
    {
        _taxFilingService = taxFilingService;
    }

    public override void Configure()
    {
        Get("/api/admin-dashboard/upcoming-filings");
        Policies("AdminOnly");
    }

    public override async Task HandleAsync(UpcomingFilingsQuery request, CancellationToken ct)
    {
        try
        {
            var days = request.Days <= 0 ? 30 : request.Days;
            var filings = await _taxFilingService.GetUpcomingAsync(days);
            await SendAsync(new UpcomingFilingsResponse
            {
                Data = filings.Cast<object>().ToList(),
                Days = days
            }, 200, cancellation: ct);
        }
        catch (Exception)
        {
            await SendErrorsAsync(500, ct);
        }
    }
}
