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

class EmploymentType(models.TextChoices):
    FULL_TIME = "full_time", "Full Time"
    PART_TIME = "part_time", "Part Time"
    VISITING = "visiting", "Visiting"
    ADJUNCT = "adjunct", "Adjunct"
    CONTRACT = "contract", "Contract"


class FacultyStatus(models.TextChoices):
    ACTIVE = "active", "Active"
    ON_LEAVE = "on_leave", "On Leave"
    RETIRED = "retired", "Retired"
    RESIGNED = "resigned", "Resigned"


class Faculty(BaseModel):
    full_name = models.CharField(max_length=255)
    email = models.EmailField(blank=True)
    phone = models.CharField(max_length=20, blank=True)
    photo_url = models.URLField(blank=True)
    bio = models.TextField(blank=True)
    date_of_birth = models.DateField(null=True, blank=True)
    designation = models.CharField(max_length=255, blank=True)
    department = models.CharField(max_length=255, blank=True)
    specialization = models.CharField(max_length=255, blank=True)
    qualification = models.CharField(max_length=255, blank=True)
    experience_years = models.PositiveSmallIntegerField(default=0)
    joined_date = models.DateField(null=True, blank=True)
    employment_type = models.CharField(
        max_length=20, choices=EmploymentType.choices, default=EmploymentType.FULL_TIME
    )
    status = models.CharField(
        max_length=20, choices=FacultyStatus.choices, default=FacultyStatus.ACTIVE
    )
    linkedin_url = models.URLField(blank=True)
    google_scholar_url = models.URLField(blank=True)
    personal_website = models.URLField(blank=True)
    research_interests = models.TextField(blank=True)

    is_featured = models.BooleanField(default=False)
    order = models.PositiveSmallIntegerField(default=0)

    def __str__(self):
        return self.full_name

    class Meta:
        verbose_name_plural = "Faculty"
        ordering = ["order", "full_name"]


class FacultyQualification(BaseModel):
    """Multiple academic degrees per faculty member."""

    class DegreeLevel(models.TextChoices):
        BACHELORS = "bachelors", "Bachelors"
        MASTERS = "masters", "Masters"
        PHD = "phd", "PhD"
        POSTDOC = "postdoc", "Post-Doctorate"
        DIPLOMA = "diploma", "Diploma"
        CERTIFICATE = "certificate", "Certificate"

    faculty = models.ForeignKey(
        Faculty, on_delete=models.CASCADE, related_name="qualifications"
    )
    degree = models.CharField(max_length=255)
    level = models.CharField(max_length=20, choices=DegreeLevel.choices)
    institution = models.CharField(max_length=255)
    year_completed = models.PositiveSmallIntegerField(null=True, blank=True)

    def __str__(self):
        return f"{self.faculty.full_name} — {self.degree}"

    class Meta:
        ordering = ["-year_completed"]


class Publication(BaseModel):
    """Research papers / books published by faculty."""

    class PublicationType(models.TextChoices):
        JOURNAL = "journal", "Journal Article"
        CONFERENCE = "conference", "Conference Paper"
        BOOK = "book", "Book"
        BOOK_CHAPTER = "book_chapter", "Book Chapter"
        OTHER = "other", "Other"

    faculty = models.ForeignKey(
        Faculty, on_delete=models.CASCADE, related_name="publications"
    )
    title = models.CharField(max_length=500)
    publication_type = models.CharField(
        max_length=20, choices=PublicationType.choices, default=PublicationType.JOURNAL
    )
    journal_or_venue = models.CharField(max_length=255, blank=True)
    year = models.PositiveSmallIntegerField(null=True, blank=True)
    doi_or_url = models.URLField(blank=True)
    co_authors = models.CharField(max_length=500, blank=True)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ["-year"]
