from django.db import models
from django.conf import settings


# ---------------- Admission Main Model ----------------
class Admission(models.Model):

    CATEGORY_CHOICES = [
        ('General', 'General'),
        ('OBC', 'OBC'),
        ('SC', 'SC'),
        ('ST', 'ST'),
        ('EWS', 'EWS'),
    ]

    GENDER_CHOICES = [
        ('Male', 'Male'),
        ('Female', 'Female'),
        ('Transgender', 'Transgender Person'),
    ]

    MEDIUM_CHOICES = [
        ('Hindi', 'Hindi'),
        ('English', 'English'),
    ]

    YES_NO = [
        ('Yes', 'Yes'),
        ('No', 'No'),
    ]

    EXAM_CHOICES = [
        ('UPSC', 'UPSC'),
        ('MPPSC', 'MPPSC'),
    ]

    # ---------------- Admission Status ----------------
    STATUS_CHOICES = [
        ('Pending', 'Pending'),
        ('Approved', 'Approved'),
        ('Rejected', 'Rejected'),
    ]

    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name="admission_profile"
    )

    # ---------------- Student Details ----------------
    
    name = models.CharField(max_length=255)
    father_name = models.CharField(max_length=255)
    mother_name = models.CharField(max_length=255)

    present_address = models.TextField()
    permanent_address = models.TextField(blank=True)
    same_as_present = models.BooleanField(default=False)

    date_of_birth = models.DateField()

    category = models.CharField(max_length=20, choices=CATEGORY_CHOICES)

    disability = models.CharField(max_length=3, choices=YES_NO)
    udid = models.CharField(max_length=100, blank=True, null=True)

    aadhar_number = models.CharField(max_length=12)
    aadhar_file = models.FileField(upload_to='aadhar/')
    photo = models.ImageField(upload_to='student_photos/')

    email = models.EmailField()
    mobile = models.CharField(max_length=10)
    alternate_mobile = models.CharField(max_length=10, blank=True)

    gender = models.CharField(max_length=20, choices=GENDER_CHOICES)
    medium = models.CharField(max_length=20, choices=MEDIUM_CHOICES)

    # ---------------- Guardian Details ----------------
    guardian_name = models.CharField(max_length=255)
    relation = models.CharField(max_length=100)
    guardian_mobile = models.CharField(max_length=10)
    guardian_alternate_mobile = models.CharField(max_length=10, blank=True)

    # ✅ Declaration
    declaration = models.BooleanField(default=False)

    # ---------------- Coaching Interest ----------------
    coaching_exam = models.CharField(max_length=20, choices=EXAM_CHOICES)
    coaching_year = models.CharField(max_length=10)

    # ---------------- Office Use Only ----------------

    registration_fee = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        blank=True,
        null=True
    )

    student_id = models.CharField(
        max_length=50,
        blank=True,
        null=True,
        unique=True
    )

    batch_time = models.CharField(
        max_length=100,
        blank=True,
        null=True
    )

    batch_number = models.CharField(
        max_length=50,
        blank=True,
        null=True
    )

    status = models.CharField(
        max_length=20,
        choices=STATUS_CHOICES,
        default='Pending'
    )
    
    created_at = models.DateTimeField(auto_now_add=True)

    def save(self, *args, **kwargs):
        if self.same_as_present:
            self.permanent_address = self.present_address
        super().save(*args, **kwargs)

    def __str__(self):
        return f"{self.name} ({self.user.email})"


# ---------------- Previous Attempts ----------------
class PreviousAttempt(models.Model):
    admission = models.ForeignKey(
        Admission,
        on_delete=models.CASCADE,
        related_name="attempts"
    )

    agency_name = models.CharField(max_length=100)
    exam_year = models.CharField(max_length=10)
    post_name = models.CharField(max_length=255)
    performance = models.CharField(max_length=255, blank=True)
    rank = models.CharField(max_length=50, blank=True)
    percentage = models.CharField(max_length=10, blank=True)

    def __str__(self):
        return f"{self.agency_name} - {self.exam_year}"


# ---------------- Qualification ----------------
class Qualification(models.Model):

    QUALIFICATION_TYPES = [
        ('High School', 'High School (10th)'),
        ('Higher Secondary', 'Higher Secondary (12th)'),
        ('Graduation', 'Graduation'),
        ('Post Graduation', 'Post Graduation'),
        ('Others', 'Others'),
    ]

    admission = models.ForeignKey(
        Admission,
        on_delete=models.CASCADE,
        related_name="qualifications"
    )

    qualification_type = models.CharField(
        max_length=50,
        choices=QUALIFICATION_TYPES
    )

    passing_year = models.CharField(max_length=10)
    marks = models.CharField(max_length=20)
    subject_stream = models.CharField(max_length=200)
    board_university = models.CharField(max_length=200)
    school_college = models.CharField(max_length=255)

    # ✅ Separate marksheet upload for each qualification
    marksheet = models.FileField(
        upload_to='marksheets/',
        blank=True,
        null=True
    )

    uploaded_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.qualification_type} - {self.admission.name}"

