using FastEndpoints;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Endpoints.Client;

public class GetPagedEndpoint : Endpoint<ClientQuery, ClientPagedResponse>
{
    private readonly ClientService _clientService;

    public GetPagedEndpoint(ClientService clientService)
    {
        _clientService = clientService;
    }

    public override void Configure()
    {
        Get("/api/client");
        Policies("Bearer");
    }

    public override async Task HandleAsync(ClientQuery request, CancellationToken ct)
    {
        var (clients, total) = await _clientService.GetPagedAsync(request.Page, request.PageSize, request.Status, request.Search);
        await SendAsync(new ClientPagedResponse
        {
            Data = clients.Cast<object>().ToList(),
            Total = total,
            Page = request.Page,
            PageSize = request.PageSize
        }, 200, cancellation: ct);
    }
}
