#!/usr/bin/env bash
# Usage: init-run.sh <slug> "<one-line goal>"
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../../.." && pwd)"
SLUG="${1:-}"
GOAL="${2:-}"

if [[ -z "$SLUG" ]]; then
  echo "usage: $0 <slug> \"<one-line goal>\"" >&2
  exit 1
fi

DIR="$ROOT/docs/product/$SLUG"
mkdir -p "$DIR/tasks"

if [[ ! -f "$DIR/README.md" ]]; then
  cat >"$DIR/README.md" <<EOF
# Product run: $SLUG

| Field | Value |
|-------|--------|
| Goal | ${GOAL:-TBD} |
| Phase | requirements |
| Status | draft |
| Updated | $(date -u +%Y-%m-%d) |

## Phase legend

\`requirements\` → \`spec\` → \`plan\` → \`tasks\` → \`ready-to-implement\`

## How to continue

\`\`\`text
/spec-driven $SLUG
/spec-driven $SLUG --status
\`\`\`
EOF
fi

if [[ ! -f "$DIR/tasks/INDEX.md" ]]; then
  cat >"$DIR/tasks/INDEX.md" <<'EOF'
# Tasks INDEX

| ID | Title | Milestone | Status | Est. lines |
|----|-------|-----------|--------|------------|
| — | (尚未拆 task；plan 批准後填入) | | | |
EOF
fi

echo "Initialized $DIR"
