"""Request/response schemas for the recruitment module (resume parsing, JD
generation).

Every model field has a default so a partial LLM reply (a resume missing a
section, a JD reply missing preferred_skills) still yields a valid, fully
shaped response instead of a validation error.
"""
from typing import Any

from pydantic import BaseModel, model_validator


class _Lenient(BaseModel):
    """LLMs return `null` for fields they could not find. Drop those keys so
    the field default (`""`, `[]`, `0`) applies instead of a validation error."""

    @model_validator(mode="before")
    @classmethod
    def _drop_nulls(cls, data: Any) -> Any:
        if isinstance(data, dict):
            return {k: v for k, v in data.items() if v is not None}
        return data


class Candidate(_Lenient):
    name: str = ""
    email: str = ""
    phone: str = ""
    location: str = ""
    linkedin: str = ""
    portfolio: str = ""


class Skill(_Lenient):
    skill_name: str = ""


class Experience(_Lenient):
    company: str = ""
    job_title: str = ""
    start_date: str = ""
    end_date: str = ""
    location: str = ""


class Education(_Lenient):
    degree: str = ""
    field_of_study: str = ""
    institution: str = ""
    start_date: str = ""
    end_date: str = ""
    grade: str = ""


class Certification(_Lenient):
    certificate_name: str = ""


class Project(_Lenient):
    name: str = ""
    description: str = ""
    technologies: list[str] = []
    url: str = ""


class ResumeParseResponse(_Lenient):
    candidate: Candidate = Candidate()
    professional_summary: str = ""
    skills: list[Skill] = []
    experience: list[Experience] = []
    highest_education_name: str = ""
    education: list[Education] = []
    certifications: list[Certification] = []
    projects: list[Project] = []
    total_experience_years: float = 0
    availability: str = ""
    keywords: list[str] = []


# ── JD generation ────────────────────────────────────────────────────────────


class JDGenerateRequest(_Lenient):
    job_id: int
    role: str
    department: str = ""
    # Free-text (e.g. "5 years") rather than a number: recruiters phrase this
    # inconsistently ("5+ years", "5-7 yrs"), and the LLM reads it as context
    # for the JD and salary estimate rather than a value the API computes on.
    min_range: str = ""
    max_range: str = ""
    location: str = ""


class SalaryRange(_Lenient):
    min_range: int = 0
    max_range: int = 0


class JobDescriptionSections(_Lenient):
    job_summary: str = ""
    responsibilities: list[str] = []
    required_qualifications: list[str] = []
    required_skills: list[str] = []
    preferred_skills: list[str] = []


class InterviewQuestion(_Lenient):
    question: str = ""
    evaluation_criteria: str = ""


class JDGenerateResponse(_Lenient):
    # Defaulted, not required: the LLM never sets this field itself — the
    # service overwrites it with the request's job_id after parsing.
    job_id: int = 0
    ai_salary_range: SalaryRange = SalaryRange()
    job_description: JobDescriptionSections = JobDescriptionSections()
    ai_interview_questions: list[InterviewQuestion] = []
