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

namespace TaxBaik.Web.Endpoints.Inquiry;

public class SubmitEndpoint : Endpoint<SubmitInquiryDto, MessageResponse>
{
    private readonly InquiryService _inquiryService;

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

    public override void Configure()
    {
        Post("/api/inquiry");
        AllowAnonymous();
    }

    public override async Task HandleAsync(SubmitInquiryDto request, CancellationToken ct)
    {
        if (string.IsNullOrWhiteSpace(request.Name) || string.IsNullOrWhiteSpace(request.Phone))
        {
            ThrowError("이름과 전화번호를 입력하세요.");
        }

        try
        {
            await _inquiryService.SubmitAsync(
                request.Name,
                request.Phone,
                request.ServiceType,
                request.Message,
                request.Email,
                HttpContext.Connection.RemoteIpAddress?.ToString(),
                request.SuppressNotification);
            await SendAsync(new MessageResponse { Message = "상담 신청이 접수되었습니다." }, 200, cancellation: ct);
        }
        catch (ValidationException ex)
        {
            ThrowError(ex.Message);
        }
    }
}
