from django.db import models
from django.utils import timezone
from utilz.models import ProductsModel, LinesModel


SHIFT_CHOICE = (("MORNING", "MORNING"), ("AFTERNOON", "AFTERNOON"), ("NIGHT", "NIGHT"), ("DAY", "DAY"))
STATUS_CHOICE = (('CREATED', 'CREATED'), ('ANALYSED', 'ANALYSED'), ('CHECKED', 'CHECKED'), ('APPROVED', 'APPROVED'))

class LineInspectionModel(models.Model):
    l_id = models.ForeignKey(LinesModel, related_name='line_insp', on_delete=models.PROTECT)
    date = models.DateField(default=timezone.now)
    shift = models.CharField(max_length=12, choices = SHIFT_CHOICE)
    product_id = models.ForeignKey(ProductsModel, related_name='product_insp', on_delete=models.PROTECT)
    blend_number = models.CharField(max_length=24)
    washing_method = models.CharField(max_length=24)
    inspector = models.CharField(max_length=60)
    supervisor = models.CharField(max_length=60, null=True, blank=True)
    foreman = models.CharField(max_length=30)
    status = models.CharField(max_length=12, choices = STATUS_CHOICE, default='CREATED')

    comment = models.TextField(null=True, blank=True)

    created_by = models.CharField(max_length=24)
    datetime_created = models.DateTimeField(auto_now_add=True)

    class Meta:
         verbose_name_plural = 'LINE INSPECTION SHIFT FORM'
         ordering = ('date', 'shift', 'l_id__line')

    #def get_product_type(self):
    #    product_type = ProductsModel.objects.filter(product_name=self.product).filter(product_size=self.product_size).first().product_type
    #    return product_type


    def __str__(self) -> str:
        return f"DATE : {self.date}, LINE : {self.l_id.line}, SHIFT : {self.shift}, PRODUCT : {self.product_id.product_name_short}"



HOUR_CHOICE = (('00','00'),('01','01'),('02','02'),('03','03'),('04','04'),('05','05'),('06','06'),('07','07'),('08','08'),('09','09'),('10','10'),('11','11'),('12','12'),('13','13'),('14','14'),('15','15'),('16','16'),('17','17'),('18','18'),('19','19'),('20','20'),('21','21'),('22','22'),('23','23'))
class LineInspectionFaultModel(models.Model):
    line_id = models.ForeignKey(LineInspectionModel, related_name='fault', on_delete=models.CASCADE)
    hour = models.CharField(max_length=6, choices= HOUR_CHOICE)
    fault = models.TextField()
    corrective_action = models.TextField(null=True, blank=True)
    result = models.TextField(null=True, blank=True)
    created_by = models.CharField(max_length=24)
    datetime_created = models.DateTimeField(auto_now_add=True)

    class Meta:
         verbose_name_plural = 'LINE INSPECTION FAULT BOX'
         ordering = ('line_id__date', 'line_id__shift', 'line_id__l_id__line', 'hour')

    def __str__(self) -> str:
        return f"DATE : {self.line_id.date}, LINE : {self.line_id.l_id.line}, SHIFT : {self.line_id.shift}"


from utilz.models import SachetDefectsModel
CAUSE_CHOICES = (("RAW MATERIAL", "M : RAW MATERIAL"), ("ENGINEERING", "E : ENGINEERING"), ("MAINTENANCE SETTING", "W : MAINTENANCE SETTING"), ("HUMAN", "O : HUMAN"))
class LineInspectiontDefectModel(models.Model):
    line_id = models.ForeignKey(LineInspectionModel, related_name='defect', on_delete=models.CASCADE)
    hour = models.CharField(max_length=6, choices= HOUR_CHOICE)
    defect_id = models.ForeignKey(SachetDefectsModel, related_name='defect_insp', on_delete=models.CASCADE)
    quantity = models.IntegerField()
    defect_cause = models.CharField(max_length=24, choices = CAUSE_CHOICES)
    comment = models.TextField(null=True, blank=True)
    created_by = models.CharField(max_length=24)
    datetime_created = models.DateTimeField(auto_now_add=True)

    class Meta:
         verbose_name_plural = 'LINE INSPECTION DEFECT RECORD'
         ordering = ('line_id__date', 'line_id__shift', 'line_id__l_id__line', 'hour')

    def __str__(self) -> str:
        return f"DATE : {self.line_id.date}, LINE : {self.line_id.l_id.line}, SHIFT : {self.line_id.shift}"



class LineInspectionDespatchPalletModel(models.Model):
    line_id = models.ForeignKey(LineInspectionModel, related_name='line_pallet', on_delete=models.CASCADE)
    qa_pallet_code = models.CharField(max_length=30)
    production_batch_code = models.CharField(max_length=30, null=True, blank=True)
    production_pallet_code = models.CharField(max_length=30, null=True, blank=True)
    case_code = models.CharField(max_length=30)
    comment = models.TextField(null=True, blank=True)
    created_by = models.CharField(max_length=24)
    datetime_created = models.DateTimeField(auto_now_add=True)

    class Meta:
         verbose_name_plural = 'LINE INSPECTION DESPATCHED PALLET RECORD'
         ordering = ('line_id__date', 'line_id__shift', 'line_id__l_id__line', 'qa_pallet_code')

    def __str__(self) -> str:
        return f"DATE : {self.line_id.date}, LINE : {self.line_id.l_id.line}, SHIFT : {self.line_id.shift}"




class LineInspectionWorkFlowModel(models.Model):
    line_id = models.OneToOneField(LineInspectionModel, related_name='workflow', on_delete=models.CASCADE)

    analysed_by = models.CharField(max_length=24, null=True, blank=True)
    analysed_comment = models.TextField(null=True, blank=True)
    analysed_datetime = models.DateTimeField(null=True, blank=True)

    checked_by = models.CharField(max_length=24, null=True, blank=True)
    checked_comment = models.TextField(null=True, blank=True)
    checked_datetime = models.DateTimeField(null=True, blank=True)

    approved_by = models.CharField(max_length=24, null=True, blank=True)
    approved_comment = models.TextField(null=True, blank=True)
    approved_datetime = models.DateTimeField(null=True, blank=True)

    datetime_created = models.DateTimeField(auto_now_add=True)

    class Meta:
         verbose_name_plural = 'LINE INSPECTION SHIFT WORKFLOW'
         ordering = ('line_id__date', 'line_id__shift', 'line_id__l_id__line')

    def __str__(self):
        return f'DATE : {self.line_id.date}, LINE : {self.line_id.l_id.line}, SHIFT : {self.line_id.shift}'




from django.core.validators import FileExtensionValidator
class LineInspectionFilesModel(models.Model):
    line_id = models.ForeignKey(LineInspectionModel, related_name='files', on_delete=models.CASCADE)
    file = models.FileField(validators=[FileExtensionValidator(['png','jpg','jpeg','pdf','doc','docx','cdr'])], upload_to='files_folder', blank=True, null=True)
    created_by = models.CharField(max_length=24)
    datetime_created = models.DateTimeField(auto_now_add=True)

    class Meta:
         verbose_name_plural = 'LINE INSPECTION UPLOADED FILES'
         ordering = ('line_id__date', 'line_id__shift', 'line_id__l_id__line')

    def __str__(self):
        return f'DATE : {self.line_id.date}, LINE : {self.line_id.l_id.line}, SHIFT : {self.line_id.shift}'