using AdsBillingService.Domain.AggregatesModel.BillingAccountAggregate;
using MediatR;

namespace AdsBillingService.API.Application.Commands;

public class CreateBillingAccountCommandHandler : IRequestHandler<CreateBillingAccountCommand, Guid>
{
    private readonly ILogger<CreateBillingAccountCommandHandler> _logger;

    public CreateBillingAccountCommandHandler(ILogger<CreateBillingAccountCommandHandler> logger)
    {
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public async Task<Guid> Handle(CreateBillingAccountCommand request, CancellationToken cancellationToken)
    {
        var paymentMethod = request.PaymentMethod.ToLower() switch
        {
            "prepaid" => PaymentMethodType.Prepaid,
            "postpaid" => PaymentMethodType.Postpaid,
            "creditcard" => PaymentMethodType.CreditCard,
            _ => PaymentMethodType.Prepaid
        };

        var billingAccount = new BillingAccount(
            request.AdvertiserId,
            request.WalletId,
            paymentMethod
        );

        // TODO: Add repository save
        // _billingAccountRepository.Add(billingAccount);
        // await _billingAccountRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

        _logger.LogInformation("Billing account created for advertiser {AdvertiserId}", request.AdvertiserId);

        return billingAccount.Id;
    }
}
