using FastEndpoints;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Endpoints.AdminDashboard;

public class GetSummaryEndpoint : EndpointWithoutRequest<AdminDashboardSummaryResponse>
{
    private readonly AdminDashboardService _dashboardService;

    public GetSummaryEndpoint(AdminDashboardService dashboardService)
    {
        _dashboardService = dashboardService;
    }

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

    public override async Task HandleAsync(CancellationToken ct)
    {
        try
        {
            var summary = await _dashboardService.GetSummaryAsync(ct);
            await SendAsync(new AdminDashboardSummaryResponse
            {
                ThisMonthInquiries = summary.ThisMonthInquiries,
                NewInquiries = summary.NewInquiries,
                TotalPosts = summary.TotalPosts,
                PublishedPosts = summary.PublishedPosts,
                RecentInquiries = summary.RecentInquiries.ToList()
            }, 200, cancellation: ct);
        }
        catch (Exception)
        {
            await SendErrorsAsync(500, ct);
        }
    }
}
