# apps/ordenes/models.py
from django.db import models

class ProductionOrder(models.Model):
    pk_AbsoluteEntry = models.BigIntegerField(primary_key=True)
    att_DocumentNumber = models.IntegerField()
    att_ItemNo = models.CharField(max_length=50)
    att_ProductDescription = models.CharField(max_length=200)
    att_ProductionOrderStatus = models.CharField(max_length=50)
    att_ProductionOrderType = models.CharField(max_length=50)
    msr_PlannedQuantity = models.DecimalField(max_digits=10, decimal_places=2)
    msr_CompletedQuantity = models.DecimalField(max_digits=10, decimal_places=2)
    att_InventoryUOM = models.CharField(max_length=20)
    att_PostingDate = models.DateField()
    att_StartDate = models.DateField()
    att_DueDate = models.DateField()
    att_Warehouse = models.CharField(max_length=20)
    att_Remarks = models.TextField(blank=True, null=True)
    att_UserSignature = models.IntegerField(blank=True, null=True)
    att_U_Joli_Turno = models.CharField(max_length=10, blank=True, null=True)
    msr_U_Joli_N_Operarios = models.IntegerField(default=1)
    att_CreationDate = models.DateTimeField()
    att_ClosingDate = models.DateTimeField(blank=True, null=True)
    att_DistributionRule = models.CharField(max_length=20, blank=True, null=True)
    att_U_Joli_Causales_Incumplimiento = models.CharField(max_length=50, blank=True, null=True)
    att_EstadoSync = models.CharField(max_length=20, default="activo")

    def __str__(self):
        return f"{self.att_DocumentNumber} - {self.att_ProductDescription}"


class ProductionOrderLine(models.Model):
    pk_LineID = models.AutoField(primary_key=True)
    fk_AbsoluteEntry = models.ForeignKey(
        ProductionOrder,
        on_delete=models.CASCADE,
        related_name="lineas"
    )
    att_LineNumber = models.IntegerField()
    att_ItemNo = models.CharField(max_length=50)
    att_ItemName = models.CharField(max_length=200)
    msr_BaseQuantity = models.DecimalField(max_digits=10, decimal_places=4)
    msr_PlannedQuantity = models.DecimalField(max_digits=10, decimal_places=2)
    msr_IssuedQuantity = models.DecimalField(max_digits=10, decimal_places=2)
    att_ProductionOrderIssueType = models.CharField(max_length=20)
    att_Warehouse = models.CharField(max_length=20)
    att_ItemType = models.CharField(max_length=20)
    att_StartDate = models.DateField()
    att_EndDate = models.DateField()
    att_VisualOrder = models.IntegerField()

    def __str__(self):
        return f"{self.att_ItemNo} ({self.fk_AbsoluteEntry_id})"