using Microsoft.EntityFrameworkCore;

namespace NetinaShop.Repository.Handlers.Discounts;

public class UpdateDiscountCommandHandler : IRequestHandler<UpdateDiscountCommand, bool>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

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