from django.db import models
from django.contrib.auth.models import AbstractUser
import os
from django.core.validators import FileExtensionValidator


def student_id_upload_path(instance, filename):
    """
    Upload path for student ID images
    Example: uploads/student_ids/REG123.jpg
    """
    ext = filename.split('.')[-1]
    filename = f"{instance.registration_number}.{ext}"
    return os.path.join('uploads/student_ids/', filename)


class User(AbstractUser):
    """
    Simple Custom User Model for BUSA
    """

    # Personal Details
    phone_number = models.CharField(
        max_length=15,
        unique=True,
        null=True,
        blank=True
    )

    county = models.CharField(max_length=100, blank=True)
    constituency = models.CharField(max_length=100, blank=True)

    # Education Details
    institution = models.CharField(max_length=200, blank=True)
    course = models.CharField(max_length=200, blank=True)

    registration_number = models.CharField(
        max_length=50,
        null=True,
        blank=True
    )

    # Student ID Upload ONLY
    student_id = models.ImageField(
        upload_to=student_id_upload_path,
        null=True,
        blank=True
    )

    # Simple Approval System
    is_approved = models.BooleanField(default=False)

    # Timestamp
    date_registered = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.get_full_name()} ({self.username})"



class Gallery(models.Model):
    caption = models.CharField(max_length=255)

    image = models.ImageField(
        upload_to='gallery/',
        validators=[FileExtensionValidator(
            allowed_extensions=['jpg', 'jpeg', 'png']
        )]
    )

    uploaded_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-uploaded_at']
        verbose_name_plural = "Gallery"

    def __str__(self):
        return self.caption



class Download(models.Model):
    name = models.CharField(max_length=255)

    file = models.FileField(
        upload_to='downloads/',
        validators=[FileExtensionValidator(allowed_extensions=['pdf'])]
    )

    uploaded_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-uploaded_at']

    def __str__(self):
        return self.name


class Official(models.Model):

    POSITION_CHOICES = [
        ("patron", "Patron"),
        ("chairperson", "Chairperson"),
        ("vice_chairperson", "Vice Chairperson"),
        ("secretary_general", "Secretary General"),
        ("finance_secretary", "Finance Secretary"),
        ("organizing_secretary", "Organizing Secretary"),
        ("public_relations_officer", "Public Relations Officer"),
        ("spokesperson", "Spokesperson"),
        ("gender_representative", "Gender Representative"),
        ("coordinator", "Coordinator"),
    ]

    name = models.CharField(max_length=255)

    position = models.CharField(
        max_length=50,
        choices=POSITION_CHOICES
    )

    image = models.ImageField(
        upload_to='officials/',
        null=True,
        blank=True,
        validators=[FileExtensionValidator(
            allowed_extensions=['jpg', 'jpeg', 'png']
        )]
    )

    description = models.TextField(
        null=True,
        blank=True
    )

    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['position']

    def __str__(self):
        return f"{self.name} - {self.get_position_display()}"