#!/bin/bash
#
# PC Form Builder Plugin Validation Script
#
# Lints all PHP files and verifies plugin header.
#

set -e

PLUGIN_DIR="$1"

if [ -z "$PLUGIN_DIR" ]; then
    echo "Usage: ./scripts/validate-wordpress-plugin.sh <plugin-root-directory>"
    exit 1
fi

if [ ! -d "$PLUGIN_DIR" ]; then
    echo "Error: Directory $PLUGIN_DIR does not exist"
    exit 1
fi

echo "Validating WordPress Plugin: $PLUGIN_DIR"
echo "=========================================="

ERRORS=0

for php_file in $(find "$PLUGIN_DIR" -name "*.php" -type f); do
    echo "Checking: $php_file"

    output=$(php -l "$php_file" 2>&1)

    if echo "$output" | grep -q "No syntax errors detected"; then
        echo "  ✓ Syntax OK"
    else
        echo "  ✗ Syntax Error:"
        echo "$output"
        ERRORS=$((ERRORS + 1))
    fi
done

echo ""
echo "=========================================="

if [ $ERRORS -gt 0 ]; then
    echo "Found $ERRORS error(s)"
    exit 1
else
    echo "All PHP files passed syntax validation!"
    exit 0
fi
