import json
import os
from pathlib import Path

root = Path(r"c:/Projects/FreeExile")
with open(root / r".agents/teamwork/spec_miner_techart_3/drq_summary.json", "r", encoding="utf-8") as f:
    drqs = json.load(f)

print(f"Checking deliverables across {len(drqs)} DRQs...")

all_files = list(root.rglob("*"))
file_map = {f.name: f for f in all_files if f.is_file()}

report_lines = []
report_lines.append(f"{'DRQ ID':<42} | {'Deliverable':<36} | {'Found Location':<60}")
report_lines.append("-" * 145)

found_count = 0
missing_count = 0

for d in drqs:
    req_id = d["id"]
    for deliv in d["all_delivs"]:
        deliv_name = Path(deliv).name
        # Check direct pending path
        pending_path = root / deliv
        if pending_path.exists():
            loc = str(pending_path.relative_to(root))
            found_count += 1
        elif deliv_name in file_map:
            loc = str(file_map[deliv_name].relative_to(root))
            found_count += 1
        else:
            loc = "NOT FOUND"
            missing_count += 1
        report_lines.append(f"{req_id:<42} | {deliv_name:<36} | {loc:<60}")

report_lines.append("=" * 145)
report_lines.append(f"Total: {found_count + missing_count} | Found on disk: {found_count} | Missing: {missing_count}")

out_path = root / r".agents/teamwork/spec_miner_techart_3/deliverables_inventory.txt"
out_path.write_text("\n".join(report_lines), encoding="utf-8")
print(f"Inventory written to {out_path}: {found_count} found, {missing_count} missing.")
