from django.db import models
import uuid
from accounts.models import Users
from encrypted_model_fields.fields import EncryptedCharField


# Create your models here.

class ProductionUnit(models.Model):
    uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, unique=True, editable=False)
    pu_name = models.CharField(max_length=100)
    pu_code = models.CharField(max_length=200, unique=True)
    pu_location = models.CharField(max_length=200)
    street = models.CharField(max_length=200, blank=True, null=True)
    city = models.CharField(max_length=100, blank=True, null=True)
    district = models.CharField(max_length=50, blank=True, null=True)
    state_or_province = models.CharField(max_length=100, blank=True, null=True)
    pin_code = models.IntegerField(blank=True, null=True)
    latitude = models.FloatField()
    longitude = models.FloatField()
    gst = models.CharField(max_length=50, null=True)
    contact_no = models.CharField(max_length=20, blank=True, null=True)
    email = models.EmailField(max_length=200, blank=True, null=True)
    status = models.CharField(max_length=100, choices=[('close', 'close'), ('open', 'open'), (
        'Currently not accepting any order', 'Currently not accepting any order')])
    # delivery_mode = models.CharField(max_length=200, choices=[("shop own", 'shop own'), ('third party', 'third party')])
    created_at = models.DateTimeField(auto_now_add=True, null=True)
    updated_date = models.DateTimeField(auto_now=True, null=True)


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


class Shop(models.Model):
    uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, unique=True, editable=False)
    unit_name=models.CharField(max_length=100)
    unit_code = models.CharField(max_length=200, unique=True)
    unit_location=models.CharField(max_length=200)
    street=models.CharField(max_length=200, blank=True, null=True)
    city=models.CharField(max_length=100, blank=True, null=True)
    district=models.CharField(max_length=50,blank=True,null=True)
    state_or_province=models.CharField(max_length=100, blank=True, null=True)
    pin_code=models.IntegerField(blank=True, null=True)
    latitude = models.FloatField()
    longitude = models.FloatField()
    contact_no = models.CharField(max_length = 20, blank=True, null=True)
    email = models.EmailField(max_length=200, blank=True, null=True)
    status = models.CharField(max_length=100, choices=[('Close', 'Close'),('Open', 'Open'),  (
    'Currently not accepting any order', 'Currently not accepting any order')])
    delivery_mode = models.CharField(max_length=200, choices = [("Shop own", 'Shop own'), ('Third party', 'Third party')])
    unit_admin_user=models.ManyToManyField('accounts.Users', null=True)
    created_at = models.DateTimeField(auto_now_add=True, null=True)
    updated_date = models.DateTimeField(auto_now=True, null= True)
    gst = models.CharField(max_length=50, null=True)
    delivery_radius = models.FloatField(default=0.0)
    
    def __str__(self):
        name = self.unit_name
        location = self.unit_location
        return f"{name} - {location}"

class BankDetails(models.Model):
    shop = models.OneToOneField(Shop, on_delete=models.CASCADE, related_name='bank_details')
    account_name = EncryptedCharField(max_length=100)
    bank_name = EncryptedCharField(max_length=100)
    branch_name = EncryptedCharField(max_length=100)
    ifsc_code = EncryptedCharField(max_length=11)
    account_number = EncryptedCharField(max_length=20)
    created_date = models.DateTimeField(auto_now_add=True, null=True)
    updated_date = models.DateTimeField(auto_now=True, null= True)

    def __str__(self):
        return f"Bank details for {self.shop.unit_name}"