from django.db import models
from django.urls import reverse

# Create your models here.

class Category(models.Model):
    name=models.CharField(max_length=200,db_index=True)
    slug=models.CharField(max_length=200,db_index=True)

    class Meta:
        ordering=['name']
        verbose_name='Категория'
        verbose_name_plural='Категории'
    def __str__(self):
            return self.name
    def get_absolute_url(self):
        return reverse('shop:ProductListByCategory',args=[self.slug])

class Product(models.Model):
        category=models.ForeignKey(Category,on_delete=models.CASCADE,related_name='products',verbose_name='Категория')
        name=models.CharField(max_length=200,db_index=True,verbose_name='Название')
        slug=models.SlugField(max_length=200,db_index=True)
        image=models.ImageField(upload_to='products/%Y/%m/%d/',blank=True,verbose_name='Изображение товара')
        discription=models.TextField(blank=True,verbose_name='Описание')
        price=models.DecimalField(max_digits=10,decimal_places=2,verbose_name='Цена')
        stock=models.PositiveIntegerField(verbose_name='На складе')
        available=models.BooleanField(default=True,verbose_name='Доступен')
        created=models.DateTimeField(auto_now_add=True)
        updated=models.DateTimeField(auto_now=True)
        class Meta:
            ordering=['name']
            index_together=[
            'id','slug']
        def __str__(self):
            return self.name
        def get_absolute_url(self):
            return reverse('shop:ProductDetail',args=[self.id,self.slug])
        def all_order_items(self):
            return sum(item.quantity for item in self.order_items.all())
class Some(models.Model):
    slug=models.SlugField(max_length=200,db_index=True)
    name=models.CharField(max_length=200)
    detail=models.PositiveIntegerField(verbose_name='На складе')
    check=models.BooleanField(default=True,verbose_name='Доступен')
    def __str__(self):
        return self.name[:50]
    def get_absolute_url(self):
        return reverse('shop:ProductDetail',args=[self.id,self.slug])
