from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.utils.text import slugify
import os

def photo_upload_path(instance, filename):
    """Generate upload path for photos"""
    # Get the content type and object
    content_type = instance.content_type.model
    obj = instance.content_object
    
    # Get object identifier (slug or id)
    identifier = getattr(obj, 'slug', obj.id)
    
    # Create path: content_type/identifier/filename
    base, ext = os.path.splitext(filename)
    new_filename = f"{slugify(base)}{ext}"
    return f"{content_type}/{identifier}/{new_filename}"

class Photo(models.Model):
    """Generic photo model that can be attached to any model"""
    image = models.ImageField(upload_to=photo_upload_path)
    caption = models.CharField(max_length=255, blank=True)
    alt_text = models.CharField(max_length=255, blank=True)
    is_primary = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    # Generic foreign key fields
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    
    class Meta:
        ordering = ['-is_primary', '-created_at']
        indexes = [
            models.Index(fields=['content_type', 'object_id']),
        ]
    
    def __str__(self):
        return f"{self.content_type} - {self.content_object} - {self.caption or 'No caption'}"
    
    def save(self, *args, **kwargs):
        # If this is marked as primary, unmark other primary photos
        if self.is_primary:
            Photo.objects.filter(
                content_type=self.content_type,
                object_id=self.object_id,
                is_primary=True
            ).exclude(id=self.id).update(is_primary=False)
        super().save(*args, **kwargs)
