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

namespace Application.Features.Api.DonationAlert.GetConfig;

internal sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
{
    public async Task<Response> Handle(Query request, CancellationToken ct)
    {
        var widgetToken = await db.Channel
            .AsNoTracking()
            .Where(c => c.ID == request.ChannelID && c.IsActive)
            .Select(c => c.WidgetToken)
            .FirstOrDefaultAsync(ct);

        var list = await db.DonationAlertConfig
            .AsNoTracking()
            .Where(c => c.ChannelID == request.ChannelID)
            .OrderBy(c => c.Amount)
            .Select(c => new AlertConfigItem(
                c.ID, c.Title, c.Amount, c.MatchType, c.Message,
                c.PlayDelaySec, c.DisplayDurationSec,
                c.PopupEffect, c.TextEffect,
                c.NicknameFontFamily, c.NicknameFontSize, c.NicknameFontColor,
                c.AmountFontFamily, c.AmountFontSize, c.AmountFontColor,
                c.MessageFontFamily, c.MessageFontSize, c.MessageFontColor,
                c.TemplateFontFamily, c.TemplateFontSize, c.TemplateFontColor,
                c.EnableImage, c.ImageUrl,
                c.EnableSound, c.SoundUrl,
                c.IsActive
            ))
            .ToListAsync(ct);

        return new Response(list, widgetToken);
    }
}