#!/bin/bash

# E2E Test Setup for T0.9
# Creates a fixture repository for testing the full orchestrator workflow

set -e

FIXTURE_DIR="${1:-/tmp/fixture}"
REMOTE_DIR="${2:-/tmp/fixture-remote}"

echo "=== Creating fixture repository structure ==="
mkdir -p "$FIXTURE_DIR"/tasks/.orchestrator/lessons
mkdir -p "$REMOTE_DIR"

# Initialize the fixture repo as a git repo
cd "$FIXTURE_DIR"
git init
git config user.email "test@example.com"
git config user.name "Test User"

# Create tasks/INDEX.md
cat > tasks/INDEX.md << 'EOF'
# Fixture Task Board

This is a simple fixture repository for testing the Poimen Orchestrator system.

## Tasks

| ID | Description | Status |
|----|-------------|--------|
| T0.1 | Create output.txt with "hello world" | [ ] |
| T0.2 | Create result.json with valid JSON | [ ] |
| T0.3 | Create done.txt with "COMPLETE" | [ ] |
EOF

# Create tasks/board.md
cat > tasks/board.md << 'EOF'
# Task Board — Fixture T0

| ID | Scope | Status | Branch | Verification |
|----|-------|--------|--------|--------------|
| T0.1 | Create file output.txt with content "hello world" | [ ] | `task/T0.1` | File exists and contains correct text |
| T0.2 | Create file result.json with valid JSON | [ ] | `task/T0.2` | File exists and parses as JSON |
| T0.3 | Create file done.txt with "COMPLETE" | [ ] | `task/T0.3` | File exists and contains correct text |
EOF

# Create a .gitkeep file so the directory exists
touch tasks/.orchestrator/.gitkeep

# Initial commit
git add .
git commit -m "Initial fixture setup"

echo "✓ Fixture repository created at $FIXTURE_DIR"
echo "  Remote at $REMOTE_DIR"
echo ""
echo "To start the orchestrator, run:"
echo "  go run ./cmd/starter \\"
echo "    --repo $FIXTURE_DIR \\"
echo "    --remote file://$REMOTE_DIR \\"
echo "    --milestone T0 \\"
echo "    --dry-run"
