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

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

public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
{
    public async Task<Response> Handle(Query request, CancellationToken ct)
    {
        var items = await db.Coin
            .AsNoTracking()
            .Where(c => c.IsActive && !c.IsDelisted)
            .OrderByDescending(c => c.IsFeatured)
            .ThenBy(c => c.DisplayOrder)
            .ThenBy(c => c.Symbol)
            .Select(c => new
            {
                c.ID,
                c.Symbol,
                c.KorName,
                c.EngName,
                c.LogoImage,
                c.IsFeatured,
                c.DisplayOrder,
                c.IsActive
            })
            .ToListAsync(ct);

        return new Response(
            items.Count,
            [..items.Select((c, i) => new Response.Row(
                i + 1,
                c.ID,
                c.Symbol,
                c.KorName,
                c.EngName,
                c.LogoImage,
                c.IsFeatured,
                c.DisplayOrder,
                c.IsActive
            ))]);
    }
}