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

class Plant(models.Model):
    name = models.CharField(max_length=255)
    location = models.CharField(max_length=255, blank=True)
    
    def __str__(self):
        return self.name

class Category(models.Model):
    name = models.CharField(max_length=100)
    
    class Meta:
        verbose_name_plural = "Categories"
        
    def __str__(self):
        return self.name

class Product(models.Model):
    name = models.CharField(max_length=255)
    sku = models.CharField(max_length=100, unique=True)
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
    description = models.TextField(blank=True)
    
    def __str__(self):
        return f"{self.name} ({self.sku})"

class Inventory(models.Model):
    plant = models.ForeignKey(Plant, on_delete=models.CASCADE, related_name='inventory')
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.DecimalField(max_digits=12, decimal_places=2, default=0)
    lot_number = models.CharField(max_length=100, blank=True)
    
    class Meta:
        verbose_name_plural = "Inventory"
        unique_together = ('plant', 'product', 'lot_number')

    def __str__(self):
        return f"{self.product.name} at {self.plant.name} - Lot: {self.lot_number or 'N/A'}"

class Machine(models.Model):
    STATUS_CHOICES = [
        ('active', 'Active'),
        ('maintenance', 'Maintenance'),
        ('broken', 'Broken'),
    ]
    name = models.CharField(max_length=255)
    plant = models.ForeignKey(Plant, on_delete=models.CASCADE, related_name='machines')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='active')
    last_maintenance = models.DateField(null=True, blank=True)

    def __str__(self):
        return f"{self.name} ({self.plant.name})"

class WorkOrder(models.Model):
    STATUS_CHOICES = [
        ('planned', 'Planned'),
        ('in_progress', 'In Progress'),
        ('completed', 'Completed'),
        ('cancelled', 'Cancelled'),
    ]
    order_number = models.CharField(max_length=50, unique=True)
    plant = models.ForeignKey(Plant, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField()
    machine = models.ForeignKey(Machine, on_delete=models.SET_NULL, null=True, blank=True)
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='planned')
    start_date = models.DateTimeField(null=True, blank=True)
    end_date = models.DateTimeField(null=True, blank=True)

    def __str__(self):
        return self.order_number

class Inspection(models.Model):
    RESULT_CHOICES = [
        ('pass', 'Pass'),
        ('fail', 'Fail'),
    ]
    work_order = models.ForeignKey(WorkOrder, on_delete=models.CASCADE, related_name='inspections')
    inspector = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    result = models.CharField(max_length=10, choices=RESULT_CHOICES)
    notes = models.TextField(blank=True)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"Inspection for {self.work_order.order_number} - {self.result}"