using System.Security.Claims;
using Microsoft.AspNetCore.Mvc;
using Web.Api.Common;
using Web.Api.Extensions;
using MediatR;

namespace Web.Api.Endpoints.Studio.Donation;

/// <summary>후원 알림 미디어 파일 업로드 (스튜디오 전용)</summary>
internal sealed class DonationAlertConfigUpload : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPost("api/studio/donation/alert/config/upload", async (
            IFormFile file,
            [FromForm] string type,
            [FromForm] int channelID,
            ClaimsPrincipal user,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = user.GetRequiredMemberID();

            var result = await sender.Send(new Application.Features.Api.DonationAlert.UploadMedia.Command(
                channelID,
                memberID,
                type,
                file
            ), ct);

            return result.Match(
                url => ApiResponse.Ok(new { 
                    url 
                }),
                CustomResults.Problem
            );
        })
        .WithTags("StudioDonationAlert")
        .RequireAuthorization()
        .DisableAntiforgery();
    }
}