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

namespace Application.Features.Api.DonationGoal.GetConfig;

internal sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
{
    public async Task<Response> Handle(Query request, CancellationToken ct)
    {
        var list = await db.DonationGoalConfig
            .AsNoTracking()
            .Where(c => c.ChannelID == request.ChannelID)
            .OrderByDescending(c => c.CreatedAt)
            .Select(c => new GoalConfigItem(
                c.ID, c.Title, (int)c.Style,
                c.StartAmount, c.TargetAmount,
                c.StartAt, c.EndAt, c.IsShowPercent,
                c.BarColor, c.BarBackgroundColor, c.BarHeightPx,
                c.TitleFontSizePx, c.TitleFontColor,
                c.AmountFontSizePx, c.AmountFontColor,
                c.TitleFontFamily, c.AmountFontFamily,
                c.IsActive
            ))
            .ToListAsync(ct);

        return new Response(list);
    }
}