"""Offline comparison for the six public Repair Planner teaching records."""
import argparse
import json
from pathlib import Path

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("technicians", type=Path)
parser.add_argument("output", type=Path, help="unused report filename")
parser.add_argument("required", nargs="*", help="required skill identifiers")
args = parser.parse_args()

required = sorted({s.strip().lower() for s in args.required if s.strip()})
rows = json.loads(args.technicians.read_text(encoding="utf-8"))
candidates = []
for row in rows:
    if row["available"] is not True:
        continue
    skills = {s.lower() for s in row["skills"]}
    matched = [s for s in required if s in skills]
    missing = [s for s in required if s not in skills]
    if required and not matched:
        continue
    candidates.append({"id": row["id"], "matched": matched, "missing": missing})

# Stable ID order for review. This exercise compares membership, not dispatch order.
candidates.sort(key=lambda person: person["id"])
eligible = [p["id"] for p in candidates if not p["missing"]] if required else None
report = {"required": required, "candidates": candidates, "singlePersonAll": eligible}
with args.output.open("x", encoding="utf-8") as file:
    json.dump(report, file, ensure_ascii=False, indent=2)
    file.write("\n")
print(f"{len(candidates)} candidate(s); singlePersonAll={eligible}; {args.output}")
