using MediatR;
using AdsTrackingService.Domain.AggregatesModel.TrackingPixelAggregate;

namespace AdsTrackingService.API.Application.Queries;

/// <summary>
/// EN: Handler for GetPixelCodeQuery.
/// VI: Handler cho GetPixelCodeQuery.
/// </summary>
public class GetPixelCodeQueryHandler : IRequestHandler<GetPixelCodeQuery, PixelCodeDto?>
{
    private readonly ITrackingPixelRepository _repository;

    public GetPixelCodeQueryHandler(ITrackingPixelRepository repository)
    {
        _repository = repository ?? throw new ArgumentNullException(nameof(repository));
    }

    public async Task<PixelCodeDto?> Handle(GetPixelCodeQuery request, CancellationToken ct)
    {
        var pixel = await _repository.GetByAdvertiserIdAsync(request.AdvertiserId, ct);
        
        return pixel == null 
            ? null 
            : new PixelCodeDto(pixel.PixelCode, pixel.IsActive, pixel.CreatedAt);
    }
}
