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

namespace Application.Features.Admin.Crypto.TickerConfig.Get;

public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
{
    public async Task<Response> Handle(Query request, CancellationToken ct)
    {
        var config = await db.Config.AsNoTracking().OrderByDescending(x => x.ID).FirstOrDefaultAsync(ct);

        if (config is null)
        {
            return new Response(5, 5.0m, -5.0m, 10);
        }

        return new Response(
            config.Crypto.TickerRefreshSeconds,
            config.Crypto.SurgeThreshold,
            config.Crypto.PlungeThreshold,
            config.Crypto.MainPageCoinCount
        );
    }
}