#!/bin/bash
# oracle-test-runner-v2.sh — md 파일 기반으로 Claude Code 실행
# Usage: bash oracle-test-runner-v2.sh <game-slug>

GAME_SLUG=${1:-abyssal-descent}
ARCADE_DIR="/home/ubuntu/sites/arcade"
INSTRUCTIONS="$ARCADE_DIR/pipeline/agent-instructions.md"
LOG_DIR="/tmp/bot_logs"

mkdir -p /tmp/bots "/tmp/bot_snapshots/${GAME_SLUG}" "$LOG_DIR"

echo "=== PatchMe Game Test: $GAME_SLUG ==="
echo "Time: $(date)"

# 기존 프로세스 정리
pkill -f "bot_${GAME_SLUG}" 2>/dev/null
pkill -f "bot-runner.sh ${GAME_SLUG}" 2>/dev/null
sleep 1

# bot-runner 백그라운드 시작
bash "$ARCADE_DIR/pipeline/bot-runner.sh" "$GAME_SLUG" &
RUNNER_PID=$!
echo "Runner PID: $RUNNER_PID"

# Claude Code 실행 — md 파일을 읽게 함
cd "$ARCADE_DIR"
claude -p "Read the file $INSTRUCTIONS and follow all instructions. The game slug is: $GAME_SLUG" \
  --allowedTools "Read,Write,Edit,Bash,Glob,Grep" \
  2>&1 | tee "$LOG_DIR/${GAME_SLUG}_agent.log"

# 완료 후 runner 종료
kill $RUNNER_PID 2>/dev/null
echo "=== Test Complete ==="
