from django.db import models
from Models.oportunidad_de_venta_model import OportunidadDeVenta

class Producto(models.Model):
    id = models.AutoField(primary_key=True)
    codigo = models.CharField(max_length=100, null=True, blank=True, unique=True)
    nombre = models.CharField(max_length=255)
    descripcion = models.TextField(null=True, blank=True)
    valor = models.DecimalField(max_digits=12, decimal_places=2)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    deleted_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        db_table = 'producto'
        managed = True

    def __str__(self):
        return self.nombre

class OportunidadProducto(models.Model):
    id = models.AutoField(primary_key=True)
    # Use string reference or import. Importing is fine.
    # Note: OportunidadDeVenta is in Models package. 
    oportunidad = models.ForeignKey(OportunidadDeVenta, on_delete=models.CASCADE, db_column='oportunidad_id', related_name='productos')
    producto = models.ForeignKey(Producto, on_delete=models.CASCADE, db_column='producto_id', related_name='oportunidades')
    cantidad = models.IntegerField(default=1)
    precio_unitario = models.DecimalField(max_digits=12, decimal_places=2, help_text="Precio al momento de la venta")
    
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    deleted_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        db_table = 'oportunidad_producto'
        managed = True
        indexes = [
            models.Index(fields=['oportunidad', 'producto']),
        ]

    def __str__(self):
        return f"{self.oportunidad} - {self.producto}"
