from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Category(models.Model):
    name = models.CharField(max_length=255)

    class Meta:
        verbose_name_plural = 'Categories'

    def __str__(self):
        return self.name

class Brand(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name

class Product(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='products')
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE, related_name='products')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='products')
    name = models.CharField(max_length=255)
    price = models.IntegerField()

    def is_owner(self, request):
        return self.user.id == request.user.id


    def __str__(self):
        return self.name

class Comment(models.Model):
    message = models.TextField()
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='comments')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comments')

    def is_owner(self, request):
        return self.user.id == request.user.id

    def __str__(self):
        return self.message

class OrderManager(models.Manager):
    def for_user(self, user):
        return self.filter(user=user)

class Order(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='orders')
    objects = OrderManager()

    objects = OrderManager()

    def is_owner(self, request):
        return self.user.id == request.user.id

    def __str__(self):
        return self.product

class OrderManager(models.Manager):
    def for_user(self, user):
        return self.filter(user = user)
