from distributors.models import Commission
from retailers.models import Retailer, Transaction, Wallet


def calculate_percentage_after_subtraction(amount1, amount2):
    difference = amount1 - amount2
    percentage = (difference / amount1) * 100
    print(percentage, "percentage")
    return percentage


def calculate_percentage_commission(forward_commission, total_amount):
    percentage_commission = (forward_commission / total_amount) * 100
    return percentage_commission


def calculate_commission(amount, percentage):
    commission = (amount * percentage) / 100

    # frw_commission = amount - commission
    return commission


def getCumulativeAmount(retailer, typ, amt, excludeID):
    try:
        rtr = retailer
        txs = Transaction.objects.exclude(id=excludeID).filter(retailer=rtr, status=1).last()
        if txs:
            if typ == 0:
                return float(txs.cumulative_amount) - float(amt)
            else:
                return float(txs.cumulative_amount) + float(amt)
        else:
            return 0
    except Exception as e:
        print("Error in getCumulativeAmount: ", str(e))
        return 0


def calculate_overall_fre_amount(obj):

    # opt_frw_commission= calculate_commission(obj.commission, obj.percentage)
    # obj.forward_commission = opt_frw_commission
    opt_frw_commission = obj.forward_commission
    opt_dist_commission = obj.distributor_commission
    opt_rtr_commission = obj.retailer_commission

    if obj:
        commission = Commission.objects.filter(operator__id=obj.id)
        if len(commission) > 0:
            super_distributor = commission.filter(source=1)  # SUPER_DISTRIBUTOR
            if len(super_distributor) > 0:
                for sup in super_distributor:
                    sup.receive_commission = opt_frw_commission
                    # frw_commission= calculate_commission(sup.receive_commission, sup.percentage)
                    sup.forward_commission = opt_dist_commission
                    sup.save()

                    distributor = commission.filter(source=2)  # DISTRIBUTOR
                    if len(distributor) > 0:
                        for dt in distributor:
                            dt.receive_commission = sup.forward_commission
                            # frw_commission= calculate_commission(dt.receive_commission, dt.percentage)
                            dt.forward_commission = opt_rtr_commission
                            dt.save()


def calculateCumulativeAmount():
    __rtr = None
    try:
        rtrs = Retailer.objects.filter(is_active=True)
        for rtr in rtrs:
            __rtr = rtr
            rtr.transaction_in_progress = True
            rtr.save()
            txs = Transaction.objects.filter(retailer=rtr, status=1)
            last_cumulative_amount = 0
            for obj in txs:
                if obj.transaction_type == 1:
                    obj.cumulative_amount = last_cumulative_amount + float(
                        obj.deduction_amount
                    )
                elif obj.transaction_type == 2:
                    obj.cumulative_amount = last_cumulative_amount - float(
                        obj.deduction_amount
                    )

                last_cumulative_amount = obj.cumulative_amount

                obj.save()
            rtr.transaction_in_progress = False
            rtr.save()
            Wallet.objects.filter(retailer=rtr).update(amount=last_cumulative_amount)
    except Exception as e:
        if __rtr:
            __rtr.transaction_in_progress = False
            __rtr.save()
        print("Error in calculateCumulativeAmount: ", str(e))