using AdsManagerService.Domain.AggregatesModel.CampaignAggregate;
using MediatR;

namespace AdsManagerService.API.Application.Queries;

public class GetCampaignByIdQueryHandler : IRequestHandler<GetCampaignByIdQuery, CampaignDto?>
{
    private readonly ICampaignRepository _campaignRepository;

    public GetCampaignByIdQueryHandler(ICampaignRepository campaignRepository)
    {
        _campaignRepository = campaignRepository ?? throw new ArgumentNullException(nameof(campaignRepository));
    }

    public async Task<CampaignDto?> Handle(GetCampaignByIdQuery request, CancellationToken cancellationToken)
    {
        var campaign = await _campaignRepository.GetByIdAsync(request.CampaignId, cancellationToken);
        
        if (campaign == null)
            return null;

        return new CampaignDto
        {
            Id = campaign.Id,
            AdvertiserId = campaign.AdvertiserId,
            Name = campaign.Name,
            Description = campaign.Description,
            Status = campaign.Status.Name,
            Objective = campaign.Objective.Name,
            BudgetType = campaign.Budget.Type.ToString(),
            BudgetAmount = campaign.Budget.Amount,
            Currency = campaign.Budget.Currency,
            TotalSpend = campaign.TotalSpend,
            StartDate = campaign.StartDate,
            EndDate = campaign.EndDate,
            CreatedAt = campaign.CreatedAt,
            UpdatedAt = campaign.UpdatedAt
        };
    }
}
