#!/bin/bash

# Verify staging deployment script
# Run this on staging server to check if all required files are deployed

echo "=== STAGING DEPLOYMENT VERIFICATION ==="
echo "Date: $(date)"
echo

# Check if we're in the right directory
if [ ! -f "hvac-community-events.php" ]; then
    echo "ERROR: Not in HVAC plugin directory"
    echo "Please run this script from: /path/to/wordpress/wp-content/plugins/hvac-community-events/"
    exit 1
fi

echo "✓ Running from HVAC plugin directory"

# Critical files check
echo
echo "=== CRITICAL FILES CHECK ==="
CRITICAL_FILES=(
    "hvac-community-events.php"
    "includes/class-hvac-plugin.php"
    "includes/find-trainer/class-hvac-find-trainer-page.php"
    "includes/find-trainer/class-hvac-mapgeo-integration.php"
    "includes/find-trainer/class-hvac-contact-form-handler.php"
    "includes/find-trainer/class-hvac-trainer-directory-query.php"
    "assets/css/find-trainer.css"
    "assets/js/find-trainer.js"
)

MISSING_FILES=()
for file in "${CRITICAL_FILES[@]}"; do
    if [ -f "$file" ]; then
        echo "✓ $file"
    else
        echo "✗ $file (MISSING)"
        MISSING_FILES+=("$file")
    fi
done

# Check file permissions
echo
echo "=== FILE PERMISSIONS CHECK ==="
for file in "${CRITICAL_FILES[@]}"; do
    if [ -f "$file" ]; then
        PERMS=$(stat -c "%a" "$file")
        if [ "$PERMS" -ge "644" ]; then
            echo "✓ $file ($PERMS)"
        else
            echo "✗ $file ($PERMS) - insufficient permissions"
        fi
    fi
done

# PHP syntax check
echo
echo "=== PHP SYNTAX CHECK ==="
PHP_FILES=(
    "hvac-community-events.php"
    "includes/class-hvac-plugin.php"
    "includes/find-trainer/class-hvac-find-trainer-page.php"
)

SYNTAX_ERRORS=()
for file in "${PHP_FILES[@]}"; do
    if [ -f "$file" ]; then
        if php -l "$file" >/dev/null 2>&1; then
            echo "✓ $file - syntax OK"
        else
            echo "✗ $file - SYNTAX ERROR"
            php -l "$file"
            SYNTAX_ERRORS+=("$file")
        fi
    fi
done

# Check WordPress plugin status
echo
echo "=== WORDPRESS PLUGIN STATUS ==="
if command -v wp >/dev/null 2>&1; then
    if wp plugin is-active hvac-community-events 2>/dev/null; then
        echo "✓ HVAC plugin is active"
    else
        echo "✗ HVAC plugin is NOT active"
        echo "Run: wp plugin activate hvac-community-events"
    fi
    
    # Check for find-a-trainer page
    if wp post exists "find-a-trainer" --post_type=page 2>/dev/null; then
        echo "✓ find-a-trainer page exists"
    else
        echo "✗ find-a-trainer page does NOT exist"
        echo "The page should be auto-created by the plugin"
    fi
else
    echo "! WP-CLI not available - cannot check plugin status"
fi

# Summary
echo
echo "=== DEPLOYMENT SUMMARY ==="
if [ ${#MISSING_FILES[@]} -eq 0 ] && [ ${#SYNTAX_ERRORS[@]} -eq 0 ]; then
    echo "✓ Deployment appears successful"
    echo
    echo "Next steps if Find a Trainer still shows error:"
    echo "1. Enable WordPress debug mode (see enable-debug-mode.php)"
    echo "2. Upload and run test-find-trainer-direct.php"
    echo "3. Check server error logs"
    echo "4. Upload and run debug-find-trainer-error.php"
else
    echo "✗ Deployment has issues:"
    
    if [ ${#MISSING_FILES[@]} -gt 0 ]; then
        echo "  Missing files: ${MISSING_FILES[*]}"
        echo "  → Re-deploy missing files from local"
    fi
    
    if [ ${#SYNTAX_ERRORS[@]} -gt 0 ]; then
        echo "  Syntax errors in: ${SYNTAX_ERRORS[*]}"
        echo "  → Fix syntax errors and re-deploy"
    fi
fi

echo
echo "=== DEBUGGING TOOLS ==="
echo "Upload these files to staging for debugging:"
echo "- debug-find-trainer-error.php (test plugin loading)"
echo "- test-find-trainer-direct.php (comprehensive browser test)"
echo "- enable-debug-mode.php (WordPress debug instructions)"