#!/bin/bash
# pre-deploy-validation.sh - Script to validate environment before deployment
# Usage: ./bin/pre-deploy-validation.sh [--ci]

set -e

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# Parse arguments
CI_MODE=false

for arg in "$@"; do
  case $arg in
    --ci)
      CI_MODE=true
      shift
      ;;
  esac
done

echo -e "${GREEN}=== Pre-Deployment Validation Tool ===${NC}"
echo "Verifying environment and test readiness before deployment..."

# Check if we're in the right directory
if [ ! -d "tests/e2e" ]; then
  echo -e "${RED}Error: Please run this script from the wordpress-dev directory${NC}"
  exit 1
fi

# Create logs directory
mkdir -p logs/pre-deploy

# Log function
log() {
  echo "[$(date +"%Y-%m-%d %H:%M:%S")] $1" >> logs/pre-deploy/validation-$(date +"%Y%m%d").log
  echo "$1"
}

# Function to run a validation check
run_check() {
  local check_name=$1
  local check_command=$2
  
  log "Running check: ${check_name}"
  
  if eval "${check_command}"; then
    log "${GREEN}✓ Check passed: ${check_name}${NC}"
    return 0
  else
    log "${RED}✗ Check failed: ${check_name}${NC}"
    return 1
  fi
}

# Validation checks
validate_npm_dependencies() {
  log "Validating npm dependencies..."
  npm list @playwright/test > /dev/null
}

validate_test_config() {
  log "Validating Playwright configuration..."
  [ -f "playwright.config.ts" ]
}

validate_plugin_activation() {
  log "Checking if plugin activation script exists..."
  [ -f "bin/verify-plugin-status.sh" ] || [ -f "bin/activate-plugin.sh" ]
}

validate_selectors() {
  log "Validating critical selectors..."
  bash bin/verify-selectors.sh
}

validate_test_users() {
  log "Checking test user creation script..."
  [ -f "bin/create-test-users.sh" ]
}

validate_certificate_system() {
  log "Validating certificate test scripts..."
  [ -f "tests/e2e/certificate-generation-checked-in.test.ts" ]
}

# Main validation logic
FAILURES=0

# Run all validation checks
if ! run_check "NPM Dependencies" validate_npm_dependencies; then
  FAILURES=$((FAILURES + 1))
fi

if ! run_check "Test Configuration" validate_test_config; then
  FAILURES=$((FAILURES + 1))
fi

if ! run_check "Plugin Activation" validate_plugin_activation; then
  FAILURES=$((FAILURES + 1))
fi

if ! run_check "Test Users" validate_test_users; then
  FAILURES=$((FAILURES + 1))
fi

if ! run_check "Certificate System" validate_certificate_system; then
  FAILURES=$((FAILURES + 1))
fi

# Run selector validation last (most comprehensive)
if ! run_check "Critical Selectors" validate_selectors; then
  FAILURES=$((FAILURES + 1))
fi

# Summary
log "\n${GREEN}=== Pre-Deployment Validation Summary ===${NC}"
if [ $FAILURES -eq 0 ]; then
  log "${GREEN}✓ All validation checks passed successfully${NC}"
  log "Environment is ready for deployment"
  exit 0
else
  log "${RED}✗ ${FAILURES} validation check(s) failed${NC}"
  log "Please fix the issues before proceeding with deployment"
  
  if [ "$CI_MODE" = true ]; then
    log "CI build failed due to pre-deployment validation failures"
    exit 1
  fi
  
  exit 1
fi