namespace NetinaShop.Repository.Handlers.Discounts;

public class CreateDiscountCommandHandler : IRequestHandler<CreateDiscountCommand , DiscountLDto>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public CreateDiscountCommandHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<DiscountLDto> Handle(CreateDiscountCommand request, CancellationToken cancellationToken)
    {
        switch (request.Type)
        {
            case DiscountType.All:
                var ent = Discount.Create(request.Code, request.DiscountPercent, request.DiscountAmount, request.HasCode, request.AmountType, request.Type, request.Count, request.StartDate, request.ExpireDate, request.PriceFloor, request.HasPriceFloor, request.PriceCeiling, request.HasPriceCeiling, request.IsInfinity, request.UseCount, request.IsForInvitation,request.IsSpecialOffer);
                _repositoryWrapper.SetRepository<Discount>().Add(ent);
                break;
            case DiscountType.Category:
                var catDis = CategoryDiscount.Create(request.Code, request.DiscountPercent, request.DiscountAmount, request.HasCode,
                    request.AmountType, request.Type, request.Count, request.StartDate, request.ExpireDate, request.PriceFloor,
                    request.HasPriceFloor, request.PriceCeiling, request.HasPriceCeiling, request.IsInfinity, request.UseCount,
                    request.IsForInvitation,request.IsSpecialOffer, request.CategoryId);
                _repositoryWrapper.SetRepository<CategoryDiscount>().Add(catDis);
                break;

            case DiscountType.Product:
                var productDis = ProductDiscount.Create(request.Code, request.DiscountPercent, request.DiscountAmount, request.HasCode,
                    request.AmountType, request.Type, request.Count, request.StartDate, request.ExpireDate, request.PriceFloor,
                    request.HasPriceFloor, request.PriceCeiling, request.HasPriceCeiling, request.IsInfinity, request.UseCount,
                    request.IsForInvitation, request.IsSpecialOffer,request.ProductId);
                _repositoryWrapper.SetRepository<ProductDiscount>().Add(productDis);
                break;
            default:
                var def = Discount.Create(request.Code, request.DiscountPercent, request.DiscountAmount, request.HasCode,
                    request.AmountType, request.Type, request.Count, request.StartDate, request.ExpireDate, request.PriceFloor,
                    request.HasPriceFloor, request.PriceCeiling, request.HasPriceCeiling, request.IsInfinity, request.UseCount,
                    request.IsForInvitation,request.IsSpecialOffer);
                _repositoryWrapper.SetRepository<Discount>().Add(def);
                break;
        }
        await _repositoryWrapper.SaveChangesAsync(cancellationToken);
        return request.Adapt<DiscountLDto>();
    }
}