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

# Create your models here.


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

    name = models.CharField(max_length=150)
    # icon = models.CharField(max_length=150)

    # 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 = "Service Categories"
        verbose_name = "Service Category"

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


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

    service = models.ForeignKey(
        ServiceCategory,
        related_name="operators",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
    )
    name = models.CharField(max_length=200)
    operator_id = models.IntegerField(verbose_name="m robotics api code", default=1)
    operator_code = models.CharField(
        verbose_name="emantor api code", max_length=10, default="NA"
    )

    icon = models.ImageField(upload_to="operator/", null=True, blank=True)
    commission = models.DecimalField(default=0, max_digits=10, decimal_places=2)
    forward_commission = models.DecimalField(
        verbose_name="S-Distributor Commission",
        default=0,
        max_digits=10,
        decimal_places=2,
    )
    distributor_commission = models.DecimalField(
        default=0, max_digits=10, decimal_places=2
    )
    retailer_commission = models.DecimalField(
        default=0, max_digits=10, decimal_places=2
    )

    percentage = models.DecimalField(
        default=0, max_digits=10, decimal_places=2, editable=False
    )

    api_code = models.IntegerField(verbose_name="plan api code", 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 = "Operator"
        verbose_name = "Operator"

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


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

    lapu_id = models.CharField(max_length=200)
    lapu_no = models.CharField(max_length=200, null=True, blank=True)
    operator = models.ForeignKey(
        Operator, on_delete=models.CASCADE, limit_choices_to={"is_active": True}
    )
    balance = models.DecimalField(default=0, max_digits=8, decimal_places=2)

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

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


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

    name = models.CharField(max_length=200)
    api_code = models.IntegerField(verbose_name="plan api code", default=0)
    operator_code = models.CharField(
        verbose_name="emantor api code", max_length=10, default="NA"
    )

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

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


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

    name = models.CharField(max_length=200)
    image = models.ImageField(upload_to="banners/", 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 = "Banners"
        verbose_name = "Banner"

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


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

    distributor = models.ForeignKey(
        "distributors.distributors", on_delete=models.CASCADE
    )

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

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


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

    message = models.TextField()

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

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


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

    instance_id = models.CharField(max_length=200)
    access_token = models.CharField(max_length=200, null=True)
    api_domain = models.CharField(max_length=200, null=True)

    class Meta:
        verbose_name_plural = "Whatsapp Instances"
        verbose_name = "Whatsapp Instance"

    def __str__(self):
        return self.instance_id


class MessagesType(models.IntegerChoices):
    WELCOME = 1
    REGISTRATION = 2
    WALLET_CREDIT = 3


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

    type = models.IntegerField(
        default=MessagesType.WELCOME, choices=MessagesType.choices
    )
    name = models.CharField(max_length=200)
    message = models.CharField(max_length=200, null=True)

    class Meta:
        verbose_name_plural = "Whatsapp Messages"
        verbose_name = "Whatsapp Message"

    def __str__(self):
        return self.name


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

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

    class Meta:
        verbose_name_plural = "KYC Settings"
        verbose_name = "KYC Setting"

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


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

    operator = models.ForeignKey(
        Operator, 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 = "Manual Recharge"
        verbose_name = "Manual Recharge"

    def __str__(self):
        if self.operator:
            return str(self.operator.name)
        return "NA"



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

    operator_from = models.ForeignKey(
        Operator, related_name='operator_from', on_delete=models.SET_NULL, null=True, blank=True
    )
    amount = models.DecimalField(max_digits=10, decimal_places=2, default=0)
    operator_to = models.ForeignKey(
        Operator, related_name='operator_to', 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 = "Transfer Recharge"
        verbose_name = "Transfer Recharge"

    def __str__(self):
        if self.operator_from:
            return str(self.operator_from.name)
        return "NA"
