#!/bin/bash

# Script to replace console.* statements with logger calls

echo "Replacing console.log statements..."
find ./src -type f \( -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.jsx" \) -exec sed -i 's/console\.log(/logger.info(/g' {} +

echo "Replacing console.error statements..."
find ./src -type f \( -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.jsx" \) -exec sed -i 's/console\.error(/logger.error(/g' {} +

echo "Replacing console.warn statements..."
find ./src -type f \( -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.jsx" \) -exec sed -i 's/console\.warn(/logger.warn(/g' {} +

echo "Adding logger import to files that need it..."

# Find files that now contain logger calls but don't have the import
for file in $(grep -l "logger\." ./src --include="*.ts" --include="*.tsx" --include="*.js" --include="*.jsx"); do
    if ! grep -q "import.*logger" "$file"; then
        # Add import after the last import statement or at the beginning if no imports
        if grep -q "^import" "$file"; then
            # Find the last import line and add after it
            last_import_line=$(grep -n "^import" "$file" | tail -1 | cut -d: -f1)
            sed -i "${last_import_line}a import logger from '@/lib/logger';" "$file"
        else
            # Add at the beginning of the file
            sed -i '1i import logger from '\''@/lib/logger';\''"$file"
        fi
    fi
done

echo "Replacement complete!"