namespace Netina.Repository.Handlers.Discounts;

public class CreateDiscountCommandHandler(IRepositoryWrapper repositoryWrapper)
    : IRequestHandler<CreateDiscountCommand, DiscountLDto>
{
    public async Task<DiscountLDto> Handle(CreateDiscountCommand request, CancellationToken cancellationToken)
    {
        if (request.HasCode)
        {
            var foundDiscount = await repositoryWrapper.SetRepository<Discount>()
                .TableNoTracking
                .Where(d => d.Code == request.Code)
                .FirstOrDefaultAsync(cancellationToken);

            if (foundDiscount != null)
                throw new BaseApiException(ApiResultStatusCode.BadRequest, "کد تخفیف مورد نظر تکراری می باشد");
        }
        switch (request.Type)
        {
            case DiscountType.All:
                var ent = Discount.Create(request.Code,request.Description, request.DiscountPercent, request.DiscountAmount,
                    request.HasCode, request.AmountType, request.Type, request.Count,request.IsImmortal, request.StartDate,
                    request.ExpireDate, request.PriceFloor, request.HasPriceFloor, request.PriceCeiling, request.HasPriceCeiling,
                    request.IsInfinity, request.UseCount, request.IsForInvitation,request.IsForFirstPurchase,request.IsSpecialOffer);
                repositoryWrapper.SetRepository<Discount>().Add(ent);
                break;
            case DiscountType.Category:
                var catDis = CategoryDiscount.Create(request.Code,request.Description, request.DiscountPercent, request.DiscountAmount, request.HasCode,
                    request.AmountType, request.Type, request.Count,request.IsImmortal, request.StartDate, request.ExpireDate, request.PriceFloor,
                    request.HasPriceFloor, request.PriceCeiling, request.HasPriceCeiling, request.IsInfinity, request.UseCount,
                    request.IsForInvitation,request.IsForFirstPurchase,request.IsSpecialOffer, request.CategoryId);
                repositoryWrapper.SetRepository<CategoryDiscount>().Add(catDis);
                break;

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