namespace Netina.Repository.Handlers.Discounts;

public class UpdateDiscountCommandHandler(IRepositoryWrapper repositoryWrapper)
    : IRequestHandler<UpdateDiscountCommand, bool>
{
    public async Task<bool> Handle(UpdateDiscountCommand request, CancellationToken cancellationToken)
    {
        switch (request.Type)
        {
            case DiscountType.All:
                var ent = await repositoryWrapper.SetRepository<Discount>().TableNoTracking.FirstOrDefaultAsync(d=>d.Id==request.Id,cancellationToken);
                if (ent == null)
                    throw new AppException("Discount not found", ApiResultStatusCode.NotFound);
                var newEnt = 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);
                newEnt.Id = ent.Id;
                newEnt.CreatedAt = ent.CreatedAt;
                newEnt.CreatedBy = ent.CreatedBy;
                repositoryWrapper.SetRepository<Discount>().Update(newEnt);
                break;
            case DiscountType.Category:
                var catEnt = await repositoryWrapper.SetRepository<CategoryDiscount>().TableNoTracking.FirstOrDefaultAsync(d => d.Id == request.Id, cancellationToken);
                if (catEnt == null)
                    throw new AppException("Discount not found", ApiResultStatusCode.NotFound);

                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);
                catDis.Id = catEnt.Id;
                catDis.CreatedAt = catEnt.CreatedAt;
                catDis.CreatedBy = catEnt.CreatedBy;
                repositoryWrapper.SetRepository<CategoryDiscount>().Update(catDis);
                break;

            case DiscountType.Product:
                var productEnt = await repositoryWrapper.SetRepository<ProductDiscount>().TableNoTracking.FirstOrDefaultAsync(d => d.Id == request.Id, cancellationToken);
                if (productEnt == null)
                    throw new AppException("Discount not found", ApiResultStatusCode.NotFound);

                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);
                productDis.Id = productEnt.Id;
                productDis.CreatedAt = productEnt.CreatedAt;
                productDis.CreatedBy = productEnt.CreatedBy;
                repositoryWrapper.SetRepository<ProductDiscount>().Update(productDis);
                break;
        }
        await repositoryWrapper.SaveChangesAsync(cancellationToken);
        return true;
    }
}