from django.db import models
from django.utils.text import slugify
import uuid
import random
import string

class StudioConfig(models.Model):
    """Singleton model to store studio-wide settings and the unique admin key."""
    admin_access_key = models.CharField(max_length=100, unique=True)
    is_setup = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        if not self.admin_access_key:
            # Using the key requested by the user as default
            self.admin_access_key = "61823dbc-ee05-455f-8924-764f15104fc1"
        super().save(*args, **kwargs)

    def __str__(self):
        return "Studio Configuration"

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')
    category = models.CharField(max_length=100, default='Sci-Fi')
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='PRE')
    description = models.TextField(blank=True)
    full_script = 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)
    is_ai_generated = models.BooleanField(default=False)
    
    # New fields for "Super Production"
    estimated_budget = models.CharField(max_length=100, blank=True, default="$150M")
    rating = models.CharField(max_length=10, default="PG-13")
    duration = models.CharField(max_length=50, blank=True, default="120 min")

    def save(self, *args, **kwargs):
        if not self.slug:
            base_slug = slugify(self.title)
            if not base_slug:
                base_slug = "project"
            
            unique_slug = base_slug
            while Project.objects.filter(slug=unique_slug).exists():
                random_string = ''.join(random.choices(string.ascii_lowercase + string.digits, k=4))
                unique_slug = f"{base_slug}-{random_string}"
            
            self.slug = unique_slug
        super().save(*args, **kwargs)

    def __str__(self):
        return self.title

class Scene(models.Model):
    project = models.ForeignKey(Project, related_name='scenes', on_delete=models.CASCADE)
    number = models.PositiveIntegerField(default=1)
    title = models.CharField(max_length=255)
    description = models.TextField()
    visual_prompt = models.TextField(blank=True)
    image_url = models.CharField(max_length=500, blank=True)
    
    class Meta:
        ordering = ['number']

    def __str__(self):
        return f"Scene {self.number}: {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)
    physical_description = models.TextField(blank=True, help_text="Detailed physical appearance based on real humans")
    current_stage = models.CharField(max_length=100, default='Modeling')
    version = models.PositiveIntegerField(default=1)
    file_location = models.CharField(max_length=500, blank=True, help_text="Path or URL to the digital file")
    assigned_artist = models.CharField(max_length=100, blank=True)
    
    def __str__(self):
        return f"{self.name} ({self.get_asset_type_display()})"
