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

namespace Application.Features.Admin.Faq.Item.Get
{
    public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response?>
    {
        public async Task<Response?> Handle(Query request, CancellationToken ct)
        {
            var item = await db.FaqItem
                .AsNoTracking()
                .Include(i => i.FaqCategory)
                .Where(i => i.ID == request.ID)
                .Select(i => new
                {
                    i.ID,
                    i.CategoryID,
                    CategoryCode = i.FaqCategory.Code,
                    CategorySubject = i.FaqCategory.Subject,
                    i.Question,
                    i.Answer,
                    i.Order,
                    i.IsActive,
                    i.UpdatedAt,
                    i.CreatedAt
                })
                .FirstOrDefaultAsync(ct);

            if (item is null)
            {
                return null;
            }

            return new Response(
                item.ID,
                item.CategoryID,
                item.CategoryCode,
                item.CategorySubject,
                item.Question,
                item.Answer,
                item.Order,
                item.IsActive,
                item.UpdatedAt,
                item.CreatedAt
            );
        }
    }
}