import subprocess, json

# Get D2 routine
result = subprocess.run(
    ["hevy", "routine", "2164feea-1b5f-4822-adb5-aec6e58cdf88", "--json"],
    capture_output=True, text=True, timeout=30
)
raw = result.stdout
idx = raw.find('{')
data = json.loads(raw[idx:])

# Pretty print the routine
routine = data if 'routine' not in data else data['routine']
print("TITLE:", routine.get('title'))
print("ID:", routine.get('id'))
print()
for i, ex in enumerate(routine.get('exercises', [])):
    eid = ex.get('exercise_template_id', '?')
    notes = ex.get('notes', '')
    rest = ex.get('rest_seconds', '?')
    sets = ex.get('sets', [])
    print(f"[{i}] ID={eid} | Rest={rest}s | Notes={notes}")
    for j, s in enumerate(sets):
        t = s.get('type','?')
        w = s.get('weight_kg')
        r = s.get('reps')
        rr = s.get('rep_range')
        if rr:
            print(f"    Set {j}: {t} {w}kg x {r} (range {rr['start']}-{rr['end']})")
        else:
            print(f"    Set {j}: {t} {w}kg x {r}")
    print()
