#!/usr/bin/env bash
set -euo pipefail

# thoughts-init - Initialize thoughts/ directory structure in current project

# Configuration
USERNAME="${THOUGHTS_USER:-nikey_es}"
THOUGHTS_DIR="thoughts"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

info() {
  echo -e "${GREEN}[INFO]${NC} $1"
}

warn() {
  echo -e "${YELLOW}[WARN]${NC} $1"
}

error() {
  echo -e "${RED}[ERROR]${NC} $1"
  exit 1
}

# Check if already initialized
if [ -d "$THOUGHTS_DIR" ]; then
  warn "thoughts/ directory already exists. Re-initializing (existing files preserved)..."
fi

# Create directory structure
info "Creating thoughts/ directory structure..."

mkdir -p "$THOUGHTS_DIR/$USERNAME/tickets"
mkdir -p "$THOUGHTS_DIR/$USERNAME/notes"
mkdir -p "$THOUGHTS_DIR/shared/research"
mkdir -p "$THOUGHTS_DIR/shared/plans"
mkdir -p "$THOUGHTS_DIR/shared/prs"

info "Directory structure created:"
echo "  $THOUGHTS_DIR/"
echo "  ├── $USERNAME/"
echo "  │   ├── tickets/"
echo "  │   └── notes/"
echo "  └── shared/"
echo "      ├── research/"
echo "      ├── plans/"
echo "      └── prs/"

# Create README
if [ ! -f "$THOUGHTS_DIR/README.md" ]; then
  info "Creating README.md..."
  cat > "$THOUGHTS_DIR/README.md" <<EOF
# Thoughts Directory

This directory contains research documents, implementation plans, and notes for this project.

## Structure

- \`$USERNAME/\` - Personal notes and tickets
  - \`tickets/\` - Ticket documentation and tracking
  - \`notes/\` - Personal notes and observations
- \`shared/\` - Team-shared documents
  - \`research/\` - Research documents from /stepwise-core:research-codebase
  - \`plans/\` - Implementation plans from /stepwise-core:create-plan
  - \`prs/\` - PR descriptions and documentation

## Usage

Use Claude Code slash commands:
- \`/stepwise-core:research-codebase [topic]\` - Research and document codebase
- \`/stepwise-core:create-plan [description]\` - Create implementation plan
- \`/stepwise-core:implement-plan [plan-file]\` - Execute a plan
- \`/stepwise-core:validate-plan [plan-file]\` - Validate implementation
EOF
fi

info "✓ thoughts/ initialized successfully!"
echo ""
info "Next steps:"
echo "  1. Use Claude Code slash commands like /stepwise-core:research-codebase"
echo "  2. Commit thoughts/ directory to git"
