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

namespace AdsManagerService.API.Application.Commands;

public class UpdateCampaignCommandHandler : IRequestHandler<UpdateCampaignCommand, bool>
{
    private readonly ICampaignRepository _campaignRepository;

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

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

        campaign.Update(request.Name, request.Description);
        _campaignRepository.Update(campaign);

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