"""Create one workspace and one prompt per run.

Usage: python3 make_runs.py <experiment-dir> <runs-per-cell>

<experiment-dir> must contain:
  tasks/<T>.txt            the task, as the developer would type it
  tasks/<T>/  (optional)   starting files copied into the workspace (keep paths, e.g. src/main/kotlin/Csv.kt)
  conditions/<C>.txt       text added before the task for condition C ("none.txt" can be empty).
                           {WS} is replaced with the workspace path, {TOOL} with <experiment-dir>/tool.
Writes ws/<T>-<C>-r<N>/ (a Gradle project from kit/template) and prompts/<T>-<C>-r<N>.txt.
"""
import os, shutil, sys
KIT = os.path.dirname(os.path.abspath(__file__))
E, N = os.path.abspath(sys.argv[1]), int(sys.argv[2])
tasks = sorted(f[:-4] for f in os.listdir(f'{E}/tasks') if f.endswith('.txt'))
conds = sorted(f[:-4] for f in os.listdir(f'{E}/conditions') if f.endswith('.txt'))
os.makedirs(f'{E}/prompts', exist_ok=True)
for t in tasks:
    task = open(f'{E}/tasks/{t}.txt').read().strip()
    for c in conds:
        for r in range(1, N + 1):
            k = f'{t}-{c}-r{r}'; ws = f'{E}/ws/{k}'
            shutil.rmtree(ws, ignore_errors=True)
            shutil.copytree(f'{KIT}/template', ws)
            for d in ('src/main/kotlin', 'src/test/kotlin'): os.makedirs(f'{ws}/{d}', exist_ok=True)
            if os.path.isdir(f'{E}/tasks/{t}'): shutil.copytree(f'{E}/tasks/{t}', ws, dirs_exist_ok=True)
            head = (f'You are Claude Code, a coding agent, working in a Kotlin project at {ws}. The developer gave you the task at the end of '
                    f'this file and then stepped away: do not ask questions, make reasonable decisions yourself, and finish the task. Work only '
                    f'inside {ws}. Gradle is installed and has its dependencies cached; run the tests with: cd {ws} && gradle test -q --offline\n'
                    'When you are done, reply with a short summary of what you did. That reply is your final answer.\n')
            extra = open(f'{E}/conditions/{c}.txt').read().replace('{WS}', ws).replace('{TOOL}', f'{E}/tool').strip()
            open(f'{E}/prompts/{k}.txt', 'w').write(head + ('\n' + extra + '\n' if extra else '') + '\n=== TASK ===\n' + task + '\n')
print(f'{len(tasks)} tasks x {len(conds)} conditions x {N} runs = {len(tasks)*len(conds)*N} prompts in {E}/prompts')
