#!/bin/bash

# WordPress Plugin Validation Script
# This script validates the PHP syntax and structure of the PC Announcements plugin

echo "🔍 Validating PC Announcements Plugin..."
echo "========================================"

PLUGIN_DIR="/home/web/data/apps/c7f9e5c6-e7c2-4258-a583-ccffcf9791c8/announcements-v274"

# Check if plugin directory exists
if [ ! -d "$PLUGIN_DIR" ]; then
    echo "❌ Plugin directory not found: $PLUGIN_DIR"
    exit 1
fi

# Navigate to plugin directory
cd "$PLUGIN_DIR" || exit 1

echo "✅ Plugin directory found: $PLUGIN_DIR"

# Check main plugin file
echo ""
echo "📝 Checking main plugin file..."
MAIN_FILE="pc-announcements-274.php"
if [ ! -f "$MAIN_FILE" ]; then
    echo "❌ Main plugin file not found: $MAIN_FILE"
    exit 1
fi

echo "✅ Main plugin file found: $MAIN_FILE"

# Check PHP syntax for main file
echo ""
echo "🔍 Checking PHP syntax for main file..."
if php -l "$MAIN_FILE"; then
    echo "✅ Main plugin file has valid PHP syntax"
else
    echo "❌ Main plugin file has PHP syntax errors"
    exit 1
fi

# Check all PHP files for syntax errors
echo ""
echo "🔍 Checking all PHP files for syntax errors..."
PHP_FILES=$(find . -name "*.php" -type f)

if [ -z "$PHP_FILES" ]; then
    echo "❌ No PHP files found"
    exit 1
fi

echo "Found PHP files:"
echo "$PHP_FILES"
echo ""

SYNTAX_ERRORS=0
for file in $PHP_FILES; do
    echo "Checking: $file"
    if php -l "$file"; then
        echo "✅ Valid syntax"
    else
        echo "❌ Syntax errors found"
        SYNTAX_ERRORS=$((SYNTAX_ERRORS + 1))
    fi
    echo ""
done

if [ $SYNTAX_ERRORS -eq 0 ]; then
    echo "✅ All PHP files have valid syntax"
else
    echo "❌ Found $SYNTAX_ERRORS PHP files with syntax errors"
    exit 1
fi

# Check plugin header
echo ""
echo "🔍 Checking plugin header..."
HEADER_CHECK=$(grep -q "Plugin Name:" "$MAIN_FILE" && grep -q "Plugin URI:" "$MAIN_FILE" && grep -q "Version:" "$MAIN_FILE" && grep -q "Requires PHP:" "$MAIN_FILE" && echo "✅ Plugin header found" || echo "❌ Plugin header incomplete")

if [[ "$HEADER_CHECK" == *"❌"* ]]; then
    echo "❌ Plugin header is incomplete"
    exit 1
else
    echo "$HEADER_CHECK"
fi

# Check required files and directories
echo ""
echo "🔍 Checking required files and directories..."
REQUIRED_FILES=(
    "includes/class-install.php"
    "admin/class-admin.php"
    "public/class-frontend.php"
    "uninstall.php"
    "admin/templates/list-page.php"
    "admin/templates/edit-page.php"
    "admin/templates/error-page.php"
)

MISSING_FILES=0
for file in "${REQUIRED_FILES[@]}"; do
    if [ -f "$file" ]; then
        echo "✅ Found: $file"
    else
        echo "❌ Missing: $file"
        MISSING_FILES=$((MISSING_FILES + 1))
    fi
done

if [ $MISSING_FILES -eq 0 ]; then
    echo "✅ All required files found"
else
    echo "❌ Missing $MISSING_FILES required files"
    exit 1
fi

# Check for WordPress coding standards compliance
echo ""
echo "🔍 Checking WordPress coding standards..."
STANDARD_CHECK=$(grep -q "defined('ABSPATH')" "$MAIN_FILE" && echo "✅ ABSPATH check found" || echo "❌ ABSPATH check missing")

if [[ "$STANDARD_CHECK" == *"❌"* ]]; then
    echo "❌ WordPress coding standards not met"
    exit 1
else
    echo "$STANDARD_CHECK"
fi

# Check for security measures
echo ""
echo "🔍 Checking security measures..."
NONCE_CHECK=$(grep -r "wp_verify_nonce\|check_ajax_referer" . --include="*.php" | grep -v "grep" | head -1)

if [ -n "$NONCE_CHECK" ]; then
    echo "✅ Nonce validation found in plugin files"
else
    echo "❌ Nonce validation missing"
    exit 1
fi

# Check for proper file permissions
echo ""
echo "🔍 Checking file permissions..."
PERMISSION_CHECK=$(ls -la "$MAIN_FILE" | grep -q "\-rw\-" && echo "✅ Main file has proper permissions" || echo "❌ Main file has incorrect permissions")

if [[ "$PERMISSION_CHECK" == *"❌"* ]]; then
    echo "⚠️  Warning: File permissions may need adjustment"
else
    echo "$PERMISSION_CHECK"
fi

echo ""
echo "🎉 Plugin validation completed successfully!"
echo "============================================="
echo "✅ All PHP files have valid syntax"
echo "✅ Plugin header is complete"
echo "✅ All required files are present"
echo "✅ WordPress coding standards are met"
echo "✅ Security measures are in place"
echo ""
echo "The PC Announcements 274 plugin is ready for installation!"
