using FastEndpoints;
using TaxBaik.Application.DTOs;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Endpoints.Client;

public class UpdateEndpoint : Endpoint<CreateClientDto, object>
{
    private readonly ClientService _clientService;

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

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

    public override async Task HandleAsync(CreateClientDto request, CancellationToken ct)
    {
        var id = Route<int>("id");
        try
        {
            await _clientService.UpdateAsync(id, request);
            var client = await _clientService.GetByIdAsync(id);
            if (client == null)
            {
                ThrowError("고객을 찾을 수 없습니다.", statusCode: 404);
            }

            await SendAsync(client, 200, cancellation: ct);
        }
        catch (ValidationException ex)
        {
            ThrowError(ex.Message);
        }
    }
}
