from django.db import models
from admissions.models import Program


class GalleryImage(models.Model):
    CATEGORY_CHOICES = [
        ('training',    'Training & Classes'),
        ('community',   'Community Events'),
        ('graduation',  'Graduation & Ceremonies'),
        ('facilities',  'Our Facilities'),
        ('other',       'Other'),
    ]
    title = models.CharField(max_length=150)
    description = models.TextField(blank=True)
    image = models.ImageField(upload_to='gallery/')
    category = models.CharField(max_length=20, choices=CATEGORY_CHOICES, default='other')
    display_order = models.PositiveIntegerField(default=0, help_text='Lower numbers appear first')
    is_published = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['display_order', '-created_at']

    def __str__(self):
        return self.title


class Testimonial(models.Model):
    name = models.CharField(max_length=100)
    role = models.CharField(max_length=150, blank=True, help_text='e.g. Graduate, Electrical Installation 2024')
    program = models.ForeignKey(
        Program, on_delete=models.SET_NULL,
        null=True, blank=True, related_name='testimonials'
    )
    quote = models.TextField()
    photo = models.ImageField(upload_to='testimonials/', blank=True)
    video_url = models.URLField(
        blank=True,
        help_text='Optional YouTube video URL (e.g. https://www.youtube.com/watch?v=...)'
    )
    is_published = models.BooleanField(default=False)
    display_order = models.PositiveIntegerField(default=0, help_text='Lower numbers appear first')
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['display_order', '-created_at']

    def __str__(self):
        return f"{self.name} — {self.program or 'General'}"

    @property
    def youtube_embed_url(self):
        if not self.video_url:
            return ''
        url = self.video_url
        if 'youtube.com/watch?v=' in url:
            video_id = url.split('v=')[1].split('&')[0]
            return f'https://www.youtube.com/embed/{video_id}'
        if 'youtu.be/' in url:
            video_id = url.split('youtu.be/')[1].split('?')[0]
            return f'https://www.youtube.com/embed/{video_id}'
        return url
