using FastEndpoints;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Endpoints.AdminDashboard;

public class GetRecentInquiriesEndpoint : Endpoint<RecentInquiriesQuery, RecentInquiriesResponse>
{
    private readonly AdminDashboardService _dashboardService;

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

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

    public override async Task HandleAsync(RecentInquiriesQuery request, CancellationToken ct)
    {
        try
        {
            var limit = request.Limit <= 0 ? 10 : request.Limit;
            if (limit > 100) limit = 100; // Security: max 100

            var inquiries = await _dashboardService.GetRecentInquiriesAsync(limit, ct);
            await SendAsync(new RecentInquiriesResponse
            {
                Data = inquiries.ToList(),
                Limit = limit
            }, 200, cancellation: ct);
        }
        catch (Exception)
        {
            await SendErrorsAsync(500, ct);
        }
    }
}
