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


class AdmissionStatus(models.TextChoices):
    PENDING = "pending", "Pending"
    REVIEWING = "reviewing", "Reviewing"
    SHORTLISTED = "shortlisted", "Shortlisted"
    ACCEPTED = "accepted", "Accepted"
    REJECTED = "rejected", "Rejected"
    WAITLISTED = "waitlisted", "Waitlisted"
    WITHDRAWN = "withdrawn", "Withdrawn"


class ApplicationSource(models.TextChoices):
    WEBSITE = "website", "Website"
    WALK_IN = "walk_in", "Walk-in"
    REFERRAL = "referral", "Referral"
    SOCIAL_MEDIA = "social_media", "Social Media"
    OTHER = "other", "Other"


class Gender(models.TextChoices):
    MALE = "male", "Male"
    FEMALE = "female", "Female"
    OTHER = "other", "Other"
    PREFER_NOT = "prefer_not", "Prefer not to say"


class AdmissionInquiry(BaseModel):
    full_name = models.CharField(max_length=255)
    email = models.EmailField()
    phone = models.CharField(max_length=20, blank=True)
    gender = models.CharField(max_length=15, choices=Gender.choices, blank=True)
    date_of_birth = models.DateField(null=True, blank=True)
    address = models.TextField(blank=True)
    nationality = models.CharField(max_length=100, blank=True)
    last_institution = models.CharField(max_length=255, blank=True)
    last_qualification = models.CharField(
        max_length=255, blank=True
    )  # e.g. "+2 Science"
    last_gpa_or_percent = models.CharField(max_length=20, blank=True)
    passing_year = models.PositiveSmallIntegerField(null=True, blank=True)
    program = models.ForeignKey(
        "programs.Program",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="inquiries",
    )
    academic_year = models.CharField(max_length=20, blank=True)  # e.g. "2026-27"
    message = models.TextField(blank=True)
    source = models.CharField(
        max_length=20,
        choices=ApplicationSource.choices,
        default=ApplicationSource.WEBSITE,
    )

    citizenship_url = models.URLField(blank=True)
    transcript_url = models.URLField(blank=True)
    photo_url = models.URLField(blank=True)
    status = models.CharField(
        max_length=20, choices=AdmissionStatus.choices, default=AdmissionStatus.PENDING
    )
    reviewed_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="reviewed_inquiries",
    )
    reviewed_at = models.DateTimeField(null=True, blank=True)
    admin_notes = models.TextField(blank=True)

    def __str__(self):
        return f"{self.full_name} → {self.program}"

    class Meta:
        verbose_name_plural = "Admission Inquiries"
        ordering = ["-created_at"]


class AdmissionStatusHistory(BaseModel):
    """Full audit trail of status changes for each inquiry."""

    inquiry = models.ForeignKey(
        AdmissionInquiry, on_delete=models.CASCADE, related_name="status_history"
    )
    old_status = models.CharField(
        max_length=20, choices=AdmissionStatus.choices, blank=True
    )
    new_status = models.CharField(max_length=20, choices=AdmissionStatus.choices)
    changed_by = models.ForeignKey(
        "users.User", on_delete=models.SET_NULL, null=True
    )
    note = models.TextField(blank=True)

    def __str__(self):
        return f"{self.inquiry} | {self.old_status} → {self.new_status}"

    class Meta:
        ordering = ["-created_at"]
