#!/bin/bash

# Migration script: ck-ai-agent-dev → ck-llm-agent-dev
# This script helps users migrate from the old AI agent expansion pack to the new LLM agent expansion pack

set -e

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

echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
echo -e "${BLUE}    CK AI → LLM Agent Migration Tool${NC}"
echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
echo ""

# Check if we're in a directory with BMAD installation
if [ ! -d ".bmad-core" ]; then
    echo -e "${RED}❌ Error: No BMAD installation found in current directory${NC}"
    echo "Please run this script from your project root where BMAD is installed"
    exit 1
fi

# Check if old expansion pack is installed
OLD_PACK_DIR=".bmad-ck-ai-agent-dev"
NEW_PACK_DIR=".bmad-ck-llm-agent-dev"

if [ ! -d "$OLD_PACK_DIR" ]; then
    echo -e "${YELLOW}⚠️  No ck-ai-agent-dev installation found${NC}"
    echo "Nothing to migrate."
    exit 0
fi

echo -e "${GREEN}✓ Found existing ck-ai-agent-dev installation${NC}"
echo ""

# Check if new pack already exists
if [ -d "$NEW_PACK_DIR" ]; then
    echo -e "${YELLOW}⚠️  Warning: ck-llm-agent-dev already exists${NC}"
    read -p "Do you want to overwrite it? (y/N) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo "Migration cancelled."
        exit 0
    fi
    rm -rf "$NEW_PACK_DIR"
fi

# Create backup
echo -e "${BLUE}Creating backup...${NC}"
BACKUP_DIR=".bmad-backup-ai-to-llm-$(date +%Y%m%d-%H%M%S)"
cp -r "$OLD_PACK_DIR" "$BACKUP_DIR"
echo -e "${GREEN}✓ Backup created: $BACKUP_DIR${NC}"

# Perform migration
echo -e "${BLUE}Migrating expansion pack...${NC}"
mv "$OLD_PACK_DIR" "$NEW_PACK_DIR"

# Update IDE configurations
echo -e "${BLUE}Updating IDE configurations...${NC}"

# Update .cursorrules if exists
if [ -f ".cursorrules" ]; then
    sed -i '' 's/@ai-architect/@llm-architect/g' .cursorrules
    sed -i '' 's/@ai-engineer/@llm-engineer/g' .cursorrules
    sed -i '' 's/@ai-orchestrator/@llm-orchestrator/g' .cursorrules
    sed -i '' 's/@ai-safety-governance/@llm-safety-governance/g' .cursorrules
    echo -e "${GREEN}✓ Updated .cursorrules${NC}"
fi

# Update .cursor/rules/ directory
if [ -d ".cursor/rules" ]; then
    # Rename files
    [ -f ".cursor/rules/ai-architect.mdc" ] && mv ".cursor/rules/ai-architect.mdc" ".cursor/rules/llm-architect.mdc"
    [ -f ".cursor/rules/ai-engineer.mdc" ] && mv ".cursor/rules/ai-engineer.mdc" ".cursor/rules/llm-engineer.mdc"
    [ -f ".cursor/rules/ai-orchestrator.mdc" ] && mv ".cursor/rules/ai-orchestrator.mdc" ".cursor/rules/llm-orchestrator.mdc"
    [ -f ".cursor/rules/ai-safety-governance.mdc" ] && mv ".cursor/rules/ai-safety-governance.mdc" ".cursor/rules/llm-safety-governance.mdc"
    echo -e "${GREEN}✓ Updated .cursor/rules/${NC}"
fi

# Update .claude/CLAUDE.md if exists
if [ -f ".claude/CLAUDE.md" ]; then
    sed -i '' 's|/ai-architect|/llm-architect|g' .claude/CLAUDE.md
    sed -i '' 's|/ai-engineer|/llm-engineer|g' .claude/CLAUDE.md
    sed -i '' 's|/ai-orchestrator|/llm-orchestrator|g' .claude/CLAUDE.md
    sed -i '' 's|/ai-safety-governance|/llm-safety-governance|g' .claude/CLAUDE.md
    sed -i '' 's|/ai-wizard|/llm-wizard|g' .claude/CLAUDE.md
    echo -e "${GREEN}✓ Updated .claude/CLAUDE.md${NC}"
fi

# Update other IDE configurations
for file in .windsurfrules .clinerules .geminirules; do
    if [ -f "$file" ]; then
        sed -i '' 's/@ai-architect/@llm-architect/g' "$file"
        sed -i '' 's/@ai-engineer/@llm-engineer/g' "$file"
        sed -i '' 's/@ai-orchestrator/@llm-orchestrator/g' "$file"
        sed -i '' 's/@ai-safety-governance/@llm-safety-governance/g' "$file"
        sed -i '' 's/@ai-wizard/@llm-wizard/g' "$file"
        echo -e "${GREEN}✓ Updated $file${NC}"
    fi
done

# Update .roo/.roomodes/ if exists
if [ -d ".roo/.roomodes" ]; then
    for file in .roo/.roomodes/ai-*.md; do
        if [ -f "$file" ]; then
            newname="${file/ai-/llm-}"
            mv "$file" "$newname"
        fi
    done
    echo -e "${GREEN}✓ Updated .roo/.roomodes/${NC}"
fi

echo ""
echo -e "${GREEN}═══════════════════════════════════════════════════════════════${NC}"
echo -e "${GREEN}✅ Migration completed successfully!${NC}"
echo -e "${GREEN}═══════════════════════════════════════════════════════════════${NC}"
echo ""
echo -e "${BLUE}What changed:${NC}"
echo "• Expansion pack renamed: ck-ai-agent-dev → ck-llm-agent-dev"
echo "• Agent commands updated: @ai-* → @llm-*"
echo "• All IDE configurations updated"
echo ""
echo -e "${BLUE}New agent commands:${NC}"
echo "• @llm-wizard (NEW) - Your friendly guide to LLM development"
echo "• @llm-architect - LLM system design expert"
echo "• @llm-engineer - LLM implementation specialist"
echo "• @llm-orchestrator - Multi-agent coordinator"
echo "• @llm-safety-governance - Safety and compliance expert"
echo ""
echo -e "${YELLOW}Note: A backup was created at: $BACKUP_DIR${NC}"
echo -e "${YELLOW}You can remove it once you've verified everything works.${NC}"