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

namespace AdsManagerService.API.Application.Commands;

public class ActivateCampaignCommandHandler : IRequestHandler<ActivateCampaignCommand, bool>
{
    private readonly ICampaignRepository _campaignRepository;

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

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

        campaign.Activate();
        _campaignRepository.Update(campaign);

        return await _campaignRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
    }
}
