using FastEndpoints;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Endpoints.Inquiry;

public class UpdateAdminMemoEndpoint : Endpoint<UpdateAdminMemoRequest, MessageResponse>
{
    private readonly InquiryService _inquiryService;

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

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

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

        try
        {
            await _inquiryService.UpdateAdminMemoAsync(id, request.AdminMemo);
            await SendAsync(new MessageResponse { Message = "메모가 저장되었습니다." }, 200, cancellation: ct);
        }
        catch (Exception ex)
        {
            ThrowError(ex.Message);
        }
    }
}
