#!/usr/bin/env python3
import json
import subprocess
from pathlib import Path

OPENCLAW_ROOT = Path.home() / '.openclaw'
AGENT = 'playgrounds'
WORKSPACE = OPENCLAW_ROOT / 'workspace-playgrounds'
REMIX_DIR = WORKSPACE / 'remix'


def fail(payload, code=1):
    print(json.dumps(payload, ensure_ascii=False))
    raise SystemExit(code)


def run(cmd):
    return subprocess.run(cmd, capture_output=True, text=True)


def main():
    result = run(['mc', 'new', AGENT])
    output = (result.stdout or '') + (result.stderr or '')
    if result.returncode != 0 and '已存在' not in output and 'exists' not in output.lower():
        fail({'prepared': False, 'error': 'mc_new_failed', 'agent': AGENT, 'details': output.strip()})

    WORKSPACE.mkdir(parents=True, exist_ok=True)
    REMIX_DIR.mkdir(parents=True, exist_ok=True)

    print(json.dumps({
        'prepared': True,
        'agent': AGENT,
        'workspace': str(WORKSPACE),
        'remix_dir': str(REMIX_DIR),
    }, ensure_ascii=False))


if __name__ == '__main__':
    main()
