#!/bin/bash

# Migration script from claude-code-router to Conduit
# This script helps users migrate their existing configurations

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}           Migrating from claude-code-router to Conduit        ${NC}"
echo -e "${BLUE}================================================================${NC}"
echo

# Check if old installation exists
OLD_CONFIG_DIR="$HOME/.claude-code-router"
NEW_CONFIG_DIR="$HOME/.conduit"

if [ ! -d "$OLD_CONFIG_DIR" ]; then
    echo -e "${YELLOW}No previous claude-code-router installation found.${NC}"
    echo -e "${GREEN}You can proceed with a fresh Conduit installation.${NC}"
    exit 0
fi

echo -e "${GREEN}Found existing claude-code-router configuration at:${NC}"
echo -e "${BLUE}$OLD_CONFIG_DIR${NC}"
echo

# Ask for confirmation
echo -e "${YELLOW}This script will:${NC}"
echo "  1. Stop any running claude-code-router service"
echo "  2. Migrate configuration files to ~/.conduit/"
echo "  3. Preserve your existing API keys and settings"
echo "  4. Create a backup of the old configuration"
echo
read -p "Do you want to proceed? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo -e "${YELLOW}Migration cancelled.${NC}"
    exit 0
fi

echo -e "${GREEN}Starting migration...${NC}"
echo

# Step 1: Stop existing service
echo -e "${BLUE}Step 1: Stopping existing claude-code-router service...${NC}"
if command -v ccr &> /dev/null; then
    if ccr status | grep -q "Status: Running"; then
        echo "Stopping claude-code-router service..."
        ccr stop || echo -e "${YELLOW}Warning: Could not stop service using ccr command${NC}"
    else
        echo "Service is not running."
    fi
else
    echo "ccr command not found - service may not be running"
fi

# Check for systemd service
if systemctl is-active --quiet claude-code-router 2>/dev/null; then
    echo "Stopping systemd service..."
    sudo systemctl stop claude-code-router || echo -e "${YELLOW}Warning: Could not stop systemd service${NC}"
fi

# Check for PM2 process
if command -v pm2 &> /dev/null; then
    if pm2 list | grep -q "claude-code-router"; then
        echo "Stopping PM2 process..."
        pm2 stop claude-code-router || echo -e "${YELLOW}Warning: Could not stop PM2 process${NC}"
        pm2 delete claude-code-router || echo -e "${YELLOW}Warning: Could not delete PM2 process${NC}"
    fi
fi

echo -e "${GREEN}✓ Service stop completed${NC}"
echo

# Step 2: Create backup
echo -e "${BLUE}Step 2: Creating backup...${NC}"
BACKUP_DIR="$OLD_CONFIG_DIR-backup-$(date +%Y%m%d-%H%M%S)"
cp -r "$OLD_CONFIG_DIR" "$BACKUP_DIR"
echo -e "${GREEN}✓ Backup created at: $BACKUP_DIR${NC}"
echo

# Step 3: Create new config directory
echo -e "${BLUE}Step 3: Creating Conduit configuration directory...${NC}"
mkdir -p "$NEW_CONFIG_DIR"
echo -e "${GREEN}✓ Created $NEW_CONFIG_DIR${NC}"

# Step 4: Migrate configuration files
echo -e "${BLUE}Step 4: Migrating configuration files...${NC}"

# Migrate main config file
if [ -f "$OLD_CONFIG_DIR/config.json" ]; then
    cp "$OLD_CONFIG_DIR/config.json" "$NEW_CONFIG_DIR/config.json"
    echo -e "${GREEN}✓ Migrated config.json${NC}"
else
    echo -e "${YELLOW}! No config.json found to migrate${NC}"
fi

# Migrate plugins directory
if [ -d "$OLD_CONFIG_DIR/plugins" ]; then
    cp -r "$OLD_CONFIG_DIR/plugins" "$NEW_CONFIG_DIR/"
    echo -e "${GREEN}✓ Migrated plugins directory${NC}"
else
    # Create empty plugins directory
    mkdir -p "$NEW_CONFIG_DIR/plugins"
    echo -e "${GREEN}✓ Created plugins directory${NC}"
fi

# Migrate any log files (but don't copy them, just note them)
if [ -f "$OLD_CONFIG_DIR/claude-code-router.log" ]; then
    echo -e "${BLUE}! Old log file found at: $OLD_CONFIG_DIR/claude-code-router.log${NC}"
    echo -e "${BLUE}  New log file will be: $NEW_CONFIG_DIR/conduit.log${NC}"
fi

echo

# Step 5: Cleanup instructions
echo -e "${BLUE}Step 5: Next steps and cleanup${NC}"
echo
echo -e "${GREEN}Migration completed successfully! 🎉${NC}"
echo
echo -e "${YELLOW}Next steps:${NC}"
echo "  1. Uninstall the old package: npm uninstall -g claude-code-router-enhanced"
echo "  2. Install Conduit: npm install -g conduit"
echo "  3. Test the new installation: conduit status"
echo "  4. Start the service: conduit start"
echo
echo -e "${YELLOW}Your configuration has been migrated to:${NC}"
echo -e "${BLUE}$NEW_CONFIG_DIR${NC}"
echo
echo -e "${YELLOW}Backup of your old configuration is at:${NC}"
echo -e "${BLUE}$BACKUP_DIR${NC}"
echo
echo -e "${YELLOW}Once you're satisfied that everything works correctly, you can remove:${NC}"
echo -e "${RED}  - Old config directory: $OLD_CONFIG_DIR${NC}"
echo -e "${RED}  - Backup directory: $BACKUP_DIR${NC}"
echo
echo -e "${GREEN}Happy routing with Conduit! 🚀${NC}"