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

namespace Application.Features.Admin.Forum.BoardPrefix.GetAll;

internal sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
{
    public async Task<Response> Handle(Query request, CancellationToken ct)
    {
        var items = await db.BoardPrefix
            .AsNoTracking()
            .Where(c => c.BoardID == request.BoardID)
            .OrderBy(c => c.Order)
            .ThenByDescending(c => c.ID)
            .Select(c => new
            {
                c.ID,
                c.BoardID,
                c.Name,
                c.Color,
                c.Order,
                c.Posts,
                c.IsActive,
                c.UpdatedAt,
                c.CreatedAt
            })
            .ToListAsync(ct);

        return new Response(
            items.Count,
            [..items.Select(c => new Response.Row(
                c.ID,
                c.BoardID,
                c.Name,
                c.Color,
                c.Order,
                c.Posts,
                c.IsActive,
                c.UpdatedAt.GetDateAt(),
                c.CreatedAt.GetDateAt()
            ))]);
    }
}