#!/bin/bash

# Script to migrate middleware and services to TypeScript

echo "Migrating middleware to TypeScript..."

# Migrate middleware files
for file in api/middleware/*.js; do
    if [ -f "$file" ]; then
        filename=$(basename "$file" .js)
        if [ "$filename" != "errorHandler" ] && [ "$filename" != "validation" ]; then
            dest="src/middleware/${filename}.ts"
            echo "Migrating: $file -> $dest"
            cp "$file" "$dest"
            
            # Add TypeScript imports
            sed -i '' '1s/^/import { Request, Response, NextFunction } from '\''express'\'';\n/' "$dest"
            
            # Fix imports
            sed -i '' "s|from '\.\./utils/|from '../utils/|g" "$dest"
            sed -i '' "s|from '\.\./|from '../|g" "$dest"
            sed -i '' "s/from '\(\..*\)'/from '\1.js'/g" "$dest"
            sed -i '' 's/\.js\.js/.js/g' "$dest"
            
            # Add basic types
            sed -i '' 's/(req, res, next)/(req: Request, res: Response, next: NextFunction)/g' "$dest"
            sed -i '' 's/(req, res)/(req: Request, res: Response)/g' "$dest"
        fi
    fi
done

echo "Migrating services to TypeScript..."

# Migrate service files
for file in api/services/*.js; do
    if [ -f "$file" ]; then
        filename=$(basename "$file" .js)
        dest="src/services/${filename}.ts"
        echo "Migrating: $file -> $dest"
        cp "$file" "$dest"
        
        # Fix imports
        sed -i '' "s|from '\.\./utils/|from '../utils/|g" "$dest"
        sed -i '' "s|from '\.\./|from '../|g" "$dest"
        sed -i '' "s/from '\(\..*\)'/from '\1.js'/g" "$dest"
        sed -i '' 's/\.js\.js/.js/g' "$dest"
        
        # Add type annotations for common patterns
        sed -i '' 's/async function \([a-zA-Z]*\)(\([^)]*\))/async function \1(\2: any): Promise<any>/g' "$dest"
        sed -i '' 's/function \([a-zA-Z]*\)(\([^)]*\))/function \1(\2: any): any/g' "$dest"
    fi
done

echo "Migrating API index to TypeScript..."

# Migrate api/index.js to TypeScript
if [ -f "api/index.js" ]; then
    cp "api/index.js" "src/api/index.ts"
    
    # Add TypeScript imports
    sed -i '' '1s/^/import { Router } from '\''express'\'';\n/' "src/api/index.ts"
    
    # Fix imports
    sed -i '' "s|from '\./routes/|from '../routes/|g" "src/api/index.ts"
    sed -i '' "s/from '\(\..*\)'/from '\1.js'/g" "src/api/index.ts"
    sed -i '' 's/\.js\.js/.js/g' "src/api/index.ts"
fi

echo "Migrating utility files to TypeScript..."

# Migrate remaining utility files
for file in api/utils/*.js; do
    if [ -f "$file" ]; then
        filename=$(basename "$file" .js)
        # Skip files already migrated
        if [ ! -f "src/utils/${filename}.ts" ]; then
            dest="src/utils/${filename}.ts"
            echo "Migrating: $file -> $dest"
            cp "$file" "$dest"
            
            # Fix imports
            sed -i '' "s/from '\(\..*\)'/from '\1.js'/g" "$dest"
            sed -i '' 's/\.js\.js/.js/g' "$dest"
            
            # Add type annotations for exports
            sed -i '' 's/export function \([a-zA-Z]*\)(\([^)]*\))/export function \1(\2: any): any/g' "$dest"
            sed -i '' 's/export const \([a-zA-Z]*\) = (\([^)]*\)) =>/export const \1 = (\2: any): any =>/g' "$dest"
        fi
    fi
done

echo "Migration complete!"