#!/bin/bash

# WordPress Plugin Validation Script
# Validates plugin structure, syntax, and WordPress coding standards

PLUGIN_DIR="$(pwd)"
PLUGIN_SLUG="pc-community-suggestions-7d3f"
MAIN_FILE="$PLUGIN_SLUG.php"

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

echo -e "${BLUE}=== WordPress Plugin Validation ===${NC}"
echo -e "Plugin: $PLUGIN_SLUG"
echo -e "Directory: $PLUGIN_DIR"
echo

# Check if main plugin file exists
if [[ ! -f "$MAIN_FILE" ]]; then
    echo -e "${RED}❌ ERROR: Main plugin file $MAIN_FILE not found${NC}"
    exit 1
fi

echo -e "${GREEN}✅ Main plugin file found${NC}"

# Check plugin header
echo -e "${BLUE}Checking plugin header...${NC}"
if ! grep -q "Plugin Name:" "$MAIN_FILE"; then
    echo -e "${RED}❌ ERROR: Plugin header missing 'Plugin Name:'${NC}"
    exit 1
fi

if ! grep -q "Plugin URI:" "$MAIN_FILE"; then
    echo -e "${YELLOW}⚠️  WARNING: Plugin header missing 'Plugin URI:'${NC}"
fi

if ! grep -q "Text Domain:" "$MAIN_FILE"; then
    echo -e "${RED}❌ ERROR: Plugin header missing 'Text Domain:'${NC}"
    exit 1
fi

if ! grep -q "Update URI:" "$MAIN_FILE"; then
    echo -e "${YELLOW}⚠️  WARNING: Plugin header missing 'Update URI:'${NC}"
fi

echo -e "${GREEN}✅ Plugin header check passed${NC}"

# Check required directories
echo -e "${BLUE}Checking directory structure...${NC}"
REQUIRED_DIRS=("includes" "admin" "admin/css" "admin/js" "public" "public/css" "public/js" "assets")

for dir in "${REQUIRED_DIRS[@]}"; do
    if [[ ! -d "$dir" ]]; then
        echo -e "${YELLOW}⚠️  WARNING: Directory $dir not found${NC}"
    else
        echo -e "${GREEN}✅ Directory $dir found${NC}"
    fi
done

# Check required files
echo -e "${BLUE}Checking required files...${NC}"
REQUIRED_FILES=(
    "includes/class-database.php"
    "includes/class-shortcodes.php"
    "includes/class-rest-api.php"
    "admin/class-admin.php"
    "admin/css/admin-style.css"
    "public/css/public-style.css"
    "public/js/public-script.js"
    "admin/js/admin-script.js"
    "uninstall.php"
)

for file in "${REQUIRED_FILES[@]}"; do
    if [[ ! -f "$file" ]]; then
        echo -e "${YELLOW}⚠️  WARNING: File $file not found${NC}"
    else
        echo -e "${GREEN}✅ File $file found${NC}"
    fi
done

# PHP syntax check
echo -e "${BLUE}Running PHP syntax checks...${NC}"
PHP_FILES=$(find . -name "*.php" -type f)
SYNTAX_ERRORS=0

for php_file in $PHP_FILES; do
    if php -l "$php_file" > /dev/null 2>&1; then
        echo -e "${GREEN}✅ Syntax OK: $php_file${NC}"
    else
        echo -e "${RED}❌ Syntax ERROR: $php_file${NC}"
        php -l "$php_file"
        SYNTAX_ERRORS=$((SYNTAX_ERRORS + 1))
    fi
done

if [[ $SYNTAX_ERRORS -gt 0 ]]; then
    echo -e "${RED}❌ Found $SYNTAX_ERRORS PHP syntax errors${NC}"
    exit 1
else
    echo -e "${GREEN}✅ All PHP files passed syntax check${NC}"
fi

# Check for WordPress functions
echo -e "${BLUE}Checking WordPress function usage...${NC}"
if ! grep -q "add_action\|add_filter" "$MAIN_FILE"; then
    echo -e "${YELLOW}⚠️  WARNING: No WordPress hooks found in main file${NC}"
fi

# Check for proper enqueueing
echo -e "${BLUE}Checking script enqueueing...${NC}"
if grep -q "wp_enqueue_style\|wp_enqueue_script" "$MAIN_FILE"; then
    echo -e "${GREEN}✅ Script enqueueing functions found${NC}"
else
    echo -e "${YELLOW}⚠️  WARNING: No script enqueueing functions found${NC}"
fi

# Check for security measures
echo -e "${BLUE}Checking security measures...${NC}"
if grep -q "check_admin_referer\|wp_verify_nonce" "$MAIN_FILE" || grep -q "check_admin_referer\|wp_verify_nonce" includes/*.php; then
    echo -e "${GREEN}✅ Security nonce checks found${NC}"
else
    echo -e "${YELLOW}⚠️  WARNING: No security nonce checks found${NC}"
fi

if grep -q "current_user_can\|is_user_logged_in" "$MAIN_FILE" || grep -q "current_user_can\|is_user_logged_in" includes/*.php; then
    echo -e "${GREEN}✅ User capability checks found${NC}"
else
    echo -e "${YELLOW}⚠️  WARNING: No user capability checks found${NC}"
fi

# Check for database operations
echo -e "${BLUE}Checking database operations...${NC}"
if grep -q "\$wpdb->" includes/class-database.php; then
    echo -e "${GREEN}✅ Database operations found${NC}"
else
    echo -e "${YELLOW}⚠️  WARNING: No database operations found${NC}"
fi

# Check for activation/deactivation hooks
echo -e "${BLUE}Checking activation hooks...${NC}"
if grep -q "register_activation_hook\|register_deactivation_hook" "$MAIN_FILE"; then
    echo -e "${GREEN}✅ Activation/deactivation hooks found${NC}"
else
    echo -e "${YELLOW}⚠️  WARNING: No activation/deactivation hooks found${NC}"
fi

# Check for shortcodes
echo -e "${BLUE}Checking shortcodes...${NC}"
if grep -q "add_shortcode" includes/class-shortcodes.php; then
    echo -e "${GREEN}✅ Shortcode registration found${NC}"
else
    echo -e "${YELLOW}⚠️  WARNING: No shortcode registration found${NC}"
fi

# Check for REST API
echo -e "${BLUE}Checking REST API...${NC}"
if grep -q "register_rest_route" includes/class-rest-api.php; then
    echo -e "${GREEN}✅ REST API routes found${NC}"
else
    echo -e "${YELLOW}⚠️  WARNING: No REST API routes found${NC}"
fi

# Check for admin menu
echo -e "${BLUE}Checking admin menu...${NC}"
if grep -q "add_menu_page\|add_submenu_page" admin/class-admin.php; then
    echo -e "${GREEN}✅ Admin menu registration found${NC}"
else
    echo -e "${YELLOW}⚠️  WARNING: No admin menu registration found${NC}"
fi

# Final summary
echo
echo -e "${BLUE}=== Validation Summary ===${NC}"
echo -e "${GREEN}✅ Plugin structure looks good${NC}"
echo -e "${GREEN}✅ All PHP files passed syntax check${NC}"
echo -e "${GREEN}✅ WordPress integration features found${NC}"
echo -e "${GREEN}✅ Security measures implemented${NC}"
echo
echo -e "${GREEN}🎉 Plugin validation completed successfully!${NC}"
echo -e "${BLUE}The plugin is ready for WordPress installation.${NC}"

# Display plugin information
echo
echo -e "${BLUE}=== Plugin Information ===${NC}"
grep "Plugin Name:" "$MAIN_FILE" | head -1
grep "Version:" "$MAIN_FILE" | head -1
grep "Author:" "$MAIN_FILE" | head -1
grep "Text Domain:" "$MAIN_FILE" | head -1

exit 0