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

namespace Web.Api.Endpoints.Donation;

/// <summary>초대 코드로 크루 가입 (크루원 본인용)</summary>
internal sealed class CrewJoinByCode : IEndpoint
{
    internal sealed record JoinRequest(string InviteCode, string Nickname);

    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPost("api/crew/join", async (
            JoinRequest body,
            ClaimsPrincipal user,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = user.GetRequiredMemberID();
            var command = new Application.Features.Api.Crew.JoinByCode.Command(
                body.InviteCode, memberID, body.Nickname
            );
            var data = await sender.Send(command, ct);
            return ApiResponse.Ok(data);
        })
        .WithTags("Crew")
        .RequireAuthorization();
    }
}