#!/bin/bash

# Script to migrate all route files from JS to TypeScript
# This script will:
# 1. Copy each .js file to .ts
# 2. Add basic TypeScript types
# 3. Move to src/routes directory

echo "Starting route migration to TypeScript..."

# Create src/routes directories
mkdir -p src/routes/tasks
mkdir -p src/routes/agents  
mkdir -p src/routes/projects
mkdir -p src/routes/bundles
mkdir -p src/routes/orchestrator
mkdir -p src/routes/usage
mkdir -p src/routes/database
mkdir -p src/routes/mcp
mkdir -p src/routes/health
mkdir -p src/routes/rbac
mkdir -p src/routes/postmortem

# Function to migrate a route file
migrate_route() {
    local src_file=$1
    local dest_dir=$2
    local filename=$(basename "$src_file" .js)
    local dest_file="$dest_dir/${filename}.ts"
    
    echo "Migrating: $src_file -> $dest_file"
    
    # Copy and transform the file
    cp "$src_file" "$dest_file"
    
    # Add TypeScript imports at the beginning
    sed -i '' '1s/^/import { Router, Request, Response, NextFunction } from '\''express'\'';\n/' "$dest_file"
    
    # Replace req, res parameters with typed versions where possible
    sed -i '' 's/async (req, res)/async (req: Request, res: Response)/g' "$dest_file"
    sed -i '' 's/(req, res)/(req: Request, res: Response)/g' "$dest_file"
    sed -i '' 's/(req, res, next)/(req: Request, res: Response, next: NextFunction)/g' "$dest_file"
    
    # Update imports to use .js extension for ES modules
    sed -i '' "s/from '\(\..*\)'/from '\1.js'/g" "$dest_file"
    sed -i '' 's/from "\(\..*\)"/from "\1.js"/g' "$dest_file"
    
    # Fix double .js.js extensions
    sed -i '' 's/\.js\.js/.js/g' "$dest_file"
}

# Migrate task routes
for file in api/routes/tasks/*.js; do
    [ -f "$file" ] && migrate_route "$file" "src/routes/tasks"
done

# Migrate agent routes
for file in api/routes/agents/*.js; do
    [ -f "$file" ] && migrate_route "$file" "src/routes/agents"
done

# Migrate project routes
for file in api/routes/projects/*.js; do
    [ -f "$file" ] && migrate_route "$file" "src/routes/projects"
done

# Migrate bundle routes
for file in api/routes/bundles/*.js; do
    [ -f "$file" ] && migrate_route "$file" "src/routes/bundles"
done

# Migrate orchestrator routes
for file in api/routes/orchestrator/*.js; do
    [ -f "$file" ] && migrate_route "$file" "src/routes/orchestrator"
done

# Migrate usage routes
for file in api/routes/usage/*.js; do
    [ -f "$file" ] && migrate_route "$file" "src/routes/usage"
done

# Migrate database routes
for file in api/routes/database/*.js; do
    [ -f "$file" ] && migrate_route "$file" "src/routes/database"
done

# Migrate MCP routes
for file in api/routes/mcp/*.js; do
    [ -f "$file" ] && migrate_route "$file" "src/routes/mcp"
done

# Migrate health routes
for file in api/routes/health/*.js; do
    [ -f "$file" ] && migrate_route "$file" "src/routes/health"
done

# Migrate RBAC routes
for file in api/routes/rbac/*.js; do
    [ -f "$file" ] && migrate_route "$file" "src/routes/rbac"
done

# Migrate postmortem routes
for file in api/routes/postmortem/*.js; do
    [ -f "$file" ] && migrate_route "$file" "src/routes/postmortem"
done

echo "Route migration complete!"
echo "Files migrated to src/routes/"