from django.db import models
from django.conf import settings
from django.shortcuts import reverse
from django_countries.fields import CountryField

class UserProfile(models.Model):
	user = models.OneToOneField(
		settings.AUTH_USER_MODEL, on_delete=models.CASCADE)


CATEGORY_CHOICES = (
	('HB', 'Health & Beauty'),
	('FA', 'Fashion'),
	('HF', 'Home & Office Furniture'),
	('GA', 'Gaming'),
	('EL', 'Electronics'),
	('HA', 'Home-Appliance'),
	)

LABEL_CHOICES = (
	('SA', 'sale'),
	('SO', 'sold-Out'),
	('HO', 'hot'),
	)


class Item(models.Model):	
	title          = models.CharField(max_length=100)
	thumbnail      = models.ImageField()
	discount_price = models.FloatField(blank=True, null=True)
	price          = models.FloatField()
	slug           = models.SlugField()
	category       = models.CharField(choices=CATEGORY_CHOICES, max_length=2)
	label          = models.CharField(choices=LABEL_CHOICES, max_length=2)
	description    = models.TextField()
	shop_owner     = models.CharField(max_length=100, blank = True, null = True)

	def __str__(self):
		return self.title

	def get_absolute_url(self):
		return reverse('core:product', kwargs={'slug': self.slug})

	def get_add_to_cart_url(self):
		return reverse('core:add-to-cart', kwargs={'slug': self.slug})

	def get_remove_from_cart_url(self):
		return reverse('core:remove-from-cart', kwargs={'slug': self.slug})


class OrderItem(models.Model):
	user     = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE)
	ordered  = models.BooleanField(default=False)
	item     = models.ForeignKey(Item, on_delete = models.CASCADE)
	quantity = models.IntegerField(default = 1)
	ordered  = models.BooleanField(default=False)

	def __str__(self):
		return f"{self.quantity} of {self.item.title}"

	def get_total_item_price(self):
		return self.quantity * self.item.price

	def get_total_discount_item_price(self):
		return self.quantity * self.item.discount_price

	def get_amount_saved(self):
	 	return self.get_total_item_price() - self.get_total_discount_item_price()

	def get_final_price(self):
		if self.item.discount_price:
			return self.get_total_discount_item_price()
		return self.get_total_item_price()


class Order(models.Model):
	user         = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE)
	items        = models.ManyToManyField(OrderItem)
	start_date   = models.DateTimeField(auto_now_add = True)
	ordered_date = models.DateTimeField()
	ordered      = models.BooleanField(default=False)
	billing_address = models.ForeignKey(
		'BillingAddress', on_delete=models.SET_NULL, blank=True, null=True)
	Payment = models.ForeignKey(
		'Payment', on_delete=models.SET_NULL, blank=True, null=True)

	def __str__(self):
		return self.user.username

	def get_total(self):
		total = 0	
		for order_item in self.items.all():
			total += order_item.get_final_price()
		return total 


class BillingAddress(models.Model):
	user               = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE)
	telephone_no       = models.CharField(max_length=100)
	apartment_add      = models.CharField(max_length=100)
	country            = CountryField(multiple=False)       
	postal_code        = models.CharField(max_length=100)


	def __str__(self):
		return self.user.username

class CheckOutAddress(models.Model):
	user               = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE)
	telephone_no       = models.CharField(max_length=100)
	email_address      = models.EmailField()
	


	def __str__(self):
		return self.user.username

class Payment(models.Model):
	mpesa_charge_id = models.CharField(max_length=50)
	user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.SET_NULL,blank = True, null =True)
	amount = models.FloatField()
	timestamp = models.DateTimeField(auto_now_add=True)


	def __str__(self):
		return self.user.username