from django.db import models
from apps.shop.models import Product


class Order(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    email = models.EmailField()
    address = models.CharField(max_length=250)
    postal_code = models.CharField(max_length=20)
    city = models.CharField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    paid = models.BooleanField(default=False)
    STATUS_CHOICES = (
        ('Новый', 'Новый'),
        ('В обработке', 'В обработке'),
        ('Уточняется', 'Уточняется'),
        ('Доставка', 'Доставка'),
        ('Выполнен', 'Выполнен'),
    )
    status = models.CharField(default='Новый', max_length=20, choices=STATUS_CHOICES, blank=True)

    class Meta:
        ordering = ('-created',)
        verbose_name = 'Заказ'
        verbose_name_plural = 'Заказы'

    def __str__(self):
        return f'Order {self.pk}'

    def get_total_cost(self):
        return sum(item.cost for item in self.items.all())


class OrderItem(models.Model):
    order = models.ForeignKey(Order, related_name='items', on_delete=models.CASCADE)
    product = models.ForeignKey(Product, related_name='order_items', on_delete=models.CASCADE)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    quantity = models.PositiveIntegerField(default=1)

    def __str__(self):
        return '{}'.format(self.id)

    @property
    def cost(self):
        return float(self.price * self.quantity)
# Create your models here.
