using MediatR;
using AdsTrackingService.Domain.AggregatesModel.AttributionAggregate;

namespace AdsTrackingService.API.Application.Queries;

/// <summary>
/// EN: Handler for GetAttributionDetailsQuery.
/// VI: Handler cho GetAttributionDetailsQuery.
/// </summary>
public class GetAttributionDetailsQueryHandler : IRequestHandler<GetAttributionDetailsQuery, AttributionDto?>
{
    private readonly IAttributionRepository _repository;

    public GetAttributionDetailsQueryHandler(IAttributionRepository repository)
    {
        _repository = repository ?? throw new ArgumentNullException(nameof(repository));
    }

    public async Task<AttributionDto?> Handle(GetAttributionDetailsQuery request, CancellationToken ct)
    {
        var attribution = await _repository.GetByConversionIdAsync(request.ConversionId, ct);
        
        return attribution == null 
            ? null 
            : new AttributionDto(
                attribution.Id,
                attribution.ConversionId,
                attribution.AdId,
                attribution.CampaignId,
                attribution.Model.ToString(),
                attribution.AttributedValue,
                attribution.AttributedAt
            );
    }
}
