using Domain.Entities.Payments.ValueObject;
using MediatR;
using System.Security.Claims;
using System.Text.Json.Serialization;
using Web.Api.Common;
using Web.Api.Extensions;

namespace Web.Api.Endpoints.Payment;

public sealed class Request
{
    [JsonPropertyName("amount")]
    public int Amount { get; set; }

    [JsonPropertyName("paymentMethod")]
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public PaymentMethod PaymentMethod { get; set; }
}

/// <summary>결제 주문 생성</summary>
internal sealed class CreateOrder : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        /// 금액/결제수단으로 주문 생성 → orderId, clientKey, successUrl 반환
        app.MapPost("api/payment/order", async (
            Request body,
            ClaimsPrincipal user,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = user.GetRequiredMemberID();
            var command = new Application.Features.Api.Payment.CreateOrder.Command(
                body.Amount,
                body.PaymentMethod,
                memberID
            );
            var data = await sender.Send(command, ct);

            return ApiResponse.Ok(data);
        })
        .WithTags("Payment")
        .RequireAuthorization();
    }
}