"""
ASKUREX JOB HUB — AI Resume Review (starter module)
----------------------------------------------------
Offline keyword/heuristic scorer for local development and testing.
Swap `score_resume()` internals for a real LLM call (e.g. the Anthropic
Messages API) once you're ready to wire in true "AI Resume Review",
"AI Skill Gap Analysis", and "AI Career Suggestion" features.

Run directly for a demo:
    python3 ai_resume_review.py
"""

import re
from dataclasses import dataclass, field


@dataclass
class ResumeReviewResult:
    match_score: float                 # 0-100
    matched_keywords: list = field(default_factory=list)
    missing_keywords: list = field(default_factory=list)
    suggestions: list = field(default_factory=list)


STOPWORDS = {"the", "and", "a", "to", "of", "in", "for", "with", "on", "is", "are"}


def _extract_keywords(text: str) -> set:
    words = re.findall(r"[a-zA-Z][a-zA-Z+#.]{1,}", text.lower())
    return {w for w in words if w not in STOPWORDS and len(w) > 2}


def score_resume(resume_text: str, job_description: str) -> ResumeReviewResult:
    resume_kw = _extract_keywords(resume_text)
    job_kw = _extract_keywords(job_description)

    if not job_kw:
        return ResumeReviewResult(match_score=0.0)

    matched = sorted(resume_kw & job_kw)
    missing = sorted(job_kw - resume_kw)

    score = round(100 * len(matched) / len(job_kw), 1)

    suggestions = []
    if missing:
        top_missing = missing[:8]
        suggestions.append(
            "Add or highlight these job-relevant skills/terms if you genuinely "
            "have them: " + ", ".join(top_missing)
        )
    if len(resume_text.split()) < 150:
        suggestions.append("Your resume looks short — add more detail on responsibilities and measurable results.")
    if not re.search(r"\d", resume_text):
        suggestions.append("Add quantifiable achievements (numbers, %, taka amounts, team size) to strengthen impact.")

    return ResumeReviewResult(
        match_score=score,
        matched_keywords=matched,
        missing_keywords=missing,
        suggestions=suggestions,
    )


def suggest_careers(skills: list[str]) -> list[str]:
    """Very simple rule-based career suggestion — replace with an ML/LLM model later."""
    catalog = {
        "python": ["Software Engineer", "Data Analyst", "Backend Developer"],
        "excel": ["Data Entry Specialist", "Accounts Officer", "Business Analyst"],
        "communication": ["Customer Support", "HR Executive", "Sales Executive"],
        "javascript": ["Frontend Developer", "Full Stack Developer"],
        "accounting": ["Accountant", "Finance Officer", "Auditor"],
        "teaching": ["Trainer", "Content Developer", "Academic Coordinator"],
    }
    suggestions = []
    for skill in skills:
        suggestions.extend(catalog.get(skill.lower(), []))
    return sorted(set(suggestions)) or ["Add more skills to your profile for tailored suggestions."]


if __name__ == "__main__":
    demo_resume = """
    Md. Rahim Uddin. 3 years experience as a Python developer.
    Worked with Django, MySQL and REST APIs. Improved API response time by 40%.
    Strong communication and team collaboration skills.
    """
    demo_job = """
    We are hiring a Python Backend Developer with experience in Django,
    REST APIs, MySQL, and cloud deployment (AWS). Good communication required.
    """
    result = score_resume(demo_resume, demo_job)
    print(f"Match score: {result.match_score}%")
    print("Matched:", result.matched_keywords)
    print("Missing:", result.missing_keywords)
    print("Suggestions:")
    for s in result.suggestions:
        print(" -", s)
