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

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 EducationalLevel(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)

    def __str__(self):
        return self.name_en

class Subject(models.Model):
    level = models.ForeignKey(EducationalLevel, on_delete=models.CASCADE, related_name='subjects')
    teacher = models.ForeignKey(Teacher, on_delete=models.SET_NULL, null=True, 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)

    def __str__(self):
        return self.name_en

class Resource(models.Model):
    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)
    file = models.FileField(_("File"), upload_to='resources/', blank=True, null=True)
    link = models.URLField(_("External Link"), blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title_en

class Student(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='student_profile')
    level = models.ForeignKey(EducationalLevel, on_delete=models.SET_NULL, null=True, blank=True, related_name='students')
    phone_number = models.CharField(_("Phone Number"), max_length=20, blank=True)
    subscribed_subjects = models.ManyToManyField(Subject, blank=True, related_name='subscribers')

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