from django.db import models
from django.conf import settings
from jupyterlab_server import slugify
from core.models import BaseModel


class ProgramLevel(models.TextChoices):
    BACHELORS = "bachelors", "Bachelors"
    MASTERS = "masters", "Masters"
    DIPLOMA = "diploma", "Diploma"
    CERTIFICATE = "certificate", "Certificate"
    PHD = "phd", "PhD"


class ProgramMode(models.TextChoices):
    FULL_TIME = "full_time", "Full Time"
    PART_TIME = "part_time", "Part Time"
    ONLINE = "online", "Online"
    HYBRID = "hybrid", "Hybrid"


class ProgramStatus(models.TextChoices):
    ACTIVE = "active", "Active"
    INACTIVE = "inactive", "Inactive"
    UPCOMING = "upcoming", "Upcoming"
    DISCONTINUED = "discontinued", "Discontinued"


class Program(BaseModel):
    name = models.CharField(max_length=255)
    code = models.CharField(max_length=20, unique=True, blank=True)
    abbreviation = models.CharField(max_length=20, blank=True)
    department = models.CharField(max_length=255, blank=True)
    faculty_name = models.CharField(
        max_length=255, blank=True
    )

    level = models.CharField(
        max_length=20, choices=ProgramLevel.choices, default=ProgramLevel.BACHELORS
    )
    mode = models.CharField(
        max_length=20, choices=ProgramMode.choices, default=ProgramMode.FULL_TIME
    )
    status = models.CharField(
        max_length=20, choices=ProgramStatus.choices, default=ProgramStatus.ACTIVE
    )

    duration = models.CharField(max_length=100, blank=True)
    total_semesters = models.PositiveSmallIntegerField(null=True, blank=True)
    total_credit_hours = models.PositiveSmallIntegerField(null=True, blank=True)
    description = models.TextField(blank=True)
    eligibility = models.TextField(blank=True)
    career_prospects = models.TextField(blank=True)

    annual_fee = models.DecimalField(
        max_digits=10, decimal_places=2, null=True, blank=True
    )
    admission_fee = models.DecimalField(
        max_digits=10, decimal_places=2, null=True, blank=True
    )
    image_url = models.URLField(blank=True)
    brochure_url = models.URLField(blank=True)

    courses = models.JSONField(default=list, blank=True)
    is_featured = models.BooleanField(default=False)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ["level", "name"]


class Semester(BaseModel):
    """Detailed semester-wise curriculum."""

    program = models.ForeignKey(
        Program, on_delete=models.CASCADE, related_name="semesters"
    )
    number = models.PositiveSmallIntegerField()  # 1, 2, 3 …
    title = models.CharField(max_length=100, blank=True)  # e.g. "First Semester"
    credit_hours = models.PositiveSmallIntegerField(default=0)

    def __str__(self):
        return f"{self.program.name} — Semester {self.number}"

    class Meta:
        ordering = ["number"]
        unique_together = [["program", "number"]]


class Course(models.Model):
    """Individual course inside a semester."""

    class CourseType(models.TextChoices):
        THEORY = "theory", "Theory"
        PRACTICAL = "practical", "Practical"
        PROJECT = "project", "Project"
        ELECTIVE = "elective", "Elective"

    semester = models.ForeignKey(
        Semester, on_delete=models.CASCADE, related_name="course_list"
    )
    code = models.CharField(max_length=20, blank=True,unique= True)
    name = models.CharField(max_length=255)
    credit_hours = models.PositiveSmallIntegerField(default=3)
    course_type = models.CharField(
        max_length=20, choices=CourseType.choices, default=CourseType.THEORY
    )

    def save(self, *args, **kwargs):
        if not self.code:
            self.code = slugify(self.name)[:20]
        super().save(*args, **kwargs)
    def __str__(self):
        return f"{self.code} — {self.name}"

    class Meta:
        ordering = ["code", "name"]
