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

namespace Web.Api.Endpoints.Studio.Donation;

/// <summary>후원 목표 위젯 설정 (스튜디오 전용)</summary>
internal sealed class DonationGoalConfig : IEndpoint
{
    internal sealed record SaveRequest(
        int? ID,
        int ChannelID,
        string Title,
        int Style,
        int StartAmount,
        int TargetAmount,
        DateTime? StartAt,
        DateTime? EndAt,
        bool IsShowPercent,
        string BarColor,
        string BarBackgroundColor,
        int BarHeightPx,
        int TitleFontSizePx,
        string TitleFontColor,
        int AmountFontSizePx,
        string AmountFontColor,
        string? TitleFontFamily,
        string? AmountFontFamily,
        bool IsActive
    );

    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        /// 목표 설정 목록 조회
        app.MapGet("api/studio/donation/goal/config/{channelID}", async (
            int channelID,
            ISender sender,
            CancellationToken ct
        ) => {
            var data = await sender.Send(new Application.Features.Api.DonationGoal.GetConfig.Query(channelID), ct);
            return ApiResponse.Ok(data);
        })
        .WithTags("StudioDonationGoal")
        .RequireAuthorization();

        /// 목표 설정 저장/수정
        app.MapPost("api/studio/donation/goal/config", async (
            SaveRequest body,
            ClaimsPrincipal user,
            ISender sender,
            CancellationToken ct
        ) => {

            var memberID = user.GetRequiredMemberID();
            var command = new Application.Features.Api.DonationGoal.SaveConfig.Command(
                body.ChannelID, 
                memberID, 
                body.ID, 
                body.Title, 
                body.Style,
                body.StartAmount, 
                body.TargetAmount, 
                body.StartAt, 
                body.EndAt,
                body.IsShowPercent, 
                body.BarColor, 
                body.BarBackgroundColor, 
                body.BarHeightPx,
                body.TitleFontSizePx, 
                body.TitleFontColor, 
                body.AmountFontSizePx,
                body.AmountFontColor,
                body.TitleFontFamily, 
                body.AmountFontFamily,
                body.IsActive
            );

            await sender.Send(command, ct);

            return ApiResponse.Ok();
        })
        .WithTags("StudioDonationGoal")
        .RequireAuthorization();

        /// 목표 설정 삭제
        app.MapDelete("api/studio/donation/goal/config/{id}/{channelID}", async (
            int id,
            int channelID,
            ISender sender,
            CancellationToken ct
        ) => {

            await sender.Send(new Application.Features.Api.DonationGoal.DeleteConfig.Command(id, channelID), ct);

            return ApiResponse.Ok();
        })
        .WithTags("StudioDonationGoal")
        .RequireAuthorization();
    }
}