from django.db import models
from django.utils.timezone import datetime
from django.contrib.auth import settings

from settings.models import Operator

# Create your models here.


class SuperDistributors(models.Model):

    objects = models.Manager()

    name = models.CharField(max_length=150)
    code = models.CharField(max_length=20, unique=True, editable=False)
    mobile = models.CharField(max_length=20, unique=True)
    password = models.CharField(max_length=20)
    email = models.CharField(max_length=200, null=True)
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        related_name="super_distributor_user",
        on_delete=models.CASCADE,
        null=True,
        blank=True,
    )

    # Default Column Name
    is_active = models.BooleanField(default=True, editable=False)
    created_on = models.DateField(default=datetime.now, editable=False)
    created_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        editable=False,
        null=True,
        blank=True,
    )

    class Meta:
        verbose_name_plural = "Super Distributors"
        verbose_name = "Super Distributors"

    def __str__(self):
        return str(self.name)


class Distributors(models.Model):

    objects = models.Manager()

    super_distributor = models.ForeignKey(
        SuperDistributors, models.SET_NULL, null=True, blank=True
    )
    name = models.CharField(max_length=150)
    code = models.CharField(max_length=20, unique=True, editable=False)
    mobile = models.CharField(max_length=20, unique=True)
    password = models.CharField(max_length=20)
    email = models.CharField(max_length=200, null=True)

    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        related_name="distributor_user",
        on_delete=models.CASCADE,
        null=True,
        blank=True,
    )

    # Default Column Name
    is_active = models.BooleanField(default=True, editable=False)
    created_on = models.DateField(default=datetime.now, editable=False)
    created_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        editable=False,
        null=True,
        blank=True,
    )

    class Meta:
        verbose_name_plural = "Distributors"
        verbose_name = "Distributors"

    def __str__(self):
        return str(self.name)


class WalletSource(models.IntegerChoices):
    SUPER_DISTRIBUTOR = 1
    DISTRIBUTOR = 2


class DistributorWallet(models.Model):
    objects = models.Manager()

    source = models.IntegerField(
        default=WalletSource.SUPER_DISTRIBUTOR, choices=WalletSource.choices
    )

    super_distributor = models.ForeignKey(
        SuperDistributors, on_delete=models.CASCADE, null=True, blank=True
    )
    distributor = models.ForeignKey(
        Distributors, on_delete=models.CASCADE, null=True, blank=True
    )

    amount = models.DecimalField(default=0, decimal_places=3, max_digits=10)

    # Default Column Name
    is_active = models.BooleanField(default=True, editable=False)
    created_on = models.DateField(default=datetime.now, editable=False)

    class Meta:
        verbose_name_plural = "Wallets"
        verbose_name = "Wallets"

    def __str__(self):
        if self.source == 1:
            if self.super_distributor:
                return str(self.super_distributor.name)

        else:
            if self.distributor:
                return str(self.distributor.name)

        return "NA"


class TransactionType(models.IntegerChoices):
    CREDIT = 1
    DEBIT = 2


class WalletTransaction(models.Model):
    objects = models.Manager()

    wallet = models.ForeignKey(DistributorWallet, on_delete=models.CASCADE)
    amount = models.DecimalField(default=0, decimal_places=3, max_digits=10)
    type = models.IntegerField(
        default=TransactionType.CREDIT, choices=TransactionType.choices
    )
    cumulative = models.DecimalField(default=0, decimal_places=3, max_digits=10)

    # Default Column Name
    is_active = models.BooleanField(default=True, editable=False)
    created_on = models.DateField(default=datetime.now, editable=False)

    class Meta:
        verbose_name_plural = "Transactions"
        verbose_name = "Transactions"

    def __str__(self):
        if self.wallet.source == 1:
            if self.wallet.super_distributor:
                return str(self.wallet.super_distributor.name)
        else:
            if self.wallet.distributor:
                return str(self.wallet.distributor.name)

        return "NA"


class Commission(models.Model):
    objects = models.Manager()

    source = models.IntegerField(
        default=WalletSource.SUPER_DISTRIBUTOR,
        choices=WalletSource.choices,
        
    )

    super_distributor = models.ForeignKey(
        SuperDistributors,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        
    )
    distributor = models.ForeignKey(
        Distributors, on_delete=models.CASCADE, null=True, blank=True
    )

    operator = models.ForeignKey(
        Operator, related_name="operators", on_delete=models.CASCADE
    )
    receive_commission = models.DecimalField(default=0, max_digits=10, decimal_places=2)
    forward_commission = models.DecimalField(default=0, max_digits=10, decimal_places=2)
    percentage = models.DecimalField(default=0, max_digits=10, decimal_places=2)

    # Default Column Name
    is_active = models.BooleanField(default=True, editable=False)
    created_on = models.DateField(default=datetime.now, editable=False)

    class Meta:
        verbose_name_plural = "Commissions"
        verbose_name = "Commission"

    def __str__(self):
        if self.source == 1:
            if self.super_distributor:
                return str(self.super_distributor.name)
            else:
                return "NA"
        else:
            if self.distributor:
                return str(self.distributor.name)
            else:
                return "NA"


class DistributorTransfer(models.Model):
    objects = models.Manager()

    distributor = models.ForeignKey(Distributors, on_delete=models.CASCADE, null=True)
    retailer = models.ForeignKey(
        "retailers.retailer", on_delete=models.CASCADE, null=True
    )
    amount = models.DecimalField(default=0, decimal_places=3, max_digits=10)

    # Default Column Name
    is_active = models.BooleanField(default=True, editable=False)
    created_on = models.DateField(default=datetime.now, editable=False)

    class Meta:
        verbose_name_plural = "Distributor Transfer"
        verbose_name = "Distributor Transfer"

    def __str__(self):

        if self.distributor:
            return str(self.distributor.name)

        return "NA"


class SuperDistributorTransfer(models.Model):
    objects = models.Manager()

    super_distributor = models.ForeignKey(
        SuperDistributors, on_delete=models.CASCADE, null=True
    )
    distributor = models.ForeignKey(Distributors, on_delete=models.CASCADE, null=True)
    amount = models.DecimalField(default=0, decimal_places=3, max_digits=10)

    # Default Column Name
    is_active = models.BooleanField(default=True, editable=False)
    created_on = models.DateField(default=datetime.now, editable=False)

    class Meta:
        verbose_name_plural = "Super Dis. Transfer"
        verbose_name = "Super Dis. Transfer"

    def __str__(self):

        if self.super_distributor:
            return str(self.super_distributor.name)

        return "NA"
