#!/usr/bin/env python3
"""rotation.py — the least-recently calibrated target with a corpus. No choice, by design.
Usage: rotation.py [--root research]"""
import os, re, sys
root = sys.argv[sys.argv.index("--root") + 1] if "--root" in sys.argv else "research"
ledger = os.path.join(root, "_calibration", "ledger.md")
last = {}
if os.path.exists(ledger):
    for line in open(ledger):
        m = re.match(r"\|\s*(\d{4}-\d{2}-\d{2})\s*\|\s*([\w.-]+)\s*\|", line)
        if m:
            last[m.group(2)] = max(last.get(m.group(2), ""), m.group(1))
targets = sorted(t for t in os.listdir(root) if not t.startswith(("_", ".")) and os.path.isdir(os.path.join(root, t)))
targets.sort(key=lambda t: (last.get(t, ""), t))
print(targets[0] if targets else "no targets")
for t in targets[:5]:
    print(f"  {t:24} last calibrated {last.get(t, 'never')}")
