import subprocess, json, sys

# Get the last D1 W4 workout (May 30) to understand the W4 progression context
result = subprocess.run(
    ["hevy", "workouts", "--limit", "2", "--kg", "--json"],
    capture_output=True, text=True, timeout=30
)
raw = result.stdout
idx = raw.find('[')
if idx < 0:
    idx = raw.find('{')
data = json.loads(raw[idx:])

if isinstance(data, list):
    workouts = data
elif isinstance(data, dict):
    workouts = data.get('workouts', data.get('data', []))

# Find D1 W4
for w in workouts:
    title = w.get('title', '')
    if 'D1' in title:
        print(f"ID: {w.get('id')} | {title} | {w.get('start_time')} | Duration: {w.get('duration_minutes')}min")
        # Get full details
        w_id = w.get('id')
        result2 = subprocess.run(
            ["hevy", "workout", w_id, "--kg", "--json"],
            capture_output=True, text=True, timeout=30
        )
        raw2 = result2.stdout
        idx2 = raw2.find('{')
        detail = json.loads(raw2[idx2:])
        for ex in detail.get('exercises', []):
            extitle = ex.get('title', ex.get('exercise_template', {}).get('title', '?'))
            sets = ex.get('sets', [])
            normal_sets = [s for s in sets if s.get('type') == 'normal']
            if normal_sets:
                weights_reps = [f"{s.get('weight_kg')}×{s.get('reps')}" for s in normal_sets]
                print(f"  {extitle}: {' | '.join(weights_reps)}")
        break
