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

namespace Web.Api.Endpoints.Studio.Crew;

/// <summary>크루 위젯 설정 CRUD</summary>
internal sealed class CrewWidgetConfigEndpoint : IEndpoint
{
    internal sealed record SaveRequest(
        int? ID, int ChannelID, string Title, int Theme, int Period,
        DateTime? StartAt, DateTime? EndAt,
        int MaxDisplayCount, bool IsShowAmount, bool IsShowDonationCount,
        bool IsShowContributionRate, bool IsShowMemberIcon, bool IsActive,
        string BgColor,
        string? TitleFontFamily, int TitleFontSizePx, string TitleFontColor,
        string? Rank1FontFamily, int Rank1FontSizePx, string Rank1FontColor,
        string? Rank2FontFamily, int Rank2FontSizePx, string Rank2FontColor,
        string? Rank3FontFamily, int Rank3FontSizePx, string Rank3FontColor,
        string? RowFontFamily, int RowFontSizePx, string RowFontColor
    );

    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapGet("api/studio/crew/widget/config/{channelID}", async (
            int channelID,
            ISender sender,
            CancellationToken ct
        ) => {
            var data = await sender.Send(new Application.Features.Api.Crew.GetWidgetConfig.Query(channelID), ct);
            return ApiResponse.Ok(data);
        })
        .WithTags("StudioCrew")
        .RequireAuthorization();

        app.MapPost("api/studio/crew/widget/config", async (
            SaveRequest body,
            ClaimsPrincipal user,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = user.GetRequiredMemberID();
            var command = new Application.Features.Api.Crew.SaveWidgetConfig.Command(
                body.ChannelID, memberID, body.ID,
                body.Title, body.Theme, body.Period,
                body.StartAt, body.EndAt,
                body.MaxDisplayCount, body.IsShowAmount, body.IsShowDonationCount,
                body.IsShowContributionRate, body.IsShowMemberIcon, body.IsActive,
                body.BgColor,
                body.TitleFontFamily, body.TitleFontSizePx, body.TitleFontColor,
                body.Rank1FontFamily, body.Rank1FontSizePx, body.Rank1FontColor,
                body.Rank2FontFamily, body.Rank2FontSizePx, body.Rank2FontColor,
                body.Rank3FontFamily, body.Rank3FontSizePx, body.Rank3FontColor,
                body.RowFontFamily, body.RowFontSizePx, body.RowFontColor
            );
            await sender.Send(command, ct);
            return ApiResponse.Ok();
        })
        .WithTags("StudioCrew")
        .RequireAuthorization();

        app.MapDelete("api/studio/crew/widget/config/{id}/{channelID}", async (
            int id,
            int channelID,
            ISender sender,
            CancellationToken ct
        ) => {
            await sender.Send(new Application.Features.Api.Crew.DeleteWidgetConfig.Command(id, channelID), ct);
            return ApiResponse.Ok();
        })
        .WithTags("StudioCrew")
        .RequireAuthorization();
    }
}