from django.db import models
from django.utils.timezone import datetime
from django.contrib.auth import settings
from distributors.models import Distributors
from settings.models import Operator, MRobotics, ServiceCategory

# Create your models here.


class Source(models.IntegerChoices):
    MRobotics = 1
    Third_Party = 2


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

    name = models.CharField(max_length=150)
    url = models.SlugField(null=True, blank=True, editable=False)
    profile = models.ImageField(upload_to="profile/", null=True, blank=True)
    mobile = models.CharField(max_length=20, unique=True)
    password = models.CharField(max_length=20)
    email = models.EmailField(max_length=250, null=True, blank=True)
    address = models.TextField(null=True, blank=True)
    pincode = models.CharField(max_length=250, null=True, blank=True)
    mpin = models.CharField(max_length=4, null=True, blank=True)
    transaction_in_progress = models.BooleanField(default=False)

    is_kyc = models.BooleanField(default=False)
    is_aadhar = models.BooleanField(default=False)
    is_pan = models.BooleanField(default=False)
    is_kyc_activated = models.BooleanField(default=False)

    distributor = models.ForeignKey(
        Distributors, on_delete=models.SET_NULL, 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 = "Retailers"
        verbose_name = "Retailer"

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


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

    retailer = models.ForeignKey(
        Retailer, on_delete=models.CASCADE, limit_choices_to={"is_active": True}
    )
    pan_number = models.CharField(max_length=150, unique=True)
    full_name = models.CharField(max_length=20)
    category = models.CharField(max_length=20)

    # 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 = "Retailers PAN"
        verbose_name = "Retailer PAN"

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

class RetailerAadhaarData(models.Model):

    objects = models.Manager()

    retailer = models.ForeignKey(
        Retailer, on_delete=models.CASCADE, limit_choices_to={"is_active": True}
    )

    full_name = models.CharField(max_length=255, null=True, blank=True)
    aadhaar_number = models.CharField(max_length=12, unique=True, null=True, blank=True)
    dob = models.DateField(null=True, blank=True)
    gender = models.CharField(max_length=1, choices=[('M', 'Male'), ('F', 'Female'), ('O', 'Other')], null=True, blank=True)
    
    # Address fields
    country = models.CharField(max_length=100, default='India', null=True, blank=True)
    state = models.CharField(max_length=100, null=True, blank=True)
    dist = models.CharField(max_length=100, null=True, blank=True)
    subdist = models.CharField(max_length=100, null=True, blank=True)
    po = models.CharField(max_length=100, null=True, blank=True)
    loc = models.CharField(max_length=255, null=True, blank=True)
    vtc = models.CharField(max_length=100, null=True, blank=True)
    street = models.CharField(max_length=255, null=True, blank=True)
    house = models.CharField(max_length=255, null=True, blank=True)
    landmark = models.CharField(max_length=255, null=True, blank=True)
    zip = models.CharField(max_length=6, null=True, blank=True)

    
    # profile_image = models.ImageField(upload_to='retailers/aadhar/',null=True, blank=True)
    care_of = models.CharField(max_length=255, null=True, blank=True)
    share_code = models.CharField(max_length=10, null=True, blank=True)
    status = models.CharField(max_length=50, null=True, blank=True)


    # Default Column Name
    is_active = models.BooleanField(default=True, editable=False)
    created_on = models.DateField(default=datetime.now, editable=False)
    
    
    class Meta:
        verbose_name = 'Aadhaar Data'
        verbose_name_plural = 'Aadhaar Data'

    def __str__(self):
        return f"{self.full_name} ({self.aadhaar_number})"


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

    retailer = models.ForeignKey(
        Retailer, on_delete=models.CASCADE, limit_choices_to={"is_active": True}
    )
    amount = models.DecimalField(max_digits=12, decimal_places=2)
    deposit = models.DecimalField(max_digits=12, decimal_places=2, default=0)
    commission = models.DecimalField(max_digits=12, decimal_places=2, default=0)
    credit = models.DecimalField(max_digits=12, decimal_places=2, default=0)

    # 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 = "Wallet"
        verbose_name = "Wallet"

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


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

    mobile = models.CharField(max_length=10, default="NA")
    order_id = models.CharField(max_length=200, null=True, blank=True)
    recharge_id = models.CharField(max_length=200, null=True, blank=True)
    user_id = models.CharField(max_length=200, null=True, blank=True)
    lapu_id = models.CharField(max_length=200, null=True, blank=True)
    mrobotics = models.ForeignKey(
        MRobotics,
        on_delete=models.DO_NOTHING,
        limit_choices_to={"is_active": True},
        null=True,
        blank=True,
    )
    source = models.IntegerField(choices=Source.choices, default=Source.MRobotics)
    retailer = models.ForeignKey(
        Retailer,
        on_delete=models.CASCADE,
        limit_choices_to={"is_active": True},
        null=True,
        blank=True,
    )
    service = models.ForeignKey(
        ServiceCategory,
        on_delete=models.CASCADE,
        limit_choices_to={"is_active": True},
        null=True,
        blank=True,
    )
    operator = models.ForeignKey(
        Operator,
        on_delete=models.CASCADE,
        limit_choices_to={"is_active": True},
        null=True,
        blank=True,
    )
    operator_no = models.CharField(max_length=200, null=True, blank=True)
    status = models.CharField(max_length=200, null=True, blank=True)
    amount = models.DecimalField(max_digits=12, decimal_places=2)
    deduction_amount = models.DecimalField(default=0, max_digits=12, decimal_places=2)
    commission_amount = models.DecimalField(default=0, max_digits=12, decimal_places=2)
    date = models.DateTimeField(default=datetime.now)
    api_reference = models.CharField(max_length=200, null=True, blank=True)
    api_transaction_id = models.CharField(max_length=200, 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 = "Recharge"
        verbose_name = "Recharge"

    def __str__(self):
        if self.order_id:
            return self.order_id

        return self.retailer.name

    def source_name(self):
        if self.source == 1:
            return "MRobotics"
        elif self.source == 2:
            return "Third_Party"


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

    class Transaction_type(models.IntegerChoices):
        Credit = 1
        Debit = 2

    class TransactionSource(models.IntegerChoices):
        Wallet = 1
        Recharge = 2
        KYC = 3

    class Status(models.IntegerChoices):
        Success = 1
        Pending = 2
        Failure = 3

    order_id = models.CharField(max_length=200, null=True, blank=True)
    tnx_id = models.CharField(max_length=200, null=True, blank=True)
    source = models.IntegerField(
        choices=TransactionSource.choices, default=TransactionSource.Wallet
    )
    retailer = models.ForeignKey(
        Retailer, on_delete=models.CASCADE, limit_choices_to={"is_active": True}
    )
    operator = models.ForeignKey(
        Operator,
        on_delete=models.CASCADE,
        limit_choices_to={"is_active": True},
        null=True,
        blank=True,
    )
    transaction_type = models.IntegerField(
        choices=Transaction_type.choices, default=Transaction_type.Credit
    )
    amount = models.DecimalField(max_digits=12, decimal_places=2)
    cumulative_amount = models.DecimalField(max_digits=12, decimal_places=2, default=0)
    deduction_amount = models.DecimalField(default=0, max_digits=12, decimal_places=2)
    commission_amount = models.DecimalField(default=0, max_digits=12, decimal_places=2)
    date = models.DateTimeField(default=datetime.now)
    status = models.IntegerField(choices=Status.choices, default=Status.Pending)
    remark = models.TextField(null=True, blank=True)
    recharge = models.ForeignKey(
        Recharge, 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 = "Transactions"
        verbose_name = "Transaction"

    def source_name(self):
        if self.source == 1:
            return "Wallet"
        elif self.source == 2:
            return "recharge"

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


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

    title = models.CharField(max_length=200, null=True, blank=True)
    message = models.CharField(max_length=200, null=True, blank=True)

    retailer = models.ForeignKey(
        Retailer, on_delete=models.CASCADE, limit_choices_to={"is_active": True}
    )
    is_sean = models.BooleanField(default=False)

    # 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 = "Notifications"

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


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

    recharge = models.ForeignKey(
        Recharge, on_delete=models.SET_NULL, null=True, blank=True
    )
    retailer = models.ForeignKey(
        Retailer, on_delete=models.SET_NULL, null=True, blank=True
    )
    error = models.TextField(null=True, blank=True)

    # 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 = "Errors"
        verbose_name = "Errors"

    def __str__(self):
        if self.retailer:
            return self.retailer.name
        else:
            return "Unknown Error"
