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

namespace TaxBaik.Web.Endpoints.Inquiry;

public class UpdateEndpoint : Endpoint<UpdateInquiryDto, object>
{
    private readonly InquiryService _inquiryService;

    public UpdateEndpoint(InquiryService inquiryService)
    {
        _inquiryService = inquiryService;
    }

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

    public override async Task HandleAsync(UpdateInquiryDto request, CancellationToken ct)
    {
        var id = Route<int>("id");
        try
        {
            var result = await _inquiryService.UpdateAsync(id, request);
            if (result == null)
            {
                ThrowError("문의를 찾을 수 없습니다.", statusCode: 404);
            }

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