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

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

            if (item is null)
            {
                throw new KeyNotFoundException($"RSS 피드 소스를 찾을 수 없습니다. (ID: {request.ID})");
            }

            return new Response(
                item.ID,
                item.Name,
                item.Url,
                item.Description,
                item.IntervalMinutes,
                item.IsActive,
                item.LastFetchedAt,
                item.UpdatedAt,
                item.CreatedAt
            );
        }
    }
}