#!/bin/bash

# Script to update hardcoded staging URLs in test files
# Will replace old URLs with references to the centralized config

# Set variables
OLD_URL="wordpress-974670-5399585.cloudwaysapps.com"
NEW_URL="upskill-staging.measurequick.com"
TESTS_DIR="/Users/ben/dev/upskill-event-manager/wordpress-dev/tests/e2e"

# Check if config directory exists, if not create it
mkdir -p "$TESTS_DIR/config"

# Check if central config file exists, create it if not
if [ ! -f "$TESTS_DIR/config/staging-config.ts" ]; then
  echo "Creating central config file..."
  cat > "$TESTS_DIR/config/staging-config.ts" << 'EOF'
/**
 * Shared staging configuration for E2E tests
 * 
 * This file centralizes the staging URL configuration to avoid
 * hardcoded URLs throughout the test files
 */

import dotenv from 'dotenv';

// Load environment variables
dotenv.config();

/**
 * Staging URL for tests
 * Uses the environment variable as the primary source
 * Falls back to the current staging URL if not set
 */
export const STAGING_URL = process.env.UPSKILL_STAGING_URL || 'https://upskill-staging.measurequick.com';

/**
 * Helper function to generate full URLs to specific paths
 * @param path The path relative to the staging URL
 * @returns Full URL
 */
export function getUrl(path: string): string {
  const basePath = path.startsWith('/') ? path : `/${path}`;
  return `${STAGING_URL}${basePath}`;
}

/**
 * Common paths used in tests
 */
export const PATHS = {
  // WordPress Admin
  admin: getUrl('/wp-admin'),
  login: getUrl('/community-login'),
  dashboard: getUrl('/hvac-dashboard'),
  createEvent: getUrl('/community/events/edit'),
  myEvents: getUrl('/my-events'),
  
  // Certificate paths
  certificatesReport: getUrl('/certificates-report'),
  generateCertificates: getUrl('/generate-certificates'),
  
  // Event related paths
  eventSummary: getUrl('/event-summary'),
  modifyEvent: getUrl('/modify-event'),
  
  // User paths
  profile: getUrl('/trainer-profile'),
  registration: getUrl('/trainer-registration'),
};

/**
 * Timeout settings for tests
 */
export const TIMEOUTS = {
  navigation: 30000,
  network: 15000,
  animation: 5000,
  standard: 10000,
};
EOF
fi

# Update BasePage to use the new config
echo "Updating BasePage.ts..."
if [ -f "$TESTS_DIR/pages/BasePage.ts" ]; then
  sed -i '' "s|baseUrl: string = 'https://$OLD_URL'|baseUrl: string = STAGING_URL|g" "$TESTS_DIR/pages/BasePage.ts"
  sed -i '' "1s|^|import { STAGING_URL } from '../config/staging-config';\n|" "$TESTS_DIR/pages/BasePage.ts"
fi

# Find all test files
echo "Finding test files with hardcoded URLs..."
TEST_FILES=$(grep -l "$OLD_URL" "$TESTS_DIR"/*.ts)

# Replace hardcoded URLs in each file
for file in $TEST_FILES; do
  echo "Processing $file..."
  
  # Add config import if it doesn't exist
  if ! grep -q "import.*staging-config" "$file"; then
    sed -i '' "1s|^|import { STAGING_URL, PATHS, TIMEOUTS } from './config/staging-config';\n|" "$file"
  fi
  
  # Replace hardcoded URLs with new URL
  sed -i '' "s|https://$OLD_URL|https://$NEW_URL|g" "$file"
  
  # Replace STAGING_URL variable declaration if it exists
  sed -i '' "s|const STAGING_URL = .*|// STAGING_URL is now imported from config|g" "$file"
  
  # Replace direct URL references with PATHS object
  sed -i '' "s|\`\${STAGING_URL}/hvac-dashboard/\`|PATHS.dashboard|g" "$file"
  sed -i '' "s|\`\${STAGING_URL}/community-login/\`|PATHS.login|g" "$file"
  sed -i '' "s|\`\${STAGING_URL}/wp-admin/\`|PATHS.admin|g" "$file"
  
  # Update page navigation timeouts
  sed -i '' "s|setDefaultNavigationTimeout(30000)|setDefaultNavigationTimeout(TIMEOUTS.navigation)|g" "$file"
  
  echo "Completed $file"
done

echo "URL update completed. ${#TEST_FILES[@]} files processed."
echo "You may still need to manually check files for specific URL patterns not caught by the script."