from django.db import models
from django.contrib.auth.models import User
from ckeditor.fields import RichTextField

from .validators import validate_file_extension


GENDERS = (
    ('Male', 'Male'),
    ('Female', 'Female'),
    ('Other', 'Other')
)

CLASSES = (
    (1, 1),
    (2, 2),
    (3, 3),
    (4, 4),
    (5, 5),
    (6, 6),
    (7, 7),
    (8, 8),
    (9, 9),
    (10, 10)
)

SECTIONS = (
    ('A', 'A'),
    ('B', 'B'),
    ('C', 'C'),
    ('D', 'D'), 
    ('E', 'E'),
)

SHIFTS = (
    ('Morning', 'Morning'),
    ('Day', 'Day'),
    ('Evening', 'Evening')
)

SCHOOL_TYPES = (
    ("Boys' School", "Boys' School"),
    ("Girls' School", "Girls' School"),
    ('Co-education School', 'Co-education School')
)

class AbstractBaseNotice(models.Model):
    file = models.FileField(upload_to="notice_file", blank=False, null=False, validators=[validate_file_extension])
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)

    class Meta:
        abstract = True


class School(models.Model):
    name = models.CharField(max_length=100, help_text="Rajshahi Govt Laboratory High School, Rajshahi", unique=True, blank=False, null=False)
    established_year = models.CharField(help_text="1969", max_length=40, blank=False, null=False)
    logo = models.ImageField(upload_to="school_logo", blank=True, null=True)
    school_pic = models.ImageField(upload_to="school_pic", blank=True, null=True)
    address = models.CharField(max_length=100, help_text="Laxmipur, Rajpara, Rajshahi", blank=False, null=False)
    motto = models.CharField(max_length=100, help_text="জ্ঞানই শক্তি", blank=True, null=True)
    eiin = models.IntegerField(help_text="123456", blank=True, null=True, unique=True)
    school_type = models.CharField(max_length=20, help_text="Boys School", blank=True, null=True, choices=SCHOOL_TYPES)
    # about = models.TextField(help_text="About school", blank=True, null=True)
    about = RichTextField(blank=False, null=False)
    total_shift = models.IntegerField(help_text="3", blank=True, null=True)
    shifts = models.CharField(max_length=100, help_text="Morning, Day, Evening", blank=True, null=True)
    total_student = models.IntegerField(help_text="1000", blank=True, null=True)
    total_teacher = models.IntegerField(help_text="50", blank=True, null=True)
    total_staff = models.IntegerField(help_text="20", blank=True, null=True)
    total_building = models.IntegerField(help_text="5", blank=True, null=True)
    total_classroom = models.IntegerField(help_text="20", blank=True, null=True)
    total_lab = models.IntegerField(help_text="5", blank=True, null=True)
    total_computer_lab = models.IntegerField(help_text="1", blank=True, null=True)
    total_library = models.IntegerField(help_text="1", blank=True, null=True)
    total_playground = models.IntegerField(help_text="1", blank=True, null=True)
    total_auditorium = models.IntegerField(help_text="1", blank=True, null=True)
    contact_no = models.CharField(max_length=15, help_text="01700000000", blank=True, null=True)
    email = models.EmailField(help_text="rajshahi.govt.laboratory@gmail.com", blank=True, null=True)
    website = models.URLField(help_text="https://www.rglhs.edu.bd", blank=True, null=True)
    facebook = models.URLField(help_text="https://www.facebook.com/rglhs", blank=True, null=True)
    twitter = models.URLField(help_text="https://www.twitter.com/rglhs", blank=True, null=True)
    youtube = models.URLField(help_text="https://www.youtube.com/rglhs", blank=True, null=True)
    instagram = models.URLField(help_text="https://www.instagram.com/rglhs", blank=True, null=True)
    embedded_map = models.TextField(blank=True, null=True)


    def save(self, *args, **kwargs):
        if self.__class__.objects.count():
            self.pk = self.__class__.objects.first().pk
        super().save(*args, **kwargs)


class Student(models.Model):
    name = models.CharField(max_length=100, help_text="Rafiul Islam", blank=False, null=False)
    gender = models.CharField(max_length=10, help_text="Male", blank=False, null=False, choices=GENDERS)
    photo = models.ImageField(upload_to="student_photo", blank=True, null=True)
    enrolled_class = models.IntegerField(help_text="10", blank=False, null=False, choices=CLASSES)
    shift = models.CharField(max_length=10, help_text="Morning", default="Morning", blank=False, null=False, choices=SHIFTS)
    section = models.CharField(max_length=15, help_text="A", default="A", blank=False, null=False, choices=SECTIONS)
    roll = models.IntegerField(help_text="1", blank=False, null=False)
    registration_number = models.IntegerField(help_text="123456", blank=True, null=True, unique=True)
    date_of_birth = models.DateField(help_text="2000-01-01", blank=True, null=True)
    address = models.CharField(max_length=100, help_text="Rajshahi", blank=True, null=True)
    phone = models.CharField(max_length=15, help_text="01700000000", blank=True, null=True)
    guardian_name = models.CharField(max_length=100, help_text="Md. Abdul Karim", blank=True, null=True)
    guardian_phone = models.CharField(max_length=15, help_text="01700000000", blank=True, null=True)
    guardian_address = models.CharField(max_length=100, help_text="Rajshahi", blank=True, null=True)


    class Meta:
        ordering = ['shift', 'enrolled_class', 'section', 'roll']
        unique_together = ('shift', 'enrolled_class', 'section', 'roll',)


class Teacher(models.Model):
    name = models.CharField(max_length=100, help_text="Md. Abdul Karim", blank=False, null=False)
    employee_id = models.IntegerField(help_text="123456", blank=True, null=True, unique=True)
    photo = models.ImageField(upload_to="teacher_photo", blank=True, null=True)
    academic_qualification = RichTextField(blank=True, null=True)
    designation = models.CharField(max_length=100, help_text="Assistant Teacher", blank=True, null=True)
    teaching_subject = models.CharField(max_length=50, help_text="Bangla", blank=True, null=True)
    joining_date = models.DateField(help_text="2000-01-21", blank=True, null=True)
    address = models.CharField(max_length=100, help_text="Rajshahi", blank=True, null=True)
    phone = models.CharField(max_length=15, help_text="01700000000", blank=True, null=True)
    email = models.EmailField(help_text="abulkarim@gmial.com", blank=True, null=True)
    former = models.BooleanField(default=False, blank=False, null=False)
    about = RichTextField(help_text="About teacher", blank=True, null=True)
    is_headmaster = models.BooleanField(default=False, blank=False, null=False)
    headmasters_speech = RichTextField(help_text="Headmaster's speech", blank=True, null=True)


    def save(self, *args, **kwargs):
        if self.is_headmaster and not self.headmasters_speech:
            raise ValueError("Headmaster's speech is required for headmaster")
        super().save(*args, **kwargs)


class Notice(AbstractBaseNotice):
    name = models.CharField(max_length=300, help_text="Notice name", blank=False, null=False)
    class Meta:
        ordering = ['-updated_at']


class Result(AbstractBaseNotice):
    name = models.CharField(max_length=300, help_text="Result name", blank=False, null=False)
    class Meta:
        ordering = ['-updated_at']


class Routine(AbstractBaseNotice):
    name = models.CharField(max_length=300, help_text="Routine name", blank=False, null=False)
    class Meta:
        ordering = ['-updated_at']


class Syllabus(AbstractBaseNotice):
    name = models.CharField(max_length=300, help_text="Syllabus name", blank=False, null=False)
    class Meta:
        ordering = ['-updated_at']
        verbose_name_plural = 'Syllabus'


class ClassAdmin(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=300, help_text="Class name", blank=False, null=False)
    enrolled_class = models.IntegerField(help_text="10", blank=False, null=False, choices=CLASSES)
    shift = models.CharField(max_length=10, help_text="Morning", default="Morning", blank=False, null=False, choices=SHIFTS)
    section = models.CharField(max_length=15, help_text="A", default="A", blank=False, null=False, choices=SECTIONS)
    class Meta:
        ordering = ['name']

class Carousel(models.Model):
    photo_one = models.ImageField(upload_to="carousel_file", blank=False, null=False)
    photo_two = models.ImageField(upload_to="carousel_file", blank=True, null=True)
    photo_three = models.ImageField(upload_to="carousel_file", blank=True, null=True)
    celebration_image = models.ImageField(upload_to="carousel_file", blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)


    class Meta:
        ordering = ['id']

    
class President(models.Model):
    name = models.CharField(max_length=100, help_text="Md. Abdul Karim", blank=False, null=False)
    photo = models.ImageField(upload_to="president_photo", blank=True, null=True)
    about = RichTextField(help_text="About president", blank=True, null=True)


    def save(self, *args, **kwargs):
        if self.__class__.objects.count():
            self.pk = self.__class__.objects.first().pk
        super().save(*args, **kwargs)


class Founder(models.Model):
    name = models.CharField(max_length=100, help_text="Md. Abdul Karim", blank=False, null=False)
    photo = models.ImageField(upload_to="founder_photo", blank=True, null=True)
    about = RichTextField(help_text="About founder", blank=True, null=True)


    def save(self, *args, **kwargs):
        if self.__class__.objects.count():
            self.pk = self.__class__.objects.first().pk
        super().save(*args, **kwargs)