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

namespace Web.Api.Endpoints.Studio.Crew;

/// <summary>크루 후원 설정 저장/수정 (스튜디오 전용)</summary>
internal sealed class CrewConfig : IEndpoint
{
    internal sealed record SaveRequest(
        int? ID,
        int ChannelID,
        string Name,
        string? Description,
        int? MinAmount,
        bool IsActive
    );

    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPost("api/studio/crew/save", async (
            SaveRequest body,
            ClaimsPrincipal user,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = user.GetRequiredMemberID();
            var command = new Application.Features.Api.Crew.SaveCrew.Command(
                body.ID, body.ChannelID, memberID,
                body.Name, body.Description, body.MinAmount, body.IsActive
            );
            await sender.Send(command, ct);
            return ApiResponse.Ok();
        })
        .WithTags("StudioCrew")
        .RequireAuthorization();
    }
}