from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser
from django.template.defaultfilters import slugify

# class User(AbstractUser):
#     UserType = models.CharField(max_length=55, choices=(("Costumer", "Costumer"), ("Seller","Seller")))


STATE_CHOICES = (
('Andhra','Andhra'),
('Arunachal','Arunachal'),
('Assam','Assam'),
('Bihar','Bihar'),
('Chhattisgarh','Chhattisgarh'),
('Goa','Goa'),
('Gujarat','Gujarat'),
('Haryana','Haryana'),
('Himachal','Himachal'),
('Jharkhand','Jharkhand'),
('Karnataka','Karnataka'),
('Kerala','Kerala'),
('Madhya','Madhya'),
('Maharashtra','Maharashtra'),
('Manipur','Manipur'),
('Meghalaya','Meghalaya'),
('Mizoram','Mizoram'),
('Nagaland','Nagaland'),
('Odisha','Odisha'),
('Punjab','Punjab'),
('Rajasthan','Rajasthan'),
('Sikkim','Sikkim'),
('Tamil','Tamil'),
('Telangana','Telangana'),
('Tripura','Tripura'),
('UttarPradesh','UttarPradesh'),
('Uttarakhand','Uttarakhand'),
('WestBengal','WestBengal'),
)

STATUS_CHOICES = (
    ("Accepted","Accepted"),
    ("Packed","Packed"),
    ("On The Way", "On The Way"),
    ("Delivered","Delivered"),
    ("Cancel","Cancel"),
    ("Pending","Pending"),
)

class Customer(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    locality = models.CharField(max_length=100)
    city = models.CharField(max_length=100)
    mobile = models.IntegerField(default=0)
    zipcode = models.IntegerField()
    state = models.CharField(max_length=100, choices=STATE_CHOICES)
    def __str__(self):
        return self.name    

class Categories(models.Model):
    title = models.CharField(max_length=100)
    def __str__(self):
        return  self.title
    
class SubCate(models.Model):
    name = models.CharField(max_length=100)
    category = models.ForeignKey(Categories, on_delete=models.PROTECT, null=True, blank=True)
    def __str__(self):
        return self.name

# Create your models here.
class Product(models.Model):
    title= models.CharField(max_length=100)
    selling_price= models.IntegerField(default=0)
    discount = models.IntegerField(default=0)
    description = models.TextField()
    categories = models.ForeignKey(Categories, on_delete=models.PROTECT)
    subCate = models.ForeignKey(SubCate, on_delete=models.PROTECT, null=True, blank=True)
    product_image  = models.ImageField(upload_to='products/')

    def __str__(self):
        return  self.title
    

class Cart(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField(default=1)
    
    @property
    def total_cost(self):
        return self.quantity * self.product.selling_price
    
    
# class Payment(models.Model):
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     amount = models.FloatField()
#     order_id = models.CharField(max_length=100,blank=True, null=True)
#     payment_Status = models.CharField(max_length=100,blank=True, null=True)
#     payment_id = models.CharField(max_length=100,blank=True, null=True)
#     paid = models.BooleanField(default=False)

    
class OrderPlaced(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    product  = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField(default=1)
    ordered_date  = models.DateTimeField(auto_now_add=True)
    status = models.CharField(max_length=50, choices=STATUS_CHOICES, default='Pending')
    # payment = models.ForeignKey(Payment, on_delete=models.CASCADE , default='')
    payment = models.CharField(max_length=100, default="Cash On Delivery", blank=True, null=True)
    amount = models.FloatField(default=0)

    @property
    def total_cost(self):
        return self.quantity * self.product.selling_price


class WishList(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    
    def __str__(self):
        return self.product.title
    