
"""Semantic skill matching using embeddings and answer-level evidence."""

from __future__ import annotations

import re

from app.schemas.common import JobStatus
from app.schemas.semantic_match import (
    SemanticSkillMatchRequest,
    SemanticSkillMatchResponse,
    SkillMatchItem,
)
from app.services.shared.embeddings import cosine, embed_texts


# Alternative terms that can provide relevant textual evidence for a skill.
# A mention is evidence of relevance, not proof of proficiency.
SKILL_ALIASES: dict[str, set[str]] = {
    "python": {"python", "python3"},
    "fastapi": {"fastapi", "fast api"},
    "rest api": {
        "rest api", "rest apis", "restful api", "restful apis",
        "rest endpoint", "rest endpoints",
    },
    "rest apis": {
        "rest api", "rest apis", "restful api", "restful apis",
        "rest endpoint", "rest endpoints",
    },
    "machine learning": {
        "machine learning", "ml", "supervised learning",
        "unsupervised learning", "model training", "model evaluation",
    },
    "nlp": {"nlp", "natural language processing"},
    "llm": {
        "llm", "llms", "large language model", "large language models",
        "generative ai", "prompt engineering", "openai models",
    },
    "llms": {
        "llm", "llms", "large language model", "large language models",
        "generative ai", "prompt engineering", "openai models",
    },
    "sql": {
        "sql", "sql database", "sql databases", "postgresql",
        "mysql", "sqlite", "relational database", "relational databases",
        "query design", "database queries",
    },
    "docker": {
        "docker", "dockerfile", "docker container",
        "docker containers", "containerization",
    },
    "problem solving": {
        "problem solving", "problem-solving", "troubleshooting",
        "debugging", "root cause analysis",
    },
    "backend development": {
        "backend development", "backend api", "backend apis",
        "server-side development", "server side development",
    },
    "ai integration": {
        "ai integration", "integrated ai", "llm integration",
        "model integration", "openai models",
    },
}


def _normalize(text: str) -> str:
    """Normalize punctuation and whitespace for phrase matching."""
    text = text.lower().replace("-", " ")
    text = re.sub(r"[^a-z0-9+#.]+", " ", text)
    return re.sub(r"\s+", " ", text).strip()


def _tokens(text: str) -> set[str]:
    return set(_normalize(text).split())


def _skill_aliases(skill: str) -> set[str]:
    key = _normalize(skill)
    aliases = set(SKILL_ALIASES.get(key, {key}))
    if key:
        aliases.add(key)
    return {alias for alias in aliases if alias}


def _has_explicit_evidence(skill: str, answer: str) -> bool:
    """Check whole phrases to avoid accidental substring matches."""
    normalized_answer = f" {_normalize(answer)} "
    return any(
        f" {_normalize(alias)} " in normalized_answer
        for alias in _skill_aliases(skill)
    )


def _lexical_score(skill: str, answer: str) -> float:
    """Calculate lexical evidence from a skill or one of its aliases."""
    if not answer.strip():
        return 0.0

    if _has_explicit_evidence(skill, answer):
        return 1.0

    skill_tokens = _tokens(skill)
    answer_tokens = _tokens(answer)
    if not skill_tokens:
        return 0.0

    overlap = len(skill_tokens & answer_tokens) / len(skill_tokens)
    return max(0.0, min(1.0, overlap))


def _answers(payload: SemanticSkillMatchRequest) -> list[str]:
    texts = [
        answer.strip()
        for answer in payload.candidate_answers
        if answer and answer.strip()
    ]

    for turn in payload.transcript:
        if (
            (turn.role or "").lower() == "candidate"
            and (turn.text or "").strip()
        ):
            texts.append(turn.text.strip())

    # Keep the service safe for requests without candidate answers.
    return texts


def _score_against_answers(
    term: str,
    term_vector: list[float],
    answers: list[str],
    answer_vectors: list[list[float]],
) -> dict:
    """Find the most relevant answer using embeddings and lexical evidence."""
    candidates: list[dict] = []

    for answer, answer_vector in zip(answers, answer_vectors):
        if not answer.strip():
            continue

        semantic = max(
            0.0,
            min(1.0, float(cosine(term_vector, answer_vector))),
        )
        lexical = _lexical_score(term, answer)
        explicit = _has_explicit_evidence(term, answer)

        # Hybrid score: semantic similarity plus direct textual evidence.
        combined = 0.55 * semantic + 0.45 * lexical

        candidates.append({
            "answer": answer,
            "semantic": semantic,
            "lexical": lexical,
            "explicit": explicit,
            "combined": max(0.0, min(1.0, combined)),
        })

    if not candidates:
        return {
            "answer": None,
            "semantic": 0.0,
            "lexical": 0.0,
            "explicit": False,
            "combined": 0.0,
        }

    # Prefer an answer containing explicit evidence for the requested term.
    explicit_candidates = [
        candidate for candidate in candidates if candidate["explicit"]
    ]
    pool = explicit_candidates or candidates

    return max(pool, key=lambda candidate: candidate["combined"])


class SemanticSkillMatchService:
    async def match(
        self, payload: SemanticSkillMatchRequest
    ) -> SemanticSkillMatchResponse:
        answers = _answers(payload)
        answer_blob = "\n".join(answers)

        skills = list(payload.required_skills)

        # Retain basic JD-based skill discovery if the caller omits skills.
        if not skills:
            tokens = re.findall(
                r"[A-Za-z][A-Za-z0-9+.#-]{2,}",
                payload.job_description,
            )
            seen: set[str] = set()

            for token in tokens:
                normalized = token.lower()
                if normalized in seen:
                    continue

                if token[0].isupper() or normalized in {
                    "python", "fastapi", "postgresql", "redis",
                    "kubernetes", "aws", "docker", "sql",
                }:
                    seen.add(normalized)
                    skills.append(token)

                if len(skills) >= 8:
                    break

            if not skills:
                skills = ["role fundamentals", "communication"]

        competencies = list(payload.demonstrated_competencies)

        jd_text = (
            f"{payload.role_title}\n"
            f"{payload.job_description}\n"
            + ", ".join(skills)
        )

        # Embed the whole answer block and each answer separately. This lets
        # the matcher locate the best evidence instead of using one giant block.
        embedding_answers = answers or [""]
        corpus = [
            jd_text,
            answer_blob,
            *skills,
            *competencies,
            *embedding_answers,
        ]

        vectors, provider, stub = await embed_texts(corpus)

        jd_vector = vectors[0]
        combined_answer_vector = vectors[1]

        skill_start = 2
        skill_end = skill_start + len(skills)
        skill_vectors = vectors[skill_start:skill_end]

        competency_start = skill_end
        competency_end = competency_start + len(competencies)
        competency_vectors = vectors[competency_start:competency_end]

        answer_start = competency_end
        answer_vectors = vectors[answer_start:]

        if answers:
            overall_similarity = max(
                0.0,
                min(1.0, float(cosine(jd_vector, combined_answer_vector))),
            )
        else:
            overall_similarity = 0.0

        skill_matches: list[SkillMatchItem] = []

        for skill, skill_vector in zip(skills, skill_vectors):
            result = _score_against_answers(
                skill, skill_vector, answers, answer_vectors
            )
            score = round(result["combined"], 3)
            explicit = result["explicit"]

            if explicit:
                evidence = (
                    "Explicit skill-related term found in the answer; "
                    "this is textual evidence, not proof of proficiency."
                )
            elif score >= 0.45:
                evidence = (
                    "Semantic or partial lexical match; review the answer "
                    "to verify practical evidence."
                )
            else:
                evidence = "Weak semantic and lexical evidence in the answers."

            skill_matches.append(
                SkillMatchItem(
                    skill=skill,
                    similarity=score,
                    matched=bool(answers) and (explicit or score >= 0.45),
                    best_answer_excerpt=(
                        result["answer"][:220] if result["answer"] else None
                    ),
                    evidence=evidence,
                )
            )

        competency_matches: list[SkillMatchItem] = []

        for competency, competency_vector in zip(
            competencies, competency_vectors
        ):
            result = _score_against_answers(
                competency, competency_vector, answers, answer_vectors
            )
            score = round(result["combined"], 3)
            explicit = result["explicit"]

            if explicit:
                evidence = "Explicit competency-related evidence appears in an answer."
            elif score >= 0.40:
                evidence = "Semantic alignment found; review the answer for supporting evidence."
            else:
                evidence = "Limited semantic or lexical evidence for this competency."

            competency_matches.append(
                SkillMatchItem(
                    skill=competency,
                    similarity=score,
                    matched=bool(answers) and (explicit or score >= 0.40),
                    best_answer_excerpt=(
                        result["answer"][:220] if result["answer"] else None
                    ),
                    evidence=evidence,
                )
            )

        matched_count = sum(item.matched for item in skill_matches)

        summary = (
            f"Overall job-description-to-answer similarity "
            f"{overall_similarity:.2f} via {provider}. "
            f"{matched_count}/{len(skill_matches)} required skills have "
            f"matching textual or semantic evidence. Matches do not establish "
            f"proficiency without supporting examples or assessment."
        )

        return SemanticSkillMatchResponse(
            correlation_id=payload.correlation_id,
            interview_id=payload.interview_id,
            role_title=payload.role_title,
            status=JobStatus.SUCCEEDED,
            stub=stub,
            message=f"Semantic skill match via {provider}",
            overall_similarity=round(overall_similarity, 3),
            embedding_provider=provider,
            skill_matches=skill_matches,
            competency_matches=competency_matches,
            summary=summary,
        )
