using System.Security.Claims;
using MediatR;
using Web.Api.Common;
using Web.Api.Extensions;

namespace Web.Api.Endpoints.Studio.Settlement;

/// <summary>정산 — 계좌 등록/수정</summary>
internal sealed class SaveAccount : IEndpoint
{
    public sealed record Body(int? AccountID, string BankCode, string AccountNumber, string AccountHolder);

    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPost("api/studio/settlement/account", async (
            Body body,
            ClaimsPrincipal user,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = user.GetRequiredMemberID();
            var result = await sender.Send(new Application.Features.Api.Studio.Settlement.SaveAccount.Command(
                memberID, body.AccountID, body.BankCode, body.AccountNumber, body.AccountHolder
            ), ct);

            return result.Match(
                data => ApiResponse.Ok(data),
                CustomResults.Problem
            );
        })
        .WithTags("StudioSettlement")
        .RequireAuthorization();
    }
}