from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Produit(models.Model):
    nom = models.CharField(max_length=255)
    description = models.TextField()
    prix = models.FloatField()
    image = models.ImageField(upload_to='images/')

    def __str__(self):
        return self.nom
    
    def get_image_url(self):
        if self.image:
            return self.image.url
        return None

class Category_user_set(models.Model):
    
    cat_parent_pf = models.CharField(max_length=200)
    cat_parent_mp = models.CharField(max_length=200)
    cat_imporation = models.CharField(max_length=200)
    user = models.IntegerField(default=0)
    type_oper1 = models.IntegerField(default=0)
    type_oper2 = models.IntegerField(default=0)
    type_oper3 = models.IntegerField(default=0)

    def __str__(self):
        return self.cat_parent_pf

class Product(models.Model):
    id = models.IntegerField(primary_key=True)
    code = models.CharField(max_length=255)
    name = models.CharField(max_length=255)
    stock = models.FloatField(default=0)
    stock_prv = models.FloatField(default=0)
    stock_min = models.FloatField(default=0)
    flag = models.BooleanField(default=False)
    def __str__(self):
        return self.code
    
    def get_id(self):
        return self.id

class Obj(models.Model):
    cmp_id = models.IntegerField()
    code = models.CharField(max_length=255)
    name = models.CharField(max_length=255)
    cmp_qty = models.FloatField()

class Bom(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=255)
    code = models.CharField(max_length=255)
    product_id = models.ForeignKey(Product,related_name='boms', on_delete=models.CASCADE)
    product_qty = models.IntegerField()

    def __str__(self):
        return self.code
    
    def get_id(self):
        return self.id


    
class Bom_lines(models.Model):
    id = models.IntegerField(primary_key=True)
    product_id = models.ForeignKey(Product, related_name='product_id',on_delete=models.CASCADE)
    
    product_qty = models.IntegerField()
    bom_id = models.ForeignKey(Bom, related_name='bom_lines',on_delete=models.CASCADE)
    def get_id(self):
        return self.id
    
    
    
        
class Author(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='books')

    def __str__(self):
        return self.title