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

namespace Application.Features.Api.Crew.GetWidgetConfig;

internal sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
{
    public async Task<Response> Handle(Query request, CancellationToken ct)
    {
        var list = await db.CrewWidgetConfig.AsNoTracking()
            .Where(c => c.ChannelID == request.ChannelID)
            .OrderByDescending(c => c.CreatedAt)
            .Select(c => new CrewWidgetConfigItem(
                c.ID, c.Title, (int)c.Theme, (int)c.Period,
                c.StartAt.HasValue ? c.StartAt.Value.ToString("o") : null,
                c.EndAt.HasValue ? c.EndAt.Value.ToString("o") : null,
                c.MaxDisplayCount, c.IsShowAmount, c.IsShowDonationCount,
                c.IsShowContributionRate, c.IsShowMemberIcon, c.IsActive,
                c.BgColor,
                c.TitleFontFamily, c.TitleFontSizePx, c.TitleFontColor,
                c.Rank1FontFamily, c.Rank1FontSizePx, c.Rank1FontColor,
                c.Rank2FontFamily, c.Rank2FontSizePx, c.Rank2FontColor,
                c.Rank3FontFamily, c.Rank3FontSizePx, c.Rank3FontColor,
                c.RowFontFamily, c.RowFontSizePx, c.RowFontColor
            ))
            .ToListAsync(ct);

        return new Response(list);
    }
}