from django.db import models
from django.utils.text import slugify


class Course(models.Model):
    """Main course model representing educational programs"""
    title = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255, unique=True)
    short_description = models.CharField(max_length=500)
    overview = models.TextField()
    duration = models.CharField(max_length=100)
    category = models.CharField(max_length=100)
    banner_image = models.ImageField(upload_to='courses/banners/', blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.title)
        super().save(*args, **kwargs)

    def __str__(self):
        return self.title


class CourseEligibility(models.Model):
    """Eligibility criteria for a course"""
    course = models.OneToOneField(Course, on_delete=models.CASCADE, related_name='eligibility')
    academic_qualification = models.TextField()
    minimum_marks = models.TextField()
    entrance_exam = models.TextField()

    def __str__(self):
        return f"Eligibility for {self.course.title}"


class CourseFee(models.Model):
    """Fee structure for a course"""
    course = models.OneToOneField(Course, on_delete=models.CASCADE, related_name='fees')
    domestic = models.CharField(max_length=100)
    international = models.CharField(max_length=100)

    def __str__(self):
        return f"Fees for {self.course.title}"


class FeeDetail(models.Model):
    """Detailed fee breakdown by year"""
    course_fee = models.ForeignKey(CourseFee, on_delete=models.CASCADE, related_name='details')
    fee_head = models.CharField(max_length=100)
    first_year = models.CharField(max_length=100)
    second_year = models.CharField(max_length=100)
    third_year = models.CharField(max_length=100)
    fourth_year = models.CharField(max_length=100)
    is_one_time = models.BooleanField(default=False)

    def __str__(self):
        return f"{self.fee_head} for {self.course_fee.course.title}"


class HostelFee(models.Model):
    """Hostel fee structure"""
    course = models.OneToOneField(Course, on_delete=models.CASCADE, related_name='hostel_fee')
    fee_per_year = models.CharField(max_length=100)
    ac_room_additional = models.CharField(max_length=100, blank=True)
    security_deposit = models.CharField(max_length=100, blank=True)
    notes = models.TextField(blank=True)

    def __str__(self):
        return f"Hostel fees for {self.course.title}"


class Semester(models.Model):
    """Semester in a course"""
    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='semesters')
    name = models.CharField(max_length=100)
    number = models.PositiveSmallIntegerField()
    
    class Meta:
        ordering = ['number']
    
    def __str__(self):
        return f"{self.name} - {self.course.title}"


class Subject(models.Model):
    """Subject in a semester"""
    semester = models.ForeignKey(Semester, on_delete=models.CASCADE, related_name='subjects')
    code = models.CharField(max_length=20)
    name = models.CharField(max_length=255)
    credits = models.PositiveSmallIntegerField()
    description = models.TextField()

    def __str__(self):
        return f"{self.code} - {self.name}"


class CareerProspect(models.Model):
    """Career opportunities after completing the course"""
    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='career_prospects')
    title = models.CharField(max_length=255)
    salary_range = models.CharField(max_length=100, blank=True)
    description = models.TextField(blank=True)

    def __str__(self):
        return f"{self.title} - {self.course.title}"


class Recruiter(models.Model):
    """Companies that recruit students from the course"""
    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='recruiters')
    name = models.CharField(max_length=255)
    logo = models.ImageField(upload_to='courses/recruiters/')

    def __str__(self):
        return f"{self.name} - {self.course.title}"


class WhyJoin(models.Model):
    """Reasons to join the course"""
    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='why_join')
    title = models.CharField(max_length=255)
    description = models.TextField()

    def __str__(self):
        return f"{self.title} - {self.course.title}"


class CourseHighlight(models.Model):
    """Key highlights of the course"""
    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='highlights')
    title = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    icon = models.CharField(max_length=50, blank=True)  # For storing icon name

    def __str__(self):
        return f"{self.title} - {self.course.title}"


class CourseImage(models.Model):
    """Images related to the course"""
    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='images')
    image = models.ImageField(upload_to='courses/gallery/')
    title = models.CharField(max_length=255)
    is_banner = models.BooleanField(default=False)

    def __str__(self):
        return f"{self.title} - {self.course.title}"


class Testimonial(models.Model):
    """Student testimonials about the course"""
    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='testimonials')
    name = models.CharField(max_length=255)
    position = models.CharField(max_length=255)
    company = models.CharField(max_length=255)
    content = models.TextField()
    photo = models.ImageField(upload_to='courses/testimonials/', blank=True, null=True)

    def __str__(self):
        return f"{self.name} - {self.course.title}"