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

namespace Application.Features.Admin.MemberGrade.GetAll
{
    public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
    {
        public async Task<Response> Handle(Query request, CancellationToken ct)
        {
            var items = await db.MemberGrade
                .AsNoTracking()
                .OrderBy(c => c.Order)
                .ThenByDescending(c => c.ID)
                .Select(c => new
                {
                    c.ID,
                    c.KorName,
                    c.EngName,
                    c.Description,
                    c.Order,
                    c.Image,
                    c.TextColor,
                    c.RequiredExp,
                    c.RequiredAttendance,
                    c.IsActive,
                    c.UpdatedAt,
                    c.CreatedAt
                })
                .ToListAsync(ct);

            return new Response(
                items.Count,
                [.. items.Select((c, i) => new Response.Row(
                    i + 1,
                    c.ID,
                    c.KorName,
                    c.EngName,
                    c.Description,
                    c.Order,
                    c.Image,
                    c.TextColor,
                    c.RequiredExp,
                    c.RequiredAttendance,
                    c.IsActive,
                    0,
                    c.UpdatedAt,
                    c.CreatedAt
                ))]
            );
        }
    }
}