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

class EventCategory(models.TextChoices):
    ACADEMIC = "academic", "Academic"
    CULTURAL = "cultural", "Cultural"
    SPORTS = "sports", "Sports"
    SEMINAR = "seminar", "Seminar"
    WORKSHOP = "workshop", "Workshop"
    COMPETITION = "competition", "Competition"
    OTHER = "other", "Other"


class EventStatus(models.TextChoices):
    UPCOMING = "upcoming", "Upcoming"
    ONGOING = "ongoing", "Ongoing"
    COMPLETED = "completed", "Completed"
    CANCELLED = "cancelled", "Cancelled"
    POSTPONED = "postponed", "Postponed"


class Event(BaseModel):
    title = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255, unique=True, blank=True)
    category = models.CharField(
        max_length=20, choices=EventCategory.choices, default=EventCategory.OTHER
    )
    status = models.CharField(
        max_length=20, choices=EventStatus.choices, default=EventStatus.UPCOMING
    )
    description = models.TextField(blank=True)
    date = models.DateField()
    end_date = models.DateField(null=True, blank=True)
    time = models.TimeField(null=True, blank=True)
    end_time = models.TimeField(null=True, blank=True)
    location = models.CharField(max_length=255, blank=True)
    location_detail = models.CharField(max_length=255, blank=True)
    is_online = models.BooleanField(default=False)
    online_link = models.URLField(blank=True)
    image_url = models.URLField(blank=True)
    banner_url = models.URLField(blank=True)
    is_registration_open = models.BooleanField(default=False)
    registration_url = models.URLField(blank=True)
    max_participants = models.PositiveIntegerField(null=True, blank=True)
    registration_deadline = models.DateField(null=True, blank=True)
    organizer = models.CharField(max_length=255, blank=True)
    contact_email = models.EmailField(blank=True)
    is_featured = models.BooleanField(default=False)
    is_past = models.BooleanField(default=False)
    views = models.PositiveIntegerField(default=0)

    def __str__(self):
        return self.title

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


class EventGalleryImage(BaseModel):
    """Photos taken during/after an event."""

    event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name="gallery")
    image_url = models.URLField()
    caption = models.CharField(max_length=255, blank=True)
    uploaded_by = models.ForeignKey(
        "users.User", on_delete=models.SET_NULL, null=True, related_name="event_gallery_images"
    )

    def __str__(self):
        return f"{self.event.title} — image {self.pk}"

    class Meta:
        ordering = ["created_at"]
