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

namespace Web.Api.Endpoints.Studio.Settings;

/// <summary>채널 연결 — YouTube 연동 해지</summary>
internal sealed class DisconnectYouTube : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPost("api/studio/youtube-disconnect", async (
            ClaimsPrincipal user,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = user.GetRequiredMemberID();

            try
            {
                await sender.Send(new Application.Features.Api.Studio.DisconnectYouTube.Command(memberID), ct);
                return ApiResponse.Ok();
            }
            catch (InvalidOperationException ex)
            {
                return Results.Problem(ex.Message, statusCode: 400);
            }
        })
        .WithTags("Studio")
        .RequireAuthorization();
    }
}