using Application.Abstractions.Data;
using Application.Abstractions.Messaging;
using Microsoft.EntityFrameworkCore;

namespace Application.Features.Api.DonationRank.GetConfig;

internal sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
{
    public async Task<Response> Handle(Query request, CancellationToken ct)
    {
        var list = await db.DonationRankConfig
            .AsNoTracking()
            .Where(c => c.ChannelID == request.ChannelID)
            .OrderByDescending(c => c.CreatedAt)
            .Select(c => new RankConfigItem(
                c.ID, c.Title, (int)c.Theme, (int)c.Period,
                c.StartAt, c.EndAt,
                c.IsShowAmount, c.MaxRankCount, c.NameMode, c.IsActive,
                c.TitleFontFamily, c.TitleFontSizePx, c.TitleFontColor,
                (int)c.NameDisplayType,
                c.IsShowDonationCount, c.IsShowGradeIcon, c.IsShowMemberIcon,
                c.Rank1FontFamily, c.Rank1FontSizePx, c.Rank1FontColor,
                c.Rank2FontFamily, c.Rank2FontSizePx, c.Rank2FontColor,
                c.Rank3FontFamily, c.Rank3FontSizePx, c.Rank3FontColor
            ))
            .ToListAsync(ct);

        return new Response(list);
    }
}