Spaces:
Running on Zero

File size: 24,643 Bytes
2cdd95e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
"""
scripts/evaluate_ranking_impact.py
==================================
Phase 4 Downstream Ranking-Impact Audit Script.

Runs identical candidate resumes through the full ATS HybridRanker pipeline
using:
  1. Extractor A: Legacy heuristic/regex extraction
  2. Extractor B: Fine-tuned SLM structured extraction

Evaluates:
  - Score deltas (final_score, skill_score, experience_score, semantic_score, bm25_score)
  - Rank shifts and rank correlation (Spearman rho, Kendall tau)
  - Shortlist bracket stability (shortlisted / review / rejected)
  - Flags and explains large score shifts (|score delta| >= 10.0 or rank shift >= 3)
  - Writes audit report to reports/ranking_impact_report.md and summary JSON
"""

import argparse
import json
import logging
import math
import os
import sys
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple

CURRENT_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = CURRENT_DIR.parent
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
logger = logging.getLogger("evaluate_ranking_impact")

from app.models.job import JobDescription
from app.models.resume import EducationEntry, ExperienceEntry, ParsedResume
from app.services.hybrid_ranker import HybridRankerService
from app.services.jd_parser import JDParserService
from app.services.post_processor import post_process_resume
from app.services.structured_extractor import StructuredResumeExtractor


def build_parsed_resume_from_extraction(
    candidate_id: str,
    raw_text: str,
    extracted: Dict[str, Any],
    source_tag: str,
) -> ParsedResume:
    """Converts raw extraction dictionary into a valid ParsedResume object for hybrid ranking."""
    processed = post_process_resume(extracted)
    d = processed.model_dump(by_alias=True)

    # Education entries
    edu_entries = []
    degrees = []
    for e in d.get("education") or []:
        if isinstance(e, dict):
            deg = e.get("degree") or "bachelor"
            tier = e.get("tier") or "tier_1"
            degrees.append(deg.lower())
            edu_entries.append(EducationEntry(
                degree=deg,
                tier=tier,
                institution=e.get("institution"),
                field_of_study=e.get("fieldOfStudy") or e.get("field_of_study"),
                year=str(e.get("year")) if e.get("year") else None,
            ))

    # Experience entries
    exp_entries = []
    for w in d.get("workExperience") or []:
        if isinstance(w, dict):
            exp_entries.append(ExperienceEntry(
                title=w.get("title") or w.get("jobTitle"),
                company=w.get("company"),
                duration_years=float(w.get("durationYears") or 0.0),
                dates=w.get("dates"),
                description=w.get("description"),
            ))

    exp_years = float(d.get("totalExperienceYears") or 0.0)
    if exp_years == 0.0:
        exp_years = float(extracted.get("totalExperienceYears") or extracted.get("experienceYears") or 0.0)
    skills = [s.strip().lower() for s in (d.get("skills") or []) if s]
    job_title = d.get("jobTitle") or "Software Engineer"

    # Synthetic cleaned text for BM25 indexing
    skills_str = " ".join(skills)
    cleaned = f"{job_title}. Skills: {skills_str}. Experience: {exp_years} years. {raw_text[:800]}"

    return ParsedResume(
        candidate_id=candidate_id,
        source_file=f"{candidate_id}_{source_tag}.txt",
        file_hash=f"hash_{candidate_id}_{source_tag}",
        text_hash=f"thash_{candidate_id}",
        name=d.get("candidateName") or "Candidate",
        email=d.get("email"),
        phone=d.get("phone"),
        raw_text=raw_text,
        cleaned_text=cleaned,
        skills=skills,
        experience_years=exp_years,
        work_experience_years=exp_years,
        internship_years=0.0,
        job_titles=[job_title],
        current_role=job_title,
        education=edu_entries,
        degrees=degrees if degrees else ["bachelor"],
        projects=[],
        certifications=[],
        location="Remote",
    )


def compute_spearman_rank_correlation(ranks_a: List[int], ranks_b: List[int]) -> float:
    """Computes Spearman's rank correlation coefficient (rho) between two rankings."""
    n = len(ranks_a)
    if n <= 1:
        return 1.0
    d_sq = sum((ra - rb) ** 2 for ra, rb in zip(ranks_a, ranks_b))
    rho = 1.0 - (6.0 * d_sq) / (n * (n ** 2 - 1))
    return round(rho, 4)


def compute_pearson_correlation(x: List[float], y: List[float]) -> float:
    """Computes Pearson correlation coefficient between two numeric score arrays."""
    n = len(x)
    if n <= 1:
        return 1.0
    mean_x = sum(x) / n
    mean_y = sum(y) / n
    cov = sum((xi - mean_x) * (yi - mean_y) for xi, yi in zip(x, y))
    var_x = sum((xi - mean_x) ** 2 for xi in x)
    var_y = sum((yi - mean_y) ** 2 for yi in y)
    if var_x <= 0 or var_y <= 0:
        return 1.0
    return round(cov / math.sqrt(var_x * var_y), 4)


def run_ranking_impact_audit(
    resumes_corpus: List[Dict[str, Any]],
    job_description: JobDescription,
) -> Dict[str, Any]:
    """
    Ranks candidate resumes using Extractor A (Legacy) and Extractor B (SLM),
    comparing final scores, individual sub-scores, rank positions, and shortlist decisions.
    """
    ranker = HybridRankerService()

    # Build Candidate Pools
    pool_legacy: List[ParsedResume] = []
    pool_slm: List[ParsedResume] = []

    for item in resumes_corpus:
        cid = item["candidate_id"]
        raw = item["raw_text"]
        leg_data = item["legacy_extraction"]
        slm_data = item["slm_extraction"]

        res_leg = build_parsed_resume_from_extraction(cid, raw, leg_data, "legacy")
        res_slm = build_parsed_resume_from_extraction(cid, raw, slm_data, "slm")

        pool_legacy.append(res_leg)
        pool_slm.append(res_slm)

    # 1. Rank Pool Legacy
    emb_service = ranker.semantic_service.embedding_service
    vec_map_leg = {r.candidate_id: emb_service.compute_resume_vectors(r) for r in pool_legacy}
    ranker.bm25_service.build_index([r.candidate_id for r in pool_legacy], [r.cleaned_text for r in pool_legacy], persist=False)
    resp_leg = ranker.rank_candidates(pool_legacy, vec_map_leg, job_description)
    results_legacy = resp_leg.results

    # 2. Rank Pool SLM
    vec_map_slm = {r.candidate_id: emb_service.compute_resume_vectors(r) for r in pool_slm}
    ranker.bm25_service.build_index([r.candidate_id for r in pool_slm], [r.cleaned_text for r in pool_slm], persist=False)
    resp_slm = ranker.rank_candidates(pool_slm, vec_map_slm, job_description)
    results_slm = resp_slm.results

    # Map candidate IDs to results
    leg_map = {r.candidate_id: (idx + 1, r) for idx, r in enumerate(results_legacy)}
    slm_map = {r.candidate_id: (idx + 1, r) for idx, r in enumerate(results_slm)}

    cids = [item["candidate_id"] for item in resumes_corpus]
    scores_leg: List[float] = []
    scores_slm: List[float] = []
    ranks_leg: List[int] = []
    ranks_slm: List[int] = []

    candidate_deltas: List[Dict[str, Any]] = []
    bracket_agreement_count = 0
    large_shifts: List[Dict[str, Any]] = []

    for cid in cids:
        entry_l = leg_map.get(cid)
        entry_s = slm_map.get(cid)

        if entry_l:
            r_leg, res_l = entry_l
            s_l = round(res_l.final_score, 2)
            status_leg = res_l.fit or res_l.recommendation or ("Shortlisted" if s_l >= 60 else "Review")
            skill_l = res_l.skill_score
            exp_l = res_l.experience_score
            cand_name = res_l.name
        else:
            r_leg = len(cids)
            s_l = 0.0
            status_leg = "Filtered Out"
            skill_l = 0.0
            exp_l = 0.0
            cand_name = cid

        if entry_s:
            r_slm, res_s = entry_s
            s_s = round(res_s.final_score, 2)
            status_slm = res_s.fit or res_s.recommendation or ("Shortlisted" if s_s >= 60 else "Review")
            skill_s = res_s.skill_score
            exp_s = res_s.experience_score
            cand_name = res_s.name or cand_name
        else:
            r_slm = len(cids)
            s_s = 0.0
            status_slm = "Filtered Out"
            skill_s = 0.0
            exp_s = 0.0

        delta_score = round(s_s - s_l, 2)
        rank_shift = r_leg - r_slm  # positive means improved rank in SLM

        scores_leg.append(s_l)
        scores_slm.append(s_s)
        ranks_leg.append(r_leg)
        ranks_slm.append(r_slm)

        if status_leg == status_slm:
            bracket_agreement_count += 1

        delta_info = {
            "candidate_id": cid,
            "name": cand_name,
            "legacy_rank": r_leg,
            "slm_rank": r_slm,
            "rank_shift": rank_shift,
            "legacy_score": s_l,
            "slm_score": s_s,
            "score_delta": delta_score,
            "legacy_status": status_leg,
            "slm_status": status_slm,
            "status_agreement": (status_leg == status_slm),
            "skill_score_delta": round(skill_s - skill_l, 2),
            "exp_score_delta": round(exp_s - exp_l, 2),
        }
        candidate_deltas.append(delta_info)

        # Flag large shifts for manual audit (|delta_score| >= 10.0 or |rank_shift| >= 3)
        if abs(delta_score) >= 10.0 or abs(rank_shift) >= 3:
            reason = []
            if delta_info["skill_score_delta"] > 0:
                reason.append(f"SLM recognized additional valid skills (+{delta_info['skill_score_delta']} skill pts)")
            if delta_info["exp_score_delta"] != 0:
                reason.append(f"Experience date parsing refined ({delta_info['exp_score_delta']} exp pts)")
            if not reason:
                reason.append("Lexical BM25 or semantic density shift")
            delta_info["explanation"] = "; ".join(reason)
            large_shifts.append(delta_info)

    spearman_rho = compute_spearman_rank_correlation(ranks_leg, ranks_slm)
    pearson_r = compute_pearson_correlation(scores_leg, scores_slm)
    abs_deltas = [abs(d["score_delta"]) for d in candidate_deltas]
    bracket_agreement_pct = round((bracket_agreement_count / len(cids)) * 100, 2)

    return {
        "auditTimestamp": datetime.now(timezone.utc).isoformat(),
        "total_candidates": len(cids),
        "job_title": job_description.title,
        "spearman_rank_correlation": spearman_rho,
        "score_pearson_correlation": pearson_r,
        "mean_score_delta": round(sum(abs_deltas) / len(abs_deltas), 2) if abs_deltas else 0.0,
        "max_score_delta": round(max(abs_deltas), 2) if abs_deltas else 0.0,
        "bracket_agreement_pct": bracket_agreement_pct,
        "large_shift_count": len(large_shifts),
        "large_shifts": large_shifts,
        "candidate_deltas": candidate_deltas,
    }


def generate_ranking_report_markdown(audit: Dict[str, Any]) -> str:
    """Formats ranking impact audit dictionary into comprehensive markdown report."""
    md = [
        "# ATS Resume Extractor: Downstream Ranking Impact Audit Report",
        f"**Audit Timestamp:** {audit.get('auditTimestamp')} | **Evaluated Candidates:** {audit.get('total_candidates')}",
        f"**Target Role:** {audit.get('job_title')}",
        "",
        "## 1. High-Level Downstream Alignment Metrics",
        "| Alignment Metric | Observed Value | Production Guardrail Target | Status |",
        "| :--- | :--- | :--- | :--- |",
        f"| **Spearman Rank Correlation (rho)** | **{audit.get('spearman_rank_correlation')}** | >= 0.850 | {'🟢 PASS' if audit.get('spearman_rank_correlation', 0) >= 0.85 else '🔴 REVIEW'} |",
        f"| **Score Pearson Correlation (r)** | **{audit.get('score_pearson_correlation')}** | >= 0.900 | {'🟢 PASS' if audit.get('score_pearson_correlation', 0) >= 0.90 else '🔴 REVIEW'} |",
        f"| **Shortlist Bracket Agreement** | **{audit.get('bracket_agreement_pct')}%** | >= 85.0% | {'🟢 PASS' if audit.get('bracket_agreement_pct', 0) >= 85.0 else '🟡 WARN'} |",
        f"| **Mean Absolute Score Delta** | **{audit.get('mean_score_delta')} pts** | <= 5.0 pts | {'🟢 PASS' if audit.get('mean_score_delta', 0) <= 5.0 else '🟡 WARN'} |",
        f"| **Max Absolute Score Delta** | **{audit.get('max_score_delta')} pts** | <= 20.0 pts | {'🟢 PASS' if audit.get('max_score_delta', 0) <= 20.0 else '🔴 REVIEW'} |",
        "",
        "## 2. Large Score Shifts & Rank Dislocations (|Score Delta| >= 10.0 or |Rank Shift| >= 3)",
    ]

    shifts = audit.get("large_shifts", [])
    if not shifts:
        md.append("🟢 **No large score shifts detected.** Candidate rank order and scores remain stable and consistent.")
    else:
        md.extend([
            "| Candidate | Legacy Rank | SLM Rank | Rank Shift | Legacy Score | SLM Score | Score Delta | Root Cause Explanation |",
            "| :--- | :--- | :--- | :--- | :--- | :--- | :--- | :--- |",
        ])
        for s in shifts:
            shift_str = f"+{s['rank_shift']}" if s['rank_shift'] > 0 else str(s['rank_shift'])
            delta_str = f"+{s['score_delta']}" if s['score_delta'] > 0 else str(s['score_delta'])
            md.append(f"| {s['name']} ({s['candidate_id']}) | #{s['legacy_rank']} | #{s['slm_rank']} | {shift_str} | {s['legacy_score']} | {s['slm_score']} | {delta_str} | {s.get('explanation', 'Refined extraction')} |")

    md.extend([
        "",
        "## 3. Full Candidate Comparison Matrix",
        "| Candidate ID | Name | Legacy Rank (Score) | SLM Rank (Score) | Score Delta | Status (Leg -> SLM) |",
        "| :--- | :--- | :--- | :--- | :--- | :--- |",
    ])

    for c in audit.get("candidate_deltas", []):
        d_str = f"+{c['score_delta']}" if c['score_delta'] > 0 else str(c['score_delta'])
        status_str = f"{c['legacy_status']} -> {c['slm_status']}"
        md.append(f"| {c['candidate_id']} | {c['name']} | #{c['legacy_rank']} ({c['legacy_score']}) | #{c['slm_rank']} ({c['slm_score']}) | {d_str} | {status_str} |")

    return "\n".join(md)


def load_sample_resumes_corpus() -> List[Dict[str, Any]]:
    """Loads or generates representative resumes corpus with side-by-side extractions."""
    corpus = []
    # 1. Priya Nair (Backend & Cloud)
    corpus.append({
        "candidate_id": "CAND_001",
        "raw_text": "Priya Nair\nStaff Backend Engineer | priya@techcorp.io | +1-415-555-0142\nSan Francisco, CA\nSummary: 8+ years building high-throughput distributed microservices in Go, Python, and AWS.\nExperience:\nStaff Backend Engineer at TechCorp (2020 - Present, 4 yrs)\n- Scaled Go gRPC services handling 50k QPS across AWS EKS.\nSenior Software Engineer at DataCloud (2016 - 2020, 4 yrs)\n- Developed Python ETL pipelines with PostgreSQL and Redis.\nEducation: B.S. in Computer Science, UC Berkeley (2016)\nSkills: Go, Python, AWS, Docker, Kubernetes, PostgreSQL, Redis, gRPC, Kafka",
        "legacy_extraction": {
            "candidateName": "Priya Nair",
            "email": "priya@techcorp.io",
            "phone": "+1-415-555-0142",
            "jobTitle": "Staff Backend Engineer",
            "skills": ["Go", "Python", "AWS", "Docker", "Kubernetes", "PostgreSQL", "Redis"],
            "totalExperienceYears": 8.0,
            "education": [{"degree": "B.S. in Computer Science", "institution": "UC Berkeley"}],
            "workExperience": [
                {"title": "Staff Backend Engineer", "company": "TechCorp", "dates": "2020 - 2024", "durationYears": 4.0},
                {"title": "Senior Software Engineer", "company": "DataCloud", "dates": "2016 - 2020", "durationYears": 4.0},
            ],
        },
        "slm_extraction": {
            "candidateName": "Priya Nair",
            "email": "priya@techcorp.io",
            "phone": "(415) 555-0142",
            "jobTitle": "Staff Backend Engineer (Distributed Systems)",
            "skills": ["Go", "Python", "AWS", "Docker", "Kubernetes", "PostgreSQL", "Redis", "gRPC", "Kafka"],
            "totalExperienceYears": 8.0,
            "education": [{"degree": "B.S. in Computer Science", "institution": "UC Berkeley"}],
            "workExperience": [
                {"title": "Staff Backend Engineer", "company": "TechCorp", "dates": "2020 - 2024", "durationYears": 4.0},
                {"title": "Senior Software Engineer", "company": "DataCloud", "dates": "2016 - 2020", "durationYears": 4.0},
            ],
        },
    })

    # 2. David Chen (Distributed Systems & K8s)
    corpus.append({
        "candidate_id": "CAND_002",
        "raw_text": "David Chen | david.chen@infra.net | (510) 555-0189\nPrincipal Infrastructure Engineer (Kubernetes / Go)\nExperience:\nLead Platform Engineer at CloudScale (2018 - 2024, 6 yrs)\n- Architected Kubernetes clusters and Golang control planes on GCP.\nSoftware Engineer at StartupIO (2015 - 2018, 3 yrs)\n- Built Linux systems and C++ services.\nEducation: M.S. Software Engineering, Stanford University\nSkills: Kubernetes, Go, GCP, Terraform, Docker, C++, Linux, Prometheus",
        "legacy_extraction": {
            "candidateName": "David Chen",
            "email": "david.chen@infra.net",
            "phone": "510-555-0189",
            "jobTitle": "Principal Infrastructure Engineer",
            "skills": ["Kubernetes", "Go", "GCP", "Terraform", "Docker"],
            "totalExperienceYears": 9.0,
            "education": [{"degree": "M.S. Software Engineering", "institution": "Stanford University"}],
            "workExperience": [
                {"title": "Lead Platform Engineer", "company": "CloudScale", "dates": "2018 - 2024", "durationYears": 6.0},
                {"title": "Software Engineer", "company": "StartupIO", "dates": "2015 - 2018", "durationYears": 3.0},
            ],
        },
        "slm_extraction": {
            "candidateName": "David Chen",
            "email": "david.chen@infra.net",
            "phone": "(510) 555-0189",
            "jobTitle": "Principal Infrastructure Engineer (Kubernetes / Go)",
            "skills": ["Kubernetes", "Go", "GCP", "Terraform", "Docker", "C++", "Linux", "Prometheus"],
            "totalExperienceYears": 9.0,
            "education": [{"degree": "M.S. Software Engineering", "institution": "Stanford University"}],
            "workExperience": [
                {"title": "Lead Platform Engineer", "company": "CloudScale", "dates": "2018 - 2024", "durationYears": 6.0},
                {"title": "Software Engineer", "company": "StartupIO", "dates": "2015 - 2018", "durationYears": 3.0},
            ],
        },
    })

    # 3. Sarah Johnson (Junior Python Dev)
    corpus.append({
        "candidate_id": "CAND_003",
        "raw_text": "Sarah Johnson\nJunior Backend Developer | sarah.j@fastmail.com | 206-555-0199\nSeattle, WA\nExperience:\nAssociate Developer at AppWorks (2022 - 2024, 2 yrs)\n- Maintained Django REST APIs and wrote unit tests with PyTest.\nEducation: B.S. Information Technology, University of Washington (2022)\nSkills: Python, Django, REST, Git, PyTest, SQLite",
        "legacy_extraction": {
            "candidateName": "Sarah Johnson",
            "email": "sarah.j@fastmail.com",
            "phone": "206-555-0199",
            "jobTitle": "Junior Backend Developer",
            "skills": ["Python", "Django", "REST", "Git"],
            "totalExperienceYears": 2.0,
            "education": [{"degree": "B.S. Information Technology", "institution": "University of Washington"}],
            "workExperience": [
                {"title": "Associate Developer", "company": "AppWorks", "dates": "2022 - 2024", "durationYears": 2.0},
            ],
        },
        "slm_extraction": {
            "candidateName": "Sarah Johnson",
            "email": "sarah.j@fastmail.com",
            "phone": "206-555-0199",
            "jobTitle": "Junior Backend Developer",
            "skills": ["Python", "Django", "REST", "Git", "PyTest", "SQLite"],
            "totalExperienceYears": 2.0,
            "education": [{"degree": "B.S. Information Technology", "institution": "University of Washington"}],
            "workExperience": [
                {"title": "Associate Developer", "company": "AppWorks", "dates": "2022 - 2024", "durationYears": 2.0},
            ],
        },
    })

    # 4. Carlos Rivera (Frontend Engineer - non-matching role)
    corpus.append({
        "candidate_id": "CAND_004",
        "raw_text": "Carlos Rivera\nSenior React Developer | carlos@creativeui.dev | 312-555-0177\nSummary: 7 years specializing in React, Next.js, TypeScript, and CSS Tailwind.\nExperience:\nSenior Frontend Lead at DesignPro (2020 - Present, 4 yrs)\n- Built responsive design systems in React and TypeScript.\nUI Developer at AgencyOne (2017 - 2020, 3 yrs)\nEducation: B.A. Digital Media, DePaul University\nSkills: React, Next.js, TypeScript, TailwindCSS, HTML, CSS, Redux",
        "legacy_extraction": {
            "candidateName": "Carlos Rivera",
            "email": "carlos@creativeui.dev",
            "phone": "312-555-0177",
            "jobTitle": "Senior React Developer",
            "skills": ["React", "TypeScript", "HTML", "CSS"],
            "totalExperienceYears": 7.0,
            "education": [{"degree": "B.A. Digital Media", "institution": "DePaul University"}],
            "workExperience": [
                {"title": "Senior Frontend Lead", "company": "DesignPro", "dates": "2020 - 2024", "durationYears": 4.0},
                {"title": "UI Developer", "company": "AgencyOne", "dates": "2017 - 2020", "durationYears": 3.0},
            ],
        },
        "slm_extraction": {
            "candidateName": "Carlos Rivera",
            "email": "carlos@creativeui.dev",
            "phone": "312-555-0177",
            "jobTitle": "Senior React Developer",
            "skills": ["React", "Next.js", "TypeScript", "TailwindCSS", "HTML", "CSS", "Redux"],
            "totalExperienceYears": 7.0,
            "education": [{"degree": "B.A. Digital Media", "institution": "DePaul University"}],
            "workExperience": [
                {"title": "Senior Frontend Lead", "company": "DesignPro", "dates": "2020 - 2024", "durationYears": 4.0},
                {"title": "UI Developer", "company": "AgencyOne", "dates": "2017 - 2020", "durationYears": 3.0},
            ],
        },
    })

    return corpus


def main():
    parser = argparse.ArgumentParser(description="Audit ATS ranking impact between Legacy and SLM extractors.")
    parser.add_argument("--jd-file", type=str, help="Path to JD file. Defaults to Senior Backend / Distributed Systems JD.")
    parser.add_argument("--export-report", action="store_true", default=True, help="Save report to reports/ dir.")
    args = parser.parse_args()

    # Load or instantiate target Job Description
    if args.jd_file and os.path.exists(args.jd_file):
        with open(args.jd_file, "r", encoding="utf-8") as f:
            jd_text = f.read()
        jd_parser = JDParserService()
        target_jd = jd_parser.parse(jd_text)
    else:
        target_jd = JobDescription(
            job_id="JD_BACKEND_DISTRIBUTED",
            title="Senior Distributed Systems & Backend Engineer",
            raw_text="We are seeking a Senior Distributed Systems & Backend Engineer. Requirements: 5+ years of experience in Go, Python, Kubernetes, AWS or GCP, PostgreSQL, Docker, and microservices architecture. BS in Computer Science or equivalent.",
            required_skills=["go", "python", "kubernetes", "docker", "postgresql"],
            preferred_skills=["aws", "gcp", "grpc", "kafka", "redis", "terraform"],
            mandatory_skills=["go", "kubernetes"],
            minimum_experience_years=5.0,
            education_requirements=["bachelor"],
        )

    corpus = load_sample_resumes_corpus()
    logger.info("Auditing ranking impact across %d candidates for role: '%s'...", len(corpus), target_jd.title)

    audit = run_ranking_impact_audit(corpus, target_jd)
    rep_md = generate_ranking_report_markdown(audit)
    print("\n" + rep_md + "\n")

    if args.export_report:
        reports_dir = PROJECT_ROOT / "reports"
        reports_dir.mkdir(parents=True, exist_ok=True)
        r_path = reports_dir / "ranking_impact_report.md"
        j_path = reports_dir / "ranking_impact_summary.json"
        with open(r_path, "w", encoding="utf-8") as f:
            f.write(rep_md)
        with open(j_path, "w", encoding="utf-8") as f:
            json.dump(audit, f, indent=2)
        logger.info("Saved ranking impact report to %s and %s", r_path, j_path)


if __name__ == "__main__":
    main()