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

namespace Application.Features.Admin.Banner.Item.Get;

public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
{
    public async Task<Response> Handle(Query request, CancellationToken ct)
    {
        var item = await db.BannerItem.AsNoTracking().FirstOrDefaultAsync(x => x.ID == request.ID, ct);
        if (item is null)
        {
            throw new KeyNotFoundException("배너를 찾을 수 없습니다.");
        }

        return new Response(
            item.ID,
            item.PositionID,
            item.Subject,
            item.DesktopImage,
            item.MobileImage,
            item.Link,
            item.Order,
            item.IsActive,
            item.StartAt,
            item.EndAt,
            item.UpdatedAt,
            item.CreatedAt
        );
    }
}