from difflib import SequenceMatcher

# def extract_resume_text(candidate_profile: dict) -> str:
#     """
#     Flatten the resume dictionary into a single string for text-based comparison.
#     Gracefully handles missing or malformed data.
#     """
#     if not isinstance(candidate_profile, dict):
#         return ""

#     text_parts = []

#     # Summary
#     summary = candidate_profile.get('summary')
#     if isinstance(summary, str):
#         text_parts.append(summary)

#     # Experience
#     experience = candidate_profile.get('experience')
#     if isinstance(experience, list):
#         for exp in experience:
#             if isinstance(exp, dict):
#                 text_parts.append(str(exp.get('headline', '')))
#                 text_parts.append(str(exp.get('company', '')))
#                 text_parts.append(str(exp.get('employment type', '')))
#                 text_parts.append(str(exp.get('duration', '')))
#                 responsibilities = exp.get('responsibilities', [])
#                 if isinstance(responsibilities, list):
#                     text_parts.extend(str(r) for r in responsibilities if r)

#     # Projects
#     projects = candidate_profile.get('projects')
#     if isinstance(projects, list):
#         for proj in projects:
#             if isinstance(proj, dict):
#                 text_parts.append(str(proj.get('title', '')))
#                 text_parts.append(str(proj.get('description', '')))
#                 technologies = proj.get('technologies', [])
#                 if isinstance(technologies, list):
#                     text_parts.extend(str(t) for t in technologies if t)

#     # Skills
#     skills = candidate_profile.get('skills')
#     if isinstance(skills, list):
#         text_parts.extend(str(skill) for skill in skills if skill)

#     # Certifications
#     certifications = candidate_profile.get('certifications')
#     if isinstance(certifications, list):
#         for cert in certifications:
#             if isinstance(cert, dict):
#                 text_parts.append(str(cert.get('name', '')))
#                 text_parts.append(str(cert.get('issuer', '')))

#     # Education
#     education = candidate_profile.get('education')
#     if isinstance(education, list):
#         for edu in education:
#             if isinstance(edu, dict):
#                 text_parts.append(str(edu.get('institution', '')))
#                 text_parts.append(str(edu.get('degree', '')))
#             elif isinstance(edu, str):
#                 text_parts.append(edu)  # For backward compatibility with string-based entries

#     return ' '.join(part for part in text_parts if part)
# def extract_resume_text(candidate_profile: dict) -> str:
#     """
#     Flatten the resume dictionary into a single string for text-based comparison.
#     Only includes values (not keys) and gracefully handles missing or malformed data.
#     """
#     if not isinstance(candidate_profile, dict):
#         return ""

#     text_parts = []

#     # Summary
#     summary = candidate_profile.get('summary')
#     if isinstance(summary, str):
#         text_parts.append(summary)

#     # Experience
#     experience = candidate_profile.get('experience')
#     if isinstance(experience, list):
#         for exp in experience:
#             if isinstance(exp, dict):
#                 if exp.get('headline'):
#                     text_parts.append(str(exp.get('headline')))
#                 if exp.get('company'):
#                     text_parts.append(str(exp.get('company')))
#                 if exp.get('employment type'):
#                     text_parts.append(str(exp.get('employment type')))
#                 if exp.get('duration'):
#                     text_parts.append(str(exp.get('duration')))
#                 if exp.get('location'):
#                     text_parts.append(str(exp.get('location')))
#                 responsibilities = exp.get('responsibilities', [])
#                 if isinstance(responsibilities, list):
#                     text_parts.extend(str(r) for r in responsibilities if r)

#     # Projects
#     projects = candidate_profile.get('projects')
#     if isinstance(projects, list):
#         for proj in projects:
#             if isinstance(proj, dict):
#                 if proj.get('title'):
#                     text_parts.append(str(proj.get('title')))
#                 if proj.get('description'):
#                     text_parts.append(str(proj.get('description')))
#                 technologies = proj.get('technologies', [])
#                 if isinstance(technologies, list):
#                     text_parts.extend(str(t) for t in technologies if t)

#     # Skills
#     skills = candidate_profile.get('skills')
#     if isinstance(skills, list):
#         text_parts.extend(str(skill) for skill in skills if skill)

#     # Certifications
#     certifications = candidate_profile.get('certifications')
#     if isinstance(certifications, list):
#         for cert in certifications:
#             if isinstance(cert, dict):
#                 if cert.get('name'):
#                     text_parts.append(str(cert.get('name')))
#                 if cert.get('issuer'):
#                     text_parts.append(str(cert.get('issuer')))

#     # Education
#     education = candidate_profile.get('education')
#     if isinstance(education, list):
#         for edu in education:
#             if isinstance(edu, dict):
#                 if edu.get('institution'):
#                     text_parts.append(str(edu.get('institution')))
#                 if edu.get('degree'):
#                     text_parts.append(str(edu.get('degree')))
#             elif isinstance(edu, str):
#                 text_parts.append(edu)  # For backward compatibility with string-based entries

#     # Filter out empty strings and join with a space
#     return ' '.join(part for part in text_parts if part)

def extract_resume_text(candidate_profile: dict) -> str:
    """
    Flatten the resume dictionary into a single string for text-based comparison.
    Now includes 'additional_security_analysis' as part of the text corpus.
    """
    if not isinstance(candidate_profile, dict):
        return ""

    text_parts = []

    # Summary
    summary = candidate_profile.get('summary')
    if isinstance(summary, str):
        text_parts.append(summary)

    # Experience
    experience = candidate_profile.get('experience')
    if isinstance(experience, list):
        for exp in experience:
            if isinstance(exp, dict):
                for key in ['headline', 'company', 'employment type', 'duration', 'location']:
                    val = exp.get(key)
                    if val:
                        text_parts.append(str(val))
                responsibilities = exp.get('responsibilities', [])
                if isinstance(responsibilities, list):
                    text_parts.extend(str(r) for r in responsibilities if r)

    # Projects
    projects = candidate_profile.get('projects')
    if isinstance(projects, list):
        for proj in projects:
            if isinstance(proj, dict):
                for key in ['title', 'description']:
                    val = proj.get(key)
                    if val:
                        text_parts.append(str(val))
                technologies = proj.get('technologies', [])
                if isinstance(technologies, list):
                    text_parts.extend(str(t) for t in technologies if t)

    # Skills
    skills = candidate_profile.get('skills')
    if isinstance(skills, list):
        text_parts.extend(str(skill) for skill in skills if skill)

    # Certifications
    certifications = candidate_profile.get('certifications')
    if isinstance(certifications, list):
        for cert in certifications:
            if isinstance(cert, dict):
                for key in ['name', 'issuer']:
                    val = cert.get(key)
                    if val:
                        text_parts.append(str(val))

    # Education
    education = candidate_profile.get('education')
    if isinstance(education, list):
        for edu in education:
            if isinstance(edu, dict):
                for key in ['institution', 'degree']:
                    val = edu.get(key)
                    if val:
                        text_parts.append(str(val))
            elif isinstance(edu, str):
                text_parts.append(edu)

    # Additional Security Analysis
    sec_analysis = candidate_profile.get('additional_security_analysis', {})
    if isinstance(sec_analysis, dict):
        for key in ['nightShiftExperience', 'readyToJoin', 'visaStatus', 'nationality']:
            val = sec_analysis.get(key)
            if val:
                text_parts.append(str(val))

        industries = sec_analysis.get('industriesWorkedIn', [])
        if isinstance(industries, list):
            text_parts.extend(str(ind) for ind in industries if ind)

        tech_skills = sec_analysis.get('technicalAndDigitalSkills', {})
        if isinstance(tech_skills, dict):
            for skill_val in tech_skills.values():
                if skill_val:
                    text_parts.append(str(skill_val))

    return ' '.join(part for part in text_parts if part)

def compare_resume_with_jd_text(jd_text: str, candidate_profile: dict) -> dict:
    """
    Compares JD text with a candidate profile (as dict) using raw text similarity.
    Returns token overlap and fuzzy similarity.
    """
    try:
        resume_text = extract_resume_text(candidate_profile)
        print("resume_text",resume_text)
        # breakpoint()
        base_tokens = set(jd_text.lower().split())
        print("base_tokens",base_tokens)
        resume_tokens = set(resume_text.lower().split())
        print("resume_tokens",resume_tokens)
        # breakpoint()

        matched_tokens = base_tokens.intersection(resume_tokens)
        print("matched_tokens",matched_tokens)
        token_score = len(matched_tokens) / len(base_tokens) * 100 if base_tokens else 0

        fuzzy_score = SequenceMatcher(None, jd_text.lower(), resume_text.lower()).ratio() * 100
        print("fuzzy_score",fuzzy_score)
        return {
            "token_overlap_score": round(token_score, 2),
            "fuzzy_similarity_score": round(fuzzy_score, 2)
        }

    except Exception as e:
        print("Error during comparison:", e)
        return {}
