#!/bin/bash
# Setup parallel Git worktrees for Trax development

echo "🌳 Setting up parallel worktrees for Trax..."
echo ""

# Base directory
BASE_DIR=$(dirname $(pwd))
MAIN_DIR=$(pwd)

# Function to create worktree
create_worktree() {
    local name=$1
    local branch=$2
    local dir="$BASE_DIR/trax-$name"
    
    if [ -d "$dir" ]; then
        echo "⚠️  Worktree $dir already exists, skipping..."
    else
        echo "Creating worktree: $dir (branch: $branch)"
        git worktree add "$dir" -b "$branch"
        
        # Link shared context
        if [ -d "$MAIN_DIR/.claude" ]; then
            echo "  Linking shared context..."
            mkdir -p "$dir/.claude"
            ln -sf "$MAIN_DIR/.claude/context" "$dir/.claude/context" 2>/dev/null
            ln -sf "$MAIN_DIR/.claude/research" "$dir/.claude/research" 2>/dev/null
        fi
        
        echo "✅ Created $name worktree"
    fi
    echo ""
}

# Create worktrees
create_worktree "tests" "feature/tests"
create_worktree "docs" "feature/docs"
create_worktree "db" "feature/database"
create_worktree "api" "feature/api"

# Show status
echo "═══════════════════════════════════════════"
echo "Worktree Status:"
echo "═══════════════════════════════════════════"
git worktree list
echo ""

echo "📝 To use parallel development:"
echo "  1. Open separate terminals"
echo "  2. cd to each worktree directory"
echo "  3. Run 'claude' in each terminal"
echo ""
echo "🧹 To clean up worktrees later:"
echo "  git worktree remove ../trax-tests"
echo "  git worktree prune"