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

namespace Application.Features.Admin.MemberGrade.Get
{
    public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
    {
        public async Task<Response> Handle(Query request, CancellationToken ct)
        {
            var item = await db.MemberGrade
                .AsNoTracking()
                .FirstOrDefaultAsync(x => x.ID == request.ID, ct);

            if (item is null)
            {
                throw new KeyNotFoundException("회원등급을 찾을 수 없습니다.");
            }

            return new Response(
                item.ID,
                item.KorName,
                item.EngName,
                item.Description,
                item.Order,
                item.Image,
                item.TextColor,
                item.RequiredExp,
                item.RequiredAttendance,
                item.IsActive,
                item.UpdatedAt,
                item.CreatedAt
            );
        }
    }
}
