using Web.Api.Common;
using Web.Api.Extensions;
using MediatR;

namespace Web.Api.Endpoints.Channel;

/// <summary>위젯 토큰 재발급</summary>
internal sealed class ResetWidgetToken : IEndpoint
{
    private sealed record Request(int ChannelID);

    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPost("api/channel/widget-token/reset", async (
            Request request,
            HttpContext http,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = http.User.GetRequiredMemberID();
            var result = await sender.Send(
                new Application.Features.Api.Channel.ResetWidgetToken.Command(request.ChannelID, memberID)
            , ct);

            return result.Match(
                token => ApiResponse.Ok(new {
                    widgetToken = token
                }),
                CustomResults.Problem
            );
        })
        .WithTags("Channel")
        .RequireAuthorization();
    }
}