using System.Security.Claims;
using MediatR;
using Web.Api.Common;
using Web.Api.Extensions;
using Domain.Entities.Donations.ValueObject;

namespace Web.Api.Endpoints.Studio.Donation;

/// <summary>후원 순위 위젯 설정 (스튜디오 전용)</summary>
internal sealed class DonationRankConfig : IEndpoint
{
    internal sealed record SaveRequest(
        int? ID,
        int ChannelID,
        string Title,
        int Theme,
        int Period,
        DateTime? StartAt,
        DateTime? EndAt,
        bool IsShowAmount,
        int MaxRankCount,
        bool? NameMode,
        bool IsActive,
        string? TitleFontFamily,
        int TitleFontSizePx,
        string TitleFontColor,
        int NameDisplayType,
        bool IsShowDonationCount,
        bool IsShowGradeIcon,
        bool IsShowMemberIcon,
        string? Rank1FontFamily,
        int Rank1FontSizePx,
        string Rank1FontColor,
        string? Rank2FontFamily,
        int Rank2FontSizePx,
        string Rank2FontColor,
        string? Rank3FontFamily,
        int Rank3FontSizePx,
        string Rank3FontColor
    );

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

        /// 순위 설정 저장/수정
        app.MapPost("api/studio/donation/rank/config", async (
            SaveRequest body,
            ClaimsPrincipal user,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = user.GetRequiredMemberID();
            var command = new Application.Features.Api.DonationRank.SaveConfig.Command(
                body.ChannelID, memberID, body.ID, body.Title,
                (RankThemeType)body.Theme, (RankPeriodType)body.Period,
                body.StartAt, body.EndAt,
                body.IsShowAmount, body.MaxRankCount, body.NameMode, body.IsActive,
                body.TitleFontFamily, body.TitleFontSizePx, body.TitleFontColor,
                (RankNameDisplayType)body.NameDisplayType,
                body.IsShowDonationCount, body.IsShowGradeIcon, body.IsShowMemberIcon,
                body.Rank1FontFamily, body.Rank1FontSizePx, body.Rank1FontColor,
                body.Rank2FontFamily, body.Rank2FontSizePx, body.Rank2FontColor,
                body.Rank3FontFamily, body.Rank3FontSizePx, body.Rank3FontColor
            );
            await sender.Send(command, ct);
            return ApiResponse.Ok();
        })
        .WithTags("StudioDonationRank")
        .RequireAuthorization();

        /// 순위 설정 삭제
        app.MapDelete("api/studio/donation/rank/config/{id}/{channelID}", async (
            int id,
            int channelID,
            ISender sender,
            CancellationToken ct
        ) => {
            await sender.Send(new Application.Features.Api.DonationRank.DeleteConfig.Command(id, channelID), ct);
            return ApiResponse.Ok();
        })
        .WithTags("StudioDonationRank")
        .RequireAuthorization();
    }
}