from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
import re

class SingletonModel(models.Model):
    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        self.pk = 1
        super(SingletonModel, self).save(*args, **kwargs)

    @classmethod
    def load(cls):
        obj, created = cls.objects.get_or_create(pk=1)
        return obj

class Teacher(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='teacher_profile')
    bio = models.TextField(_("Bio"), blank=True)
    avatar = models.ImageField(_("Avatar"), upload_to='teachers/', blank=True, null=True)
    specialization = models.CharField(_("Specialization"), max_length=255, blank=True)

    def __str__(self):
        return self.user.get_full_name() or self.user.username

class Classroom(models.Model):
    name_en = models.CharField(_("Name (English)"), max_length=100)
    name_ar = models.CharField(_("Name (Arabic)"), max_length=100)
    description = models.TextField(_("Description"), blank=True)

    class Meta:
        verbose_name = _("Classroom")
        verbose_name_plural = _("Classrooms")

    def __str__(self):
        return self.name_en

class Subject(models.Model):
    classroom = models.ForeignKey(Classroom, on_delete=models.CASCADE, related_name='subjects')
    teachers = models.ManyToManyField(Teacher, blank=True, related_name='subjects')
    name_en = models.CharField(_("Name (English)"), max_length=200)
    name_ar = models.CharField(_("Name (Arabic)"), max_length=200)
    description_en = models.TextField(_("Description (English)"), blank=True)
    description_ar = models.TextField(_("Description (Arabic)"), blank=True)
    price = models.DecimalField(_("Price"), max_digits=10, decimal_places=2, default=0.00)
    image = models.ImageField(_("Image"), upload_to='subjects/', blank=True, null=True)
    google_drive_link = models.URLField(_("Google Drive Link"), blank=True)
    google_meet_link = models.URLField(_("Google Meet Link"), blank=True)
    youtube_live_url = models.URLField(_("YouTube Live URL"), blank=True, help_text=_("Paste the YouTube Live link here for large broadcasts (500+ students)."))

    def __str__(self):
        return self.name_en

    def get_youtube_id(self):
        """Extracts the video ID from a YouTube URL."""
        if not self.youtube_live_url:
            return None
        # Handle various formats: 
        # https://youtu.be/VIDEO_ID
        # https://www.youtube.com/watch?v=VIDEO_ID
        # https://www.youtube.com/live/VIDEO_ID
        import re
        patterns = [
            r'(?:v=|\/)([0-9A-Za-z_-]{11}).*',
            r'(?:youtu\.be\/)([0-9A-Za-z_-]{11})',
            r'(?:live\/)([0-9A-Za-z_-]{11})',
        ]
        for pattern in patterns:
            match = re.search(pattern, self.youtube_live_url)
            if match:
                return match.group(1)
        return None

class Resource(models.Model):
    RESOURCE_TYPES = (
        ('FILE', _('File (PDF, Doc, etc.)')),
        ('VIDEO', _('Video Link (YouTube)')),
        ('LINK', _('External Link')),
    )

    subject = models.ForeignKey(Subject, on_delete=models.CASCADE, related_name='resources')
    title_en = models.CharField(_("Title (English)"), max_length=200)
    title_ar = models.CharField(_("Title (Arabic)"), max_length=200)
    resource_type = models.CharField(_("Resource Type"), max_length=10, choices=RESOURCE_TYPES, default='FILE')
    file = models.FileField(_("File"), upload_to='resources/', blank=True, null=True)
    link = models.URLField(_("Link"), blank=True, help_text=_("YouTube URL for Video type, or external URL for Link type."))
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title_en

    def get_youtube_id(self):
        """Extracts the video ID from the link if it is a YouTube URL."""
        if not self.link or self.resource_type != 'VIDEO':
            return None
        
        patterns = [
            r'(?:v=|\/)([0-9A-Za-z_-]{11}).*',
            r'(?:youtu\.be\/)([0-9A-Za-z_-]{11})',
            r'(?:live\/)([0-9A-Za-z_-]{11})',
        ]
        for pattern in patterns:
            match = re.search(pattern, self.link)
            if match:
                return match.group(1)
        return None
    
    def is_image(self):
        if self.file:
            return self.file.name.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.webp'))
        return False
        
    def is_pdf(self):
        if self.file:
            return self.file.name.lower().endswith('.pdf')
        return False

class Governorate(models.Model):
    name_en = models.CharField(_("Name (English)"), max_length=100, default="")
    name_ar = models.CharField(_("Name (Arabic)"), max_length=100, default="")

    class Meta:
        verbose_name = _("Governorate")
        verbose_name_plural = _("Governorates")

    def __str__(self):
        return f"{self.name_en}"

class City(models.Model):
    governorate = models.ForeignKey(Governorate, on_delete=models.CASCADE, related_name='cities', verbose_name=_("Governorate"), null=True, blank=True)
    name_en = models.CharField(_("Name (English)"), max_length=100, default="")
    name_ar = models.CharField(_("Name (Arabic)"), max_length=100, default="")
    
    class Meta:
        verbose_name = _("City")
        verbose_name_plural = _("Cities")

    def __str__(self):
        return f"{self.name_en}"

class Student(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='student_profile')
    classroom = models.ForeignKey(Classroom, on_delete=models.SET_NULL, null=True, blank=True, related_name='students')
    phone_number = models.CharField(_("Phone Number"), max_length=20, blank=True)
    mobile_number = models.CharField(_("Mobile Number"), max_length=20, blank=True)
    subscribed_subjects = models.ManyToManyField(Subject, blank=True, related_name='subscribers')
    
    governorate = models.ForeignKey(Governorate, on_delete=models.SET_NULL, null=True, blank=True, verbose_name=_("Governorate"))
    city = models.ForeignKey(City, on_delete=models.SET_NULL, null=True, blank=True, verbose_name=_("City"))
    avatar = models.ImageField(_("Picture"), upload_to='students/', blank=True, null=True)
    
    # Email Verification
    email_otp_code = models.CharField(max_length=6, blank=True, null=True)
    is_email_verified = models.BooleanField(default=False)

    def __str__(self):
        return self.user.get_full_name() or self.user.username

# --- Configuration Models ---

class WablasConfiguration(SingletonModel):
    api_url = models.URLField(default="https://texas.wablas.com")
    api_token = models.CharField(max_length=255)
    secret_key = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        verbose_name = "Wablas Configuration"
        verbose_name_plural = "Wablas Configuration"

    def __str__(self):
        return "Wablas Configuration"

class ThawaniConfiguration(SingletonModel):
    api_key = models.CharField(max_length=255, help_text="Thawani Secret Key")
    publishable_key = models.CharField(max_length=255, help_text="Thawani Publishable Key")
    is_sandbox = models.BooleanField(default=True, help_text="Check to use Sandbox environment")

    class Meta:
        verbose_name = "Thawani Configuration"
        verbose_name_plural = "Thawani Configuration"

    def __str__(self):
        return "Thawani Configuration"

class PlatformSettings(SingletonModel):
    name = models.CharField(max_length=100, default="My School Platform")
    logo = models.ImageField(upload_to='platform/', blank=True, null=True)
    description = models.TextField(blank=True, null=True)
    contact_email = models.EmailField(blank=True, null=True)
    contact_phone = models.CharField(max_length=20, blank=True, null=True)
    address = models.TextField(blank=True, null=True)
    
    # Social Media
    facebook_link = models.URLField(blank=True, null=True)
    twitter_link = models.URLField(blank=True, null=True)
    instagram_link = models.URLField(blank=True, null=True)
    
    class Meta:
        verbose_name = "Platform Profile"
        verbose_name_plural = "Platform Profile"

    def __str__(self):
        return "Platform Profile"