from django.db import models
from django.conf import settings
from core.models import BaseModel

class GalleryCategory(models.TextChoices):
    CAMPUS      = "campus",      "Campus"
    EVENTS      = "events",      "Events"
    ACTIVITIES  = "activities",  "Activities"
    SPORTS      = "sports",      "Sports"
    GRADUATION  = "graduation",  "Graduation"
    LABS        = "labs",        "Laboratories"
    OTHER       = "other",       "Other"


class Album(BaseModel):
    """Groups images into named albums."""
    title       = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    category    = models.CharField(max_length=20, choices=GalleryCategory.choices, default=GalleryCategory.OTHER)
    cover_image = models.URLField(blank=True)
    date        = models.DateField(null=True, blank=True)
    is_published= models.BooleanField(default=True)
    is_featured = models.BooleanField(default=False)

    def __str__(self):
        return self.title

    @property
    def image_count(self):
        return self.images.count()

    class Meta:
        ordering = ["-is_featured", "-date", "-created_at"]


class GalleryImage(BaseModel):

    image_url   = models.URLField()
    thumbnail_url = models.URLField(blank=True)   # smaller version
    caption     = models.CharField(max_length=255, blank=True)
    alt_text    = models.CharField(max_length=255, blank=True)

    album       = models.ForeignKey(Album, on_delete=models.SET_NULL, null=True, blank=True, related_name="images")
    category    = models.CharField(max_length=20, choices=GalleryCategory.choices, default=GalleryCategory.CAMPUS)
  
    order       = models.PositiveSmallIntegerField(default=0)
    is_featured = models.BooleanField(default=False)
    is_published= models.BooleanField(default=True)

    


    def __str__(self):
        return self.caption or f"Image {self.pk}"

    class Meta:
        ordering = ["-is_featured", "order", "-created_at"]
