from django.db import models
from django.utils.text import slugify

class Project(models.Model):
    TYPES = (
        ('MOVIE', 'Feature Film'),
        ('SERIES', 'TV Series'),
        ('SHORT', 'Short Film'),
    )
    STATUS_CHOICES = (
        ('PRE', 'Pre-Production'),
        ('PROD', 'Production'),
        ('POST', 'Post-Production'),
        ('DONE', 'Completed'),
    )

    title = models.CharField(max_length=255)
    slug = models.SlugField(unique=True, blank=True)
    project_type = models.CharField(max_length=10, choices=TYPES, default='MOVIE')
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='PRE')
    description = models.TextField(blank=True)
    thumbnail_url = models.URLField(blank=True, help_text="URL to a representative image")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.title)
        super().save(*args, **kwargs)

    def __str__(self):
        return self.title

class PipelineStep(models.Model):
    STAGES = (
        # Pre-Production
        ('SCRIPT', 'Roteiro & Storyboard'),
        ('CONCEPT', 'Concept Art'),
        ('ANIMATIC', 'Animatic'),
        # Production
        ('MODELING', 'Modelagem 3D'),
        ('TEXTURING', 'Texturização'),
        ('RIGGING', 'Rigging'),
        ('ANIMATION', 'Animação'),
        ('LIGHTING', 'Iluminação'),
        ('FX', 'Simulação (FX)'),
        # Post-Production
        ('RENDERING', 'Renderização'),
        ('COMPOSITING', 'Composição'),
        ('EDITING', 'Edição & Sonoplastia'),
    )
    project = models.ForeignKey(Project, related_name='steps', on_delete=models.CASCADE)
    name = models.CharField(max_length=20, choices=STAGES)
    progress = models.PositiveIntegerField(default=0, help_text="Progress from 0 to 100")
    is_completed = models.BooleanField(default=False)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['id']

    def __str__(self):
        return f"{self.project.title} - {self.get_name_display()}"

class CgiAsset(models.Model):
    ASSET_TYPES = (
        ('CHAR', 'Character'),
        ('PROP', 'Prop'),
        ('ENV', 'Environment'),
    )
    project = models.ForeignKey(Project, related_name='assets', on_delete=models.CASCADE)
    name = models.CharField(max_length=255)
    asset_type = models.CharField(max_length=10, choices=ASSET_TYPES)
    is_realistic = models.BooleanField(default=True)
    current_stage = models.CharField(max_length=100, default='Modeling')
    
    def __str__(self):
        return f"{self.name} ({self.get_asset_type_display()})"