#!/bin/bash

################################################################################
# Extended Analysis Functions
################################################################################
# Purpose: All 32 additional intelligence checks for website slowness
# Categories: WordPress, Database, PHP, Web Server, Cron/Tasks
################################################################################

# ============================================================================
# CATEGORY 1: WORDPRESS-SPECIFIC SETTINGS (8 checks)
# ============================================================================

### 1.1 - WP_DEBUG Enabled Check
analyze_wp_debug() {
    local docroot="$1"
    local wp_config="$docroot/wp-config.php"

    if [ ! -f "$wp_config" ]; then
        return 0
    fi

    local debug_enabled=$(grep -c "define.*'WP_DEBUG'.*true" "$wp_config" 2>/dev/null || echo 0)

    if [ "$debug_enabled" -gt 0 ]; then
        save_analysis_data "wordpress_settings.tmp" "CRITICAL: WP_DEBUG enabled in production"
        save_analysis_data "wordpress_settings.tmp" "  Impact: 10-15% performance penalty from error logging"
        save_analysis_data "wordpress_settings.tmp" "  Fix: Set define( 'WP_DEBUG', false );"
    fi
}

### 1.2 - XML-RPC Enabled Check
analyze_xmlrpc() {
    local domain="$1"

    local xmlrpc_test=$(curl -s -m 3 "https://$domain/xmlrpc.php" -w "%{http_code}" -o /dev/null 2>/dev/null || echo "000")

    if [ "$xmlrpc_test" != "403" ] && [ "$xmlrpc_test" != "404" ]; then
        save_analysis_data "wordpress_settings.tmp" "WARNING: XML-RPC is enabled"
        save_analysis_data "wordpress_settings.tmp" "  Security risk, unnecessary API exposure"
        save_analysis_data "wordpress_settings.tmp" "  Fix: Add to .htaccess or disable via plugin"
    fi
}

### 1.3 - WordPress Heartbeat API
analyze_heartbeat_api() {
    local docroot="$1"

    if [ -f "$docroot/wp-config.php" ]; then
        local heartbeat_interval=$(grep -o "HEARTBEAT_INTERVAL.*[0-9]*" "$docroot/wp-config.php" 2>/dev/null | grep -o "[0-9]*" | head -1)

        if [ -z "$heartbeat_interval" ]; then
            save_analysis_data "wordpress_settings.tmp" "INFO: Heartbeat running at default (15s) interval"
            save_analysis_data "wordpress_settings.tmp" "  Recommendation: Increase to 60s: define('HEARTBEAT_INTERVAL', 60);"
        elif [ "$heartbeat_interval" -lt 30 ]; then
            save_analysis_data "wordpress_settings.tmp" "WARNING: Heartbeat interval too frequent: ${heartbeat_interval}s"
            save_analysis_data "wordpress_settings.tmp" "  Recommend: At least 60 seconds"
        fi
    fi
}

### 1.4 - Autosave Frequency
analyze_autosave_frequency() {
    local docroot="$1"

    if [ -f "$docroot/wp-config.php" ]; then
        local autosave_interval=$(grep -o "AUTOSAVE_INTERVAL.*[0-9]*" "$docroot/wp-config.php" 2>/dev/null | grep -o "[0-9]*" | head -1)

        if [ -z "$autosave_interval" ] || [ "$autosave_interval" -lt 120 ]; then
            save_analysis_data "wordpress_settings.tmp" "INFO: Autosave frequency at default or too frequent"
            save_analysis_data "wordpress_settings.tmp" "  Recommendation: Set to 300s (5 min): define('AUTOSAVE_INTERVAL', 300);"
        fi
    fi
}

### 1.5 - REST API Exposure
analyze_rest_api_exposure() {
    local domain="$1"

    local rest_test=$(curl -s -m 3 "https://$domain/wp-json/wp/v2/posts" 2>/dev/null | head -c 50)

    if [[ "$rest_test" == *"ID"* ]] || [[ "$rest_test" == *"title"* ]]; then
        save_analysis_data "wordpress_settings.tmp" "INFO: REST API is fully exposed (public)"
        save_analysis_data "wordpress_settings.tmp" "  Consider: Require authentication or limit access"
    fi
}

### 1.6 - Emoji Support
analyze_emoji_scripts() {
    local domain="$1"

    local emoji_test=$(curl -s -m 5 "https://$domain" 2>/dev/null | grep -c "wp-emoji" || echo 0)

    if [ "$emoji_test" -gt 0 ]; then
        save_analysis_data "wordpress_settings.tmp" "INFO: Emoji support scripts loading"
        save_analysis_data "wordpress_settings.tmp" "  Unnecessary for most sites, adds 1-2 extra HTTP requests"
        save_analysis_data "wordpress_settings.tmp" "  Fix: Use disable-emojis plugin or add function to disable"
    fi
}

### 1.7 - Post Revision Distribution
analyze_post_revision_distribution() {
    local db_name="$1"

    if [ -z "$db_name" ]; then
        return 0
    fi

    local high_revision_posts=$(mysql -Ns -e "
        SELECT COUNT(DISTINCT post_parent) FROM ${db_name}.wp_posts
        WHERE post_type='revision'
        GROUP BY post_parent
        HAVING COUNT(*) > 50;
    " 2>/dev/null | wc -l || echo 0)

    if [ "$high_revision_posts" -gt 0 ]; then
        save_analysis_data "wordpress_settings.tmp" "WARNING: $high_revision_posts posts with >50 revisions each"
        save_analysis_data "wordpress_settings.tmp" "  These posts bloat the database"
        save_analysis_data "wordpress_settings.tmp" "  Fix: Delete old revisions: wp post delete \$(wp post list --format=ids --post_type=revision) --force"
    fi
}

### 1.8 - Pingbacks/Trackbacks
analyze_pingbacks_trackbacks() {
    local docroot="$1"

    if [ -f "$docroot/wp-config.php" ]; then
        # Check if default setting is configured
        if ! grep -q "default_ping_status" "$docroot/wp-config.php"; then
            save_analysis_data "wordpress_settings.tmp" "INFO: Pingbacks/trackbacks enabled (default)"
            save_analysis_data "wordpress_settings.tmp" "  Security & performance: Disable with wp option update default_ping_status 'closed'"
        fi
    fi
}

# ============================================================================
# CATEGORY 2: DATABASE TUNING (8 checks)
# ============================================================================

### 2.1 - InnoDB Buffer Pool Size
analyze_innodb_buffer_pool() {
    local buffer_pool=$(mysql -Ns -e "SELECT @@innodb_buffer_pool_size;" 2>/dev/null || echo "0")

    if [ "$buffer_pool" -lt 268435456 ]; then  # Less than 256MB
        save_analysis_data "database_tuning.tmp" "CRITICAL: InnoDB buffer pool too small"
        save_analysis_data "database_tuning.tmp" "  Current: $(( buffer_pool / 1024 / 1024 ))MB"
        save_analysis_data "database_tuning.tmp" "  Recommend: 256MB minimum, 50-75% of available RAM"
        save_analysis_data "database_tuning.tmp" "  Edit /etc/my.cnf: innodb_buffer_pool_size = 8G"
    fi
}

### 2.2 - Max Allowed Packet
analyze_max_allowed_packet() {
    local max_packet=$(mysql -Ns -e "SELECT @@max_allowed_packet;" 2>/dev/null || echo "0")

    if [ "$max_packet" -lt 16777216 ]; then  # Less than 16MB
        save_analysis_data "database_tuning.tmp" "WARNING: max_allowed_packet is small"
        save_analysis_data "database_tuning.tmp" "  Current: $(( max_packet / 1024 / 1024 ))MB"
        save_analysis_data "database_tuning.tmp" "  Large queries may fail silently"
        save_analysis_data "database_tuning.tmp" "  Edit /etc/my.cnf: max_allowed_packet = 256M"
    fi
}

### 2.3 - Slow Query Log Threshold
analyze_slow_query_threshold() {
    local long_query_time=$(mysql -Ns -e "SELECT @@long_query_time;" 2>/dev/null || echo "10")

    if (( $(echo "$long_query_time > 2" | bc -l) )); then
        save_analysis_data "database_tuning.tmp" "INFO: Slow query threshold is high (${long_query_time}s)"
        save_analysis_data "database_tuning.tmp" "  Misses real slow queries that take 1-2 seconds"
        save_analysis_data "database_tuning.tmp" "  Edit /etc/my.cnf: long_query_time = 1"
    fi
}

### 2.4 - InnoDB File Per Table
analyze_innodb_file_per_table() {
    local file_per_table=$(mysql -Ns -e "SELECT @@innodb_file_per_table;" 2>/dev/null || echo "0")

    if [ "$file_per_table" -eq 0 ]; then
        save_analysis_data "database_tuning.tmp" "WARNING: InnoDB file-per-table is disabled"
        save_analysis_data "database_tuning.tmp" "  All tables in single ibdata1 file (can grow huge)"
        save_analysis_data "database_tuning.tmp" "  Edit /etc/my.cnf: innodb_file_per_table = 1"
    fi
}

### 2.5 - Query Cache (MySQL 5.7)
analyze_query_cache() {
    local query_cache=$(mysql -Ns -e "SELECT @@version;" 2>/dev/null || echo "8.0")

    if [[ "$query_cache" == 5.7* ]]; then
        local cache_type=$(mysql -Ns -e "SELECT @@query_cache_type;" 2>/dev/null || echo "0")
        if [ "$cache_type" -gt 0 ]; then
            save_analysis_data "database_tuning.tmp" "INFO: Query cache enabled (MySQL 5.7)"
            save_analysis_data "database_tuning.tmp" "  Can be slow, consider disabling or upgrading to MySQL 8.0"
        fi
    fi
}

### 2.6 - Temporary Table Location
analyze_temp_table_location() {
    local tmp_table_size=$(mysql -Ns -e "SELECT @@tmp_table_size;" 2>/dev/null || echo "0")

    if [ "$tmp_table_size" -lt 33554432 ]; then  # Less than 32MB
        save_analysis_data "database_tuning.tmp" "INFO: Temporary table size is small"
        save_analysis_data "database_tuning.tmp" "  Current: $(( tmp_table_size / 1024 / 1024 ))MB"
        save_analysis_data "database_tuning.tmp" "  Large GROUP BY/DISTINCT queries go to disk"
        save_analysis_data "database_tuning.tmp" "  Edit /etc/my.cnf: tmp_table_size = 512M"
    fi
}

### 2.7 - Connection Timeout Settings
analyze_connection_timeout() {
    local wait_timeout=$(mysql -Ns -e "SELECT @@wait_timeout;" 2>/dev/null || echo "28800")

    if [ "$wait_timeout" -gt 3600 ]; then
        save_analysis_data "database_tuning.tmp" "INFO: wait_timeout is high (${wait_timeout}s)"
        save_analysis_data "database_tuning.tmp" "  May accumulate idle connections"
        save_analysis_data "database_tuning.tmp" "  For pooling: Set to 600 (10 min)"
    fi
}

### 2.8 - Innodb Flush Log at Trx Commit
analyze_innodb_flush_log() {
    local flush_log=$(mysql -Ns -e "SELECT @@innodb_flush_log_at_trx_commit;" 2>/dev/null || echo "1")

    if [ "$flush_log" -eq 1 ]; then
        save_analysis_data "database_tuning.tmp" "INFO: InnoDB flush log = 1 (safest but slowest)"
        save_analysis_data "database_tuning.tmp" "  Change to 2 for better performance with acceptable risk"
        save_analysis_data "database_tuning.tmp" "  Edit /etc/my.cnf: innodb_flush_log_at_trx_commit = 2"
    fi
}

# ============================================================================
# CATEGORY 3: PHP PERFORMANCE (6 checks)
# ============================================================================

### 3.1 - OPcache Configuration
analyze_opcache() {
    local opcache_enabled=$(php -r "echo extension_loaded('Zend OPcache') ? 1 : 0;" 2>/dev/null || echo 0)

    if [ "$opcache_enabled" -eq 0 ]; then
        save_analysis_data "php_tuning.tmp" "CRITICAL: OPcache not enabled"
        save_analysis_data "php_tuning.tmp" "  Impact: 2-3x slower PHP execution"
        save_analysis_data "php_tuning.tmp" "  Fix: Enable in php.ini with proper config"
    else
        local cache_size=$(php -i 2>/dev/null | grep "opcache.memory_consumption" | awk '{print $NF}' || echo "0")
        if [ "$cache_size" -lt 256 ]; then
            save_analysis_data "php_tuning.tmp" "WARNING: OPcache memory too small (${cache_size}MB)"
            save_analysis_data "php_tuning.tmp" "  Recommend: 256MB or more"
        fi
    fi
}

### 3.2 - Xdebug in Production
analyze_xdebug() {
    local xdebug_enabled=$(php -m 2>/dev/null | grep -c "Xdebug" || echo 0)

    if [ "$xdebug_enabled" -gt 0 ]; then
        save_analysis_data "php_tuning.tmp" "CRITICAL: Xdebug enabled in production!"
        save_analysis_data "php_tuning.tmp" "  Impact: 50-70% performance penalty"
        save_analysis_data "php_tuning.tmp" "  Fix: Disable or uninstall immediately"
    fi
}

### 3.3 - Realpath Cache
analyze_realpath_cache() {
    local realpath_size=$(php -i 2>/dev/null | grep "realpath_cache_size" | awk '{print $NF}' || echo "0")

    if [[ "$realpath_size" == "4K" ]] || [ "$realpath_size" = "4096" ]; then
        save_analysis_data "php_tuning.tmp" "INFO: Realpath cache is default (4K, too small)"
        save_analysis_data "php_tuning.tmp" "  For WordPress: Recommend 128M"
        save_analysis_data "php_tuning.tmp" "  Edit php.ini: realpath_cache_size = 128M"
    fi
}

### 3.4 - Timezone Configuration
analyze_timezone_config() {
    local php_tz=$(php -i 2>/dev/null | grep "date.timezone" | head -1 | awk '{print $NF}' || echo "UTC")

    if [ "$php_tz" = "no value" ] || [ -z "$php_tz" ]; then
        save_analysis_data "php_tuning.tmp" "INFO: Timezone not configured"
        save_analysis_data "php_tuning.tmp" "  Set to UTC or match site timezone"
        save_analysis_data "php_tuning.tmp" "  Edit php.ini: date.timezone = UTC"
    fi
}

### 3.5 - Display Errors in Production
analyze_display_errors() {
    local display_errors=$(php -i 2>/dev/null | grep "display_errors" | grep -o "On\|Off" | head -1 || echo "Off")

    if [ "$display_errors" = "On" ]; then
        save_analysis_data "php_tuning.tmp" "WARNING: display_errors is On in production"
        save_analysis_data "php_tuning.tmp" "  Security risk, performance penalty"
        save_analysis_data "php_tuning.tmp" "  Edit php.ini: display_errors = Off"
    fi
}

### 3.6 - Disabled Functions
analyze_disabled_functions() {
    local disabled=$(php -i 2>/dev/null | grep "disable_functions" | grep -o "[^ ].*" | head -1 || echo "none")

    if [ "$disabled" != "none" ] && [ -n "$disabled" ]; then
        save_analysis_data "php_tuning.tmp" "INFO: Functions disabled (security): $disabled"
        save_analysis_data "php_tuning.tmp" "  Verify these don't break any plugins"
    fi
}

# ============================================================================
# CATEGORY 4: WEB SERVER TUNING (6 checks)
# ============================================================================

### 4.1 - HTTP/2 Enabled
analyze_http2() {
    local http2_enabled=$(apache2ctl -M 2>/dev/null | grep -c "http2_module" || echo 0)

    if [ "$http2_enabled" -eq 0 ]; then
        save_analysis_data "web_server.tmp" "WARNING: HTTP/2 not enabled"
        save_analysis_data "web_server.tmp" "  Impact: 15-30% slower asset delivery"
        save_analysis_data "web_server.tmp" "  Fix: a2enmod http2 && systemctl restart apache2"
    fi
}

### 4.2 - KeepAlive Settings
analyze_keepalive() {
    local keepalive=$(grep -A 3 "^KeepAlive" /etc/apache2/apache2.conf 2>/dev/null | head -1 || echo "unknown")

    if [[ "$keepalive" == *"Off"* ]]; then
        save_analysis_data "web_server.tmp" "INFO: KeepAlive is disabled"
        save_analysis_data "web_server.tmp" "  Enable for better performance: KeepAlive On"
    fi
}

### 4.3 - Sendfile Enabled
analyze_sendfile() {
    local sendfile=$(grep -i "EnableSendfile" /etc/apache2/apache2.conf 2>/dev/null | grep -o "On\|Off" || echo "unknown")

    if [ "$sendfile" = "Off" ] || [ -z "$sendfile" ]; then
        save_analysis_data "web_server.tmp" "WARNING: Sendfile not enabled"
        save_analysis_data "web_server.tmp" "  10-20% faster static file delivery"
        save_analysis_data "web_server.tmp" "  Enable: EnableSendfile on"
    fi
}

### 4.4 - Gzip Compression Level
analyze_gzip_compression() {
    local gzip_level=$(grep -i "DeflateCompressionLevel" /etc/apache2/mods-enabled/deflate.conf 2>/dev/null | grep -o "[0-9]" || echo "6")

    if [ "$gzip_level" = "9" ]; then
        save_analysis_data "web_server.tmp" "INFO: Gzip compression level is 9 (maximum)"
        save_analysis_data "web_server.tmp" "  Too slow, not worth it. Recommend level 6"
        save_analysis_data "web_server.tmp" "  Edit deflate.conf: DeflateCompressionLevel 6"
    fi
}

### 4.5 - SSL/TLS Version
analyze_ssl_version() {
    local ssl_version=$(grep -i "SSLProtocol" /etc/apache2/sites-enabled/*ssl.conf 2>/dev/null | grep -o "TLSv[0-9.]*" | head -1 || echo "unknown")

    if [[ "$ssl_version" == *"1.0"* ]] || [[ "$ssl_version" == *"1.1"* ]]; then
        save_analysis_data "web_server.tmp" "WARNING: Using outdated SSL/TLS: $ssl_version"
        save_analysis_data "web_server.tmp" "  Recommend: TLS 1.2 minimum, TLS 1.3 preferred"
        save_analysis_data "web_server.tmp" "  Set: SSLProtocol TLSv1.2 TLSv1.3"
    fi
}

### 4.6 - Unused Apache Modules
analyze_apache_modules() {
    local module_count=$(apache2ctl -M 2>/dev/null | wc -l || echo "0")

    if [ "$module_count" -gt 45 ]; then
        save_analysis_data "web_server.tmp" "INFO: Many Apache modules loaded ($module_count)"
        save_analysis_data "web_server.tmp" "  Review and disable unused modules"
        save_analysis_data "web_server.tmp" "  Command: apache2ctl -M"
    fi
}

# ============================================================================
# CATEGORY 5: CRON & BACKGROUND TASKS (4 checks)
# ============================================================================

### 5.1 - WordPress Cron Execution
analyze_wordpress_cron() {
    local docroot="$1"

    if [ -f "$docroot/wp-config.php" ]; then
        local cron_disabled=$(grep -c "DISABLE_WP_CRON.*true" "$docroot/wp-config.php" 2>/dev/null || echo 0)

        if [ "$cron_disabled" -eq 0 ]; then
            save_analysis_data "cron_tasks.tmp" "INFO: Using wp-cron (runs every pageload)"
            save_analysis_data "cron_tasks.tmp" "  Better: Switch to system cron"
            save_analysis_data "cron_tasks.tmp" "  Set: define('DISABLE_WP_CRON', true);"
            save_analysis_data "cron_tasks.tmp" "  Then add system cron: */5 * * * * curl https://example.com/wp-cron.php"
        fi
    fi
}

### 5.2 - Backup Schedule
analyze_backup_schedule() {
    local backup_time=$(grep -i "backup" /etc/cron.d/cpanel* 2>/dev/null | grep -o "[0-9]*:[0-9]*" | head -1 || echo "unknown")

    if [ "$backup_time" != "unknown" ]; then
        # Extract hour
        local hour=${backup_time%%:*}
        if [ "$hour" -ge 7 ] && [ "$hour" -le 18 ]; then
            save_analysis_data "cron_tasks.tmp" "WARNING: Backup scheduled during peak hours ($hour:00)"
            save_analysis_data "cron_tasks.tmp" "  Move to 2-4 AM for off-peak"
        fi
    fi
}

### 5.3 - Database Optimization Schedule
analyze_db_optimization_schedule() {
    local has_optimize=$(grep -c "mysqlcheck\|optimize" /etc/cron.d/* 2>/dev/null || echo 0)

    if [ "$has_optimize" -eq 0 ]; then
        save_analysis_data "cron_tasks.tmp" "INFO: No database optimization scheduled"
        save_analysis_data "cron_tasks.tmp" "  Recommend weekly: mysqlcheck -Aou database_name"
        save_analysis_data "cron_tasks.tmp" "  Add to crontab: 0 3 * * 0 mysqlcheck -Aou -u root -p{pass}"
    fi
}

### 5.4 - Slow Cron Jobs
analyze_slow_cron_jobs() {
    # This requires WordPress and wp-cli to be available
    if command -v wp &> /dev/null; then
        local cron_jobs=$(wp cron schedule list 2>/dev/null | wc -l || echo "0")

        if [ "$cron_jobs" -gt 10 ]; then
            save_analysis_data "cron_tasks.tmp" "INFO: Many scheduled cron jobs ($cron_jobs)"
            save_analysis_data "cron_tasks.tmp" "  Review with: wp cron schedule list"
            save_analysis_data "cron_tasks.tmp" "  Disable unnecessary ones for better performance"
        fi
    fi
}

# ============================================================================
# ADDITIONAL HIGH-VALUE CHECKS
# ============================================================================

### Missing Critical Indexes
analyze_missing_critical_indexes() {
    local db_name="$1"

    if [ -z "$db_name" ]; then
        return 0
    fi

    # Check for common missing indexes
    local missing_indexes=""

    # Check wp_postmeta meta_key index
    local meta_key_index=$(mysql -Ns -e "
        SELECT COUNT(*) FROM information_schema.STATISTICS
        WHERE TABLE_SCHEMA='${db_name}'
        AND TABLE_NAME='wp_postmeta'
        AND COLUMN_NAME='meta_key';
    " 2>/dev/null || echo "0")

    if [ "$meta_key_index" -eq 0 ]; then
        missing_indexes+="wp_postmeta(meta_key) "
    fi

    # Check wp_posts post_type index
    local post_type_index=$(mysql -Ns -e "
        SELECT COUNT(*) FROM information_schema.STATISTICS
        WHERE TABLE_SCHEMA='${db_name}'
        AND TABLE_NAME='wp_posts'
        AND COLUMN_NAME='post_type';
    " 2>/dev/null || echo "0")

    if [ "$post_type_index" -eq 0 ]; then
        missing_indexes+="wp_posts(post_type) "
    fi

    if [ -n "$missing_indexes" ]; then
        save_analysis_data "database_tuning.tmp" "CRITICAL: Missing critical indexes"
        save_analysis_data "database_tuning.tmp" "  Missing: $missing_indexes"
        save_analysis_data "database_tuning.tmp" "  Add: ALTER TABLE table_name ADD INDEX (column_name);"
    fi
}

### Database to Memory Ratio
analyze_database_memory_ratio() {
    local db_name="$1"

    if [ -z "$db_name" ]; then
        return 0
    fi

    local db_size=$(mysql -Ns -e "
        SELECT ROUND(SUM(data_length + index_length) / 1024 / 1024)
        FROM information_schema.tables
        WHERE table_schema='${db_name}';
    " 2>/dev/null || echo "0")

    local system_memory=$(free -m | awk 'NR==2{print $2}')

    if [ "$db_size" -gt 0 ] && [ "$system_memory" -gt 0 ]; then
        if [ "$db_size" -gt "$system_memory" ]; then
            save_analysis_data "database_tuning.tmp" "CRITICAL: Database (${db_size}MB) larger than available RAM (${system_memory}MB)"
            save_analysis_data "database_tuning.tmp" "  This causes heavy disk I/O"
            save_analysis_data "database_tuning.tmp" "  Solutions: Increase RAM, optimize DB, or split database"
        fi
    fi
}

################################################################################
# EXPORT ALL FUNCTIONS
################################################################################

export -f analyze_wp_debug
export -f analyze_xmlrpc
export -f analyze_heartbeat_api
export -f analyze_autosave_frequency
export -f analyze_rest_api_exposure
export -f analyze_emoji_scripts
export -f analyze_post_revision_distribution
export -f analyze_pingbacks_trackbacks
export -f analyze_innodb_buffer_pool
export -f analyze_max_allowed_packet
export -f analyze_slow_query_threshold
export -f analyze_innodb_file_per_table
export -f analyze_query_cache
export -f analyze_temp_table_location
export -f analyze_connection_timeout
export -f analyze_innodb_flush_log
export -f analyze_opcache
export -f analyze_xdebug
export -f analyze_realpath_cache
export -f analyze_timezone_config
export -f analyze_display_errors
export -f analyze_disabled_functions
export -f analyze_http2
export -f analyze_keepalive
export -f analyze_sendfile
export -f analyze_gzip_compression
export -f analyze_ssl_version
export -f analyze_apache_modules
export -f analyze_wordpress_cron
################################################################################
# PHASE 4: ADVANCED DATABASE & ISSUE PATTERN DETECTION (Tier 1 Quick Wins)
################################################################################

# ============================================================================
# PHASE 4 DATABASE CHECKS (6 quick wins)
# ============================================================================

### P4.1 - Table Engine Mismatch
analyze_table_engine_mismatch() {
    # Check if tables use inconsistent storage engines
    local engines=$(mysql -e "SELECT DISTINCT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() 2>/dev/null" 2>/dev/null | grep -v ENGINE | sort -u)

    if [ ! -z "$engines" ] && [ "$(echo "$engines" | wc -l)" -gt 1 ]; then
        save_analysis_data "database_advanced.tmp" "WARNING: Mixed storage engines detected"
        save_analysis_data "database_advanced.tmp" "  Engines: $(echo $engines | tr '\n' ', ')"
        save_analysis_data "database_advanced.tmp" "  Recommendation: Convert all tables to InnoDB"
    fi
}

### P4.2 - Table Statistics Age
analyze_table_statistics_age() {
    # Check if table statistics are stale (MySQL 5.7+)
    local stale_count=$(mysql -e "SELECT COUNT(*) FROM mysql.innodb_table_stats WHERE STAT_MODIFIED < DATE_SUB(NOW(), INTERVAL 30 DAY)" 2>/dev/null | tail -1 || echo 0)

    if [ "$stale_count" -gt 0 ]; then
        save_analysis_data "database_advanced.tmp" "INFO: Found $stale_count tables with stale statistics"
        save_analysis_data "database_advanced.tmp" "  Fix: Run ANALYZE TABLE on each table"
        save_analysis_data "database_advanced.tmp" "  Or: wp db optimize (WordPress)"
    fi
}

### P4.3 - Index Cardinality Analysis
analyze_index_cardinality() {
    # Check for indexes with poor selectivity
    local poor_indexes=$(mysql -e "SELECT TABLE_NAME, INDEX_NAME FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND SEQ_IN_INDEX=1 AND CARDINALITY IS NOT NULL AND CARDINALITY / (SELECT TABLE_ROWS FROM information_schema.TABLES t WHERE t.TABLE_SCHEMA=information_schema.STATISTICS.TABLE_SCHEMA AND t.TABLE_NAME=information_schema.STATISTICS.TABLE_NAME) > 0.95 LIMIT 5" 2>/dev/null | grep -v TABLE_NAME | head -5)

    if [ ! -z "$poor_indexes" ]; then
        save_analysis_data "database_advanced.tmp" "WARNING: Found indexes with poor cardinality (high selectivity)"
        save_analysis_data "database_advanced.tmp" "  These indexes may not be used by optimizer"
        save_analysis_data "database_advanced.tmp" "  Review and consider dropping unused indexes"
    fi
}

### P4.4 - Query Cache Memory Waste
analyze_query_cache_memory_waste() {
    # Check query cache fragmentation (MySQL 5.7)
    local qcache_info=$(mysql -e "SHOW STATUS LIKE 'Qcache%'" 2>/dev/null)

    if echo "$qcache_info" | grep -q "Qcache_free_blocks"; then
        local free_blocks=$(echo "$qcache_info" | grep "Qcache_free_blocks" | awk '{print $2}')
        local total_blocks=$(echo "$qcache_info" | grep "Qcache_total_blocks" | awk '{print $2}')

        if [ "$total_blocks" -gt 0 ]; then
            local fragmentation=$((free_blocks * 100 / total_blocks))
            if [ "$fragmentation" -gt 30 ]; then
                save_analysis_data "database_advanced.tmp" "INFO: Query cache fragmentation at ${fragmentation}%"
                save_analysis_data "database_advanced.tmp" "  Consider: FLUSH QUERY CACHE; or redesign query strategy"
            fi
        fi
    fi
}

### P4.5 - Replication Lag Check
analyze_replication_lag() {
    # Check if database is replica and has lag
    local slave_status=$(mysql -e "SHOW SLAVE STATUS\G 2>/dev/null" | grep "Seconds_Behind_Master" | awk '{print $NF}')

    if [ ! -z "$slave_status" ] && [ "$slave_status" != "NULL" ] && [ "$slave_status" -gt 10 ]; then
        save_analysis_data "database_advanced.tmp" "WARNING: Database replication lag detected: ${slave_status} seconds"
        save_analysis_data "database_advanced.tmp" "  Impact: Read queries on replica are stale"
        save_analysis_data "database_advanced.tmp" "  Solution: Optimize master, increase replica resources"
    fi
}

### P4.6 - Table Size Growth Tracking
analyze_table_size_growth() {
    # Identify rapidly growing tables (potential logging tables)
    local large_tables=$(mysql -e "SELECT TABLE_NAME, ROUND(((data_length+index_length)/1024/1024),2) as size_mb FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() ORDER BY size_mb DESC LIMIT 5" 2>/dev/null | grep -v TABLE_NAME)

    if [ ! -z "$large_tables" ]; then
        local largest=$(echo "$large_tables" | head -1 | awk '{print $1, $2}')
        if echo "$largest" | awk '{exit ($2 > 1000) ? 0 : 1}'; then
            save_analysis_data "database_advanced.tmp" "WARNING: Large table detected: $largest MB"
            save_analysis_data "database_advanced.tmp" "  If this is wp_postmeta or wp_options, consider archiving old data"
        fi
    fi
}

# ============================================================================
# PHASE 4 SYSTEM & ERROR PATTERN CHECKS (6 quick wins)
# ============================================================================

### P4.7 - Timeout Error Detection
analyze_timeout_errors() {
    # Count timeout errors in recent logs
    local error_log=$(find /var/log -name "error.log" -o -name "php-fpm.log" 2>/dev/null | head -1)

    if [ -f "$error_log" ]; then
        local timeout_count=$(tail -1000 "$error_log" 2>/dev/null | grep -ci "timeout\|timed out" || echo 0)

        if [ "$timeout_count" -gt 10 ]; then
            save_analysis_data "error_patterns.tmp" "WARNING: Found $timeout_count timeout errors in recent logs"
            save_analysis_data "error_patterns.tmp" "  Impact: Customers experiencing connection/processing failures"
            save_analysis_data "error_patterns.tmp" "  Solutions: Increase timeouts, optimize code, add resources"
        fi
    fi
}

### P4.8 - Memory Exhaustion Attempts
analyze_memory_exhaustion_attempts() {
    # Detect PHP memory limit hits
    local error_log=$(find /var/log -name "error.log" -o -name "php-fpm.log" 2>/dev/null | head -1)

    if [ -f "$error_log" ]; then
        local memory_count=$(tail -1000 "$error_log" 2>/dev/null | grep -ci "allowed memory\|memory.*exhausted" || echo 0)

        if [ "$memory_count" -gt 0 ]; then
            save_analysis_data "error_patterns.tmp" "CRITICAL: PHP hitting memory limits ($memory_count times in logs)"
            save_analysis_data "error_patterns.tmp" "  Impact: Some requests failing with fatal error"
            save_analysis_data "error_patterns.tmp" "  Fix: Increase memory_limit in php.ini"
        fi
    fi
}

### P4.9 - Disk Inode Usage
analyze_disk_inode_usage() {
    # Check filesystem inode exhaustion (causes performance degradation)
    local inode_usage=$(df -i / 2>/dev/null | tail -1 | awk '{print int($5)}' || echo 0)

    if [ "$inode_usage" -gt 80 ]; then
        save_analysis_data "system_resources.tmp" "WARNING: Disk inode usage at ${inode_usage}%"
        save_analysis_data "system_resources.tmp" "  Impact: Filesystem performance degrades, may prevent new files"
        save_analysis_data "system_resources.tmp" "  Fix: Delete old logs, temporary files, or backups"
    fi
}

### P4.10 - Zombie Process Detection
analyze_zombie_processes() {
    # Count zombie/defunct processes
    local zombie_count=$(ps aux 2>/dev/null | grep -c " <defunct>" || echo 0)

    if [ "$zombie_count" -gt 5 ]; then
        save_analysis_data "system_resources.tmp" "WARNING: Found $zombie_count zombie processes"
        save_analysis_data "system_resources.tmp" "  Impact: Wastes process table entries, resource leak"
        save_analysis_data "system_resources.tmp" "  Fix: Restart PHP-FPM or MySQL to clean up"
    fi
}

### P4.11 - Swap Usage Detection (Critical)
analyze_swap_usage_phase4() {
    # Check if system is using swap (massive performance killer)
    local swap_used=$(free 2>/dev/null | grep Swap | awk '{print $3}' || echo 0)

    if [ "$swap_used" -gt 0 ]; then
        save_analysis_data "system_resources.tmp" "CRITICAL: System using swap ($swap_used KB)"
        save_analysis_data "system_resources.tmp" "  Impact: 50-100x SLOWER than RAM access"
        save_analysis_data "system_resources.tmp" "  Emergency: Upgrade RAM or reduce memory usage immediately"
    fi
}

### P4.12 - Load Average Trending
analyze_load_average_trend() {
    # Detect increasing load trend (early warning)
    local load_1=$(uptime 2>/dev/null | grep -oP 'load average: \K[^,]+' | head -1)
    local load_5=$(uptime 2>/dev/null | grep -oP 'load average: \K[^,]+' | tail -2 | head -1)

    if [ ! -z "$load_1" ] && [ ! -z "$load_5" ]; then
        local ratio=$(echo "scale=2; $load_1 / $load_5" | bc 2>/dev/null || echo 1)

        if (( $(echo "$ratio > 1.2" | bc -l) )); then
            save_analysis_data "system_resources.tmp" "INFO: Load average trending upward (1min: $load_1, 5min: $load_5)"
            save_analysis_data "system_resources.tmp" "  Early warning: Monitor for increasing problems"
        fi
    fi
}

################################################################################
# EXPORT ALL PHASE 4 FUNCTIONS
################################################################################

################################################################################
# PHASE 5: CONTENT & NETWORK OPTIMIZATION (Tier 1 Quick Wins)
################################################################################

# ============================================================================
# PHASE 5 CONTENT OPTIMIZATION CHECKS (10 checks)
# ============================================================================

### P5.1 - Unoptimized Images
analyze_unoptimized_images() {
    local docroot="$1"
    if [ ! -d "$docroot" ]; then return 0; fi

    # Count large images (>500KB) that aren't optimized
    local large_images=$(find "$docroot" -type f \( -name "*.jpg" -o -name "*.png" -o -name "*.gif" \) -size +500k 2>/dev/null | wc -l)

    if [ "$large_images" -gt 0 ]; then
        save_analysis_data "content_optimization.tmp" "WARNING: Found $large_images large unoptimized images (>500KB)"
        save_analysis_data "content_optimization.tmp" "  Recommendation: Convert to WebP or compress with ImageMagick"
        save_analysis_data "content_optimization.tmp" "  Impact: 30-50% reduction in transfer size"
    fi
}

### P5.2 - Missing WebP Format
analyze_webp_conversion() {
    local docroot="$1"
    if [ ! -d "$docroot" ]; then return 0; fi

    # Check if any WebP files exist (suggests conversion is happening)
    local webp_count=$(find "$docroot" -name "*.webp" 2>/dev/null | wc -l)
    local total_images=$(find "$docroot" -type f \( -name "*.jpg" -o -name "*.png" \) 2>/dev/null | wc -l)

    if [ "$total_images" -gt 10 ] && [ "$webp_count" -eq 0 ]; then
        save_analysis_data "content_optimization.tmp" "INFO: WebP conversion opportunity"
        save_analysis_data "content_optimization.tmp" "  Site has $total_images images but no WebP versions"
        save_analysis_data "content_optimization.tmp" "  Impact: 30-50% smaller files, better browser support"
    fi
}

### P5.3 - Large CSS/JS Files
analyze_large_assets() {
    local docroot="$1"
    if [ ! -d "$docroot" ]; then return 0; fi

    # Find large unminified CSS/JS files
    local large_css=$(find "$docroot" -name "*.css" -size +100k 2>/dev/null | wc -l)
    local large_js=$(find "$docroot" -name "*.js" -size +100k 2>/dev/null | wc -l)

    if [ "$large_css" -gt 0 ] || [ "$large_js" -gt 0 ]; then
        save_analysis_data "content_optimization.tmp" "WARNING: Large CSS ($large_css) or JS ($large_js) files"
        save_analysis_data "content_optimization.tmp" "  Recommendation: Minify or split into smaller chunks"
        save_analysis_data "content_optimization.tmp" "  Impact: 20-40% reduction in file size"
    fi
}

### P5.4 - Render-Blocking Resources
analyze_render_blocking() {
    local domain="$1"

    # Test for render-blocking resources in head
    local head_content=$(curl -s "https://$domain/" 2>/dev/null | sed -n '/<head>/,/<\/head>/p' | grep -c "<script\|<style" || echo 0)

    if [ "$head_content" -gt 5 ]; then
        save_analysis_data "content_optimization.tmp" "INFO: Multiple render-blocking resources in <head>"
        save_analysis_data "content_optimization.tmp" "  Found $head_content scripts/styles blocking page load"
        save_analysis_data "content_optimization.tmp" "  Recommendation: Move scripts to end of body, defer non-critical CSS"
    fi
}

### P5.5 - Font Loading Issues
analyze_font_loading() {
    local domain="$1"

    # Check for web fonts (may cause render delay)
    local fonts=$(curl -s "https://$domain/" 2>/dev/null | grep -c "@font-face\|fonts.googleapis\|fonts.gstatic" || echo 0)

    if [ "$fonts" -gt 0 ]; then
        save_analysis_data "content_optimization.tmp" "INFO: Web fonts detected ($fonts references)"
        save_analysis_data "content_optimization.tmp" "  Recommendation: Use font-display: swap to prevent invisible text"
        save_analysis_data "content_optimization.tmp" "  Impact: Faster perceived load time"
    fi
}

### P5.6 - HTTP Requests Count
analyze_request_count() {
    local domain="$1"

    # Count number of requests on homepage
    local request_count=$(curl -s -D - "https://$domain/" 2>/dev/null | grep -c "^HTTP" || echo 0)

    if [ "$request_count" -gt 80 ]; then
        save_analysis_data "content_optimization.tmp" "WARNING: High request count ($request_count requests)"
        save_analysis_data "content_optimization.tmp" "  Each request adds latency even with HTTP/2"
        save_analysis_data "content_optimization.tmp" "  Recommendation: Consolidate files, defer non-critical resources"
    fi
}

### P5.7 - Third-Party Scripts
analyze_third_party_scripts() {
    local domain="$1"

    # Count external scripts (ads, analytics, etc.)
    local external_scripts=$(curl -s "https://$domain/" 2>/dev/null | grep -o "src=['\"]https://[^'\"]*['\"]" | grep -cv "$(echo $domain | sed 's/www\.//')" || echo 0)

    if [ "$external_scripts" -gt 5 ]; then
        save_analysis_data "content_optimization.tmp" "INFO: Found $external_scripts third-party scripts"
        save_analysis_data "content_optimization.tmp" "  Impact: Slower page load (external dependencies)"
        save_analysis_data "content_optimization.tmp" "  Recommendation: Lazy load non-critical third-party scripts"
    fi
}

### P5.8 - Unused CSS/JavaScript
analyze_unused_assets() {
    local domain="$1"

    # Detect common signs of unused code
    local inline_styles=$(curl -s "https://$domain/" 2>/dev/null | grep -c "style=" || echo 0)

    if [ "$inline_styles" -gt 10 ]; then
        save_analysis_data "content_optimization.tmp" "INFO: Found $inline_styles inline styles"
        save_analysis_data "content_optimization.tmp" "  Recommendation: Move to external stylesheet for better caching"
    fi
}

### P5.9 - Content Delivery Optimization
analyze_content_delivery() {
    local domain="$1"

    # Check if responses are compressed
    local encoding=$(curl -s -I "https://$domain/" 2>/dev/null | grep -i "content-encoding" || echo "none")

    if echo "$encoding" | grep -iq "gzip\|deflate\|brotli"; then
        save_analysis_data "content_optimization.tmp" "✓ Content compression enabled: $encoding"
    else
        save_analysis_data "content_optimization.tmp" "WARNING: Content compression not detected"
        save_analysis_data "content_optimization.tmp" "  Impact: 30-50% larger responses"
        save_analysis_data "content_optimization.tmp" "  Fix: Enable gzip/brotli compression"
    fi
}

### P5.10 - Cache Headers Analysis
analyze_cache_headers() {
    local domain="$1"

    # Check cache control headers
    local cache_header=$(curl -s -I "https://$domain/" 2>/dev/null | grep -i "cache-control" || echo "none")

    if echo "$cache_header" | grep -iq "max-age=0\|no-cache\|no-store"; then
        save_analysis_data "content_optimization.tmp" "WARNING: Cache headers prevent caching"
        save_analysis_data "content_optimization.tmp" "  Current: $cache_header"
        save_analysis_data "content_optimization.tmp" "  Recommendation: Set Cache-Control: max-age=3600 (1 hour minimum)"
    fi
}

# ============================================================================
# PHASE 5 NETWORK & DNS CHECKS (8 checks)
# ============================================================================

### P5.11 - DNS Resolution Time
analyze_dns_resolution_time() {
    local domain="$1"

    # Measure DNS resolution time
    local dns_time=$(dig +stats "$domain" 2>/dev/null | grep "Query time:" | awk '{print $4}')

    if [ ! -z "$dns_time" ] && [ "$dns_time" -gt 100 ]; then
        save_analysis_data "network_optimization.tmp" "WARNING: Slow DNS resolution (${dns_time}ms)"
        save_analysis_data "network_optimization.tmp" "  Recommendation: Use faster DNS provider (1.1.1.1, 8.8.8.8)"
        save_analysis_data "network_optimization.tmp" "  Impact: Reduce by 50-100ms"
    fi
}

### P5.12 - DNS Records Configuration
analyze_dns_records() {
    local domain="$1"

    # Check for unnecessary DNS lookups
    local cname_count=$(dig +short CNAME "$domain" 2>/dev/null | wc -l)

    if [ "$cname_count" -gt 3 ]; then
        save_analysis_data "network_optimization.tmp" "INFO: Multiple CNAME records detected ($cname_count)"
        save_analysis_data "network_optimization.tmp" "  Each CNAME adds DNS lookup time"
        save_analysis_data "network_optimization.tmp" "  Recommendation: Minimize CNAME chains"
    fi
}

### P5.13 - Redirect Chain Length
analyze_redirect_chains() {
    local domain="$1"

    # Count redirects from http to https to final destination (with 10s timeout)
    local redirect_count=$(curl -s -m 10 -I -L "http://$domain/" 2>/dev/null | grep -c "HTTP/")

    if [ "$redirect_count" -gt 3 ]; then
        save_analysis_data "network_optimization.tmp" "WARNING: Long redirect chain ($redirect_count hops)"
        save_analysis_data "network_optimization.tmp" "  Impact: Each redirect adds 100-200ms"
        save_analysis_data "network_optimization.tmp" "  Recommendation: Point http directly to final destination"
    fi
}

### P5.14 - SSL/TLS Certificate Validity
analyze_ssl_certificate() {
    local domain="$1"

    # Check certificate expiration
    local expiry=$(echo | openssl s_client -servername "$domain" -connect "$domain:443" 2>/dev/null | openssl x509 -noout -dates 2>/dev/null | grep notAfter | cut -d= -f2)
    local days_left=$(( ($(date -d "$expiry" +%s) - $(date +%s)) / 86400 ))

    if [ "$days_left" -lt 30 ]; then
        save_analysis_data "network_optimization.tmp" "WARNING: SSL certificate expiring in $days_left days"
        save_analysis_data "network_optimization.tmp" "  Recommendation: Renew immediately to prevent outage"
    fi
}

### P5.15 - Connection Keep-Alive
analyze_connection_keepalive() {
    local domain="$1"

    # Check keep-alive header
    local keepalive=$(curl -s -I "https://$domain/" 2>/dev/null | grep -i "connection\|keep-alive" || echo "none")

    if ! echo "$keepalive" | grep -iq "keep-alive"; then
        save_analysis_data "network_optimization.tmp" "WARNING: Connection keep-alive not detected"
        save_analysis_data "network_optimization.tmp" "  Impact: Slower for multiple requests"
        save_analysis_data "network_optimization.tmp" "  Fix: Set Connection: keep-alive in server config"
    fi
}

### P5.16 - HTTP to HTTPS Redirect
analyze_https_redirect() {
    local domain="$1"

    # Check if http redirects to https
    local https_test=$(curl -s -m 10 -I "http://$domain/" 2>/dev/null | grep -c "301\|302\|308")

    if [ "$https_test" -eq 0 ]; then
        save_analysis_data "network_optimization.tmp" "WARNING: HTTP not redirecting to HTTPS"
        save_analysis_data "network_optimization.tmp" "  Security risk and performance impact"
        save_analysis_data "network_optimization.tmp" "  Recommendation: Add permanent redirect (301)"
    fi
}

### P5.17 - Network Waterfall Analysis
analyze_network_waterfall() {
    local domain="$1"

    # Simple check for overall response time
    local response_time=$(curl -s -m 10 -w "%{time_total}" -o /dev/null "https://$domain/" 2>/dev/null | cut -d. -f1)

    if [ "$response_time" -gt 3 ]; then
        save_analysis_data "network_optimization.tmp" "WARNING: Overall page load time ${response_time}+ seconds"
        save_analysis_data "network_optimization.tmp" "  Recommendation: Analyze full waterfall with browser DevTools"
        save_analysis_data "network_optimization.tmp" "  Check for slow DNS, connection, or server response"
    fi
}

### P5.18 - CDN Performance
analyze_cdn_performance() {
    local domain="$1"

    # Check if using CloudFlare, Cloudfront, or other CDN
    local cdn_header=$(curl -s -m 10 -I "https://$domain/" 2>/dev/null | grep -i "server:\|x-served-by\|x-cache" | head -1)

    if echo "$cdn_header" | grep -iq "cloudflare\|cloudfront\|akamai\|cdn"; then
        save_analysis_data "network_optimization.tmp" "✓ CDN detected: $cdn_header"
    else
        save_analysis_data "network_optimization.tmp" "INFO: No CDN detected"
        save_analysis_data "network_optimization.tmp" "  Recommendation: Implement CDN for 20-40% faster delivery"
    fi
}

################################################################################
# PHASE 6: FRAMEWORK-SPECIFIC DEEP DIVES (15 checks)
################################################################################

### P6.1 - Drupal Module Bloat
analyze_drupal_module_bloat() {
    local docroot="$1"

    if [ ! -f "$docroot/modules/node/node.module" ] && [ ! -f "$docroot/core/modules/node/node.module" ]; then
        return 0  # Not Drupal
    fi

    # Check if database query function is available
    if ! declare -f mysql_query_safe &>/dev/null; then
        return 0  # Cannot query database
    fi

    # Count enabled modules from database
    local module_count=$(echo "SELECT COUNT(*) FROM system WHERE type='module' AND status=1;" | mysql_query_safe 2>/dev/null | tail -1 | grep -o "^[0-9]*$")

    if [ -z "$module_count" ]; then
        return 0  # Query failed, skip this check
    fi

    if [ "$module_count" -gt 50 ]; then
        save_analysis_data "framework_deep_dive.tmp" "WARNING: Drupal has $module_count enabled modules (high)"
        save_analysis_data "framework_deep_dive.tmp" "  More modules = slower page load and more memory usage"
        save_analysis_data "framework_deep_dive.tmp" "  Recommendation: Disable unused modules via admin UI"
    fi
}

### P6.2 - Drupal Cache Configuration
analyze_drupal_cache_config() {
    local docroot="$1"

    if [ ! -f "$docroot/settings.php" ]; then
        return 0  # Not Drupal
    fi

    # Check cache backend configuration (case-insensitive)
    local has_redis=$(grep -ci "redis" "$docroot/settings.php" 2>/dev/null || echo 0)
    local has_memcache=$(grep -ci "memcache" "$docroot/settings.php" 2>/dev/null || echo 0)

    if [ "$has_redis" -eq 0 ] && [ "$has_memcache" -eq 0 ]; then
        save_analysis_data "framework_deep_dive.tmp" "INFO: Drupal using default database cache"
        save_analysis_data "framework_deep_dive.tmp" "  Recommendation: Implement Redis for 5-10x faster caching"
    fi
}

### P6.3 - Drupal Database Optimization
analyze_drupal_database_slow() {
    local docroot="$1"

    if [ ! -f "$docroot/settings.php" ]; then
        return 0  # Not Drupal
    fi

    # Check cache table size (can grow large without pruning)
    local cache_size=$(echo "SELECT SUM(DATA_LENGTH) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME LIKE 'cache%';" | mysql_query_safe 2>/dev/null | tail -1 || echo 0)

    if [ "$cache_size" -gt 104857600 ]; then  # 100MB
        save_analysis_data "framework_deep_dive.tmp" "WARNING: Drupal cache tables > 100MB"
        save_analysis_data "framework_deep_dive.tmp" "  Impact: Slow cache operations and memory usage"
        save_analysis_data "framework_deep_dive.tmp" "  Fix: Run 'drush cache-clear all' and configure cache expiry"
    fi
}

### P6.4 - Joomla Component Bloat
analyze_joomla_component_bloat() {
    local docroot="$1"

    if [ ! -f "$docroot/administrator/manifests/files/joomla.xml" ] && [ ! -d "$docroot/components" ]; then
        return 0  # Not Joomla
    fi

    # Count enabled components
    local component_count=$(ls "$docroot/components/" 2>/dev/null | wc -l)

    if [ "$component_count" -gt 30 ]; then
        save_analysis_data "framework_deep_dive.tmp" "WARNING: Joomla has $component_count components installed"
        save_analysis_data "framework_deep_dive.tmp" "  More components = more overhead and memory usage"
        save_analysis_data "framework_deep_dive.tmp" "  Recommendation: Uninstall unused components in admin"
    fi
}

### P6.5 - Joomla Cache Type
analyze_joomla_cache_type() {
    local docroot="$1"

    if [ ! -f "$docroot/configuration.php" ]; then
        return 0  # Not Joomla
    fi

    # Check if using file cache (slower) vs memcached
    local file_cache=$(grep -c "cacheHandler.*file" "$docroot/configuration.php" 2>/dev/null || echo 0)

    if [ "$file_cache" -gt 0 ]; then
        save_analysis_data "framework_deep_dive.tmp" "INFO: Joomla using file-based cache"
        save_analysis_data "framework_deep_dive.tmp" "  Slower than Redis/Memcached for high-traffic sites"
        save_analysis_data "framework_deep_dive.tmp" "  Recommendation: Switch to Redis for 3-5x improvement"
    fi
}

### P6.6 - Joomla Session Handler
analyze_joomla_session_bloat() {
    local docroot="$1"

    if [ ! -f "$docroot/configuration.php" ]; then
        return 0  # Not Joomla
    fi

    # Check session table size
    local session_size=$(echo "SELECT SUM(DATA_LENGTH) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='jos_session';" | mysql_query_safe 2>/dev/null | tail -1 || echo 0)

    if [ "$session_size" -gt 52428800 ]; then  # 50MB
        save_analysis_data "framework_deep_dive.tmp" "WARNING: Joomla session table > 50MB"
        save_analysis_data "framework_deep_dive.tmp" "  Impact: Slow session queries, large table scans"
        save_analysis_data "framework_deep_dive.tmp" "  Fix: Configure session garbage collection or implement cleanup"
    fi
}

### P6.7 - Magento Flat Catalog
analyze_magento_flat_catalog() {
    local docroot="$1"

    if [ ! -f "$docroot/app/etc/env.php" ] && [ ! -f "$docroot/app/etc/local.xml" ]; then
        return 0  # Not Magento
    fi

    # Check if flat catalog is enabled
    local flat_enabled=$(grep -c "flat.*=.*1\|use_flat.*true" "$docroot/app/etc/env.php" "$docroot/app/etc/local.xml" 2>/dev/null || echo 0)

    if [ "$flat_enabled" -eq 0 ]; then
        save_analysis_data "framework_deep_dive.tmp" "INFO: Magento flat catalog not enabled"
        save_analysis_data "framework_deep_dive.tmp" "  Impact: Much slower product queries (5-10x slower)"
        save_analysis_data "framework_deep_dive.tmp" "  Fix: Enable in admin: Stores > Settings > Configuration > Catalog > Frontend > Use Flat Catalog"
    fi
}

### P6.8 - Magento Indexing Status
analyze_magento_indexing() {
    local docroot="$1"

    if [ ! -f "$docroot/app/etc/env.php" ] && [ ! -f "$docroot/app/etc/local.xml" ]; then
        return 0  # Not Magento
    fi

    # Check indexer status (reindex_events table growth)
    local reindex_queue=$(echo "SELECT COUNT(*) FROM catalog_product_flat_0;" | mysql_query_safe 2>/dev/null | tail -1 || echo 0)

    if [ "$reindex_queue" -gt 100000 ]; then
        save_analysis_data "framework_deep_dive.tmp" "WARNING: Magento has $reindex_queue unprocessed index entries"
        save_analysis_data "framework_deep_dive.tmp" "  Impact: Slow product operations and search"
        save_analysis_data "framework_deep_dive.tmp" "  Fix: Run: php bin/magento indexer:reindex"
    fi
}

### P6.9 - Magento Log Tables
analyze_magento_log_tables() {
    local docroot="$1"

    if [ ! -f "$docroot/app/etc/env.php" ] && [ ! -f "$docroot/app/etc/local.xml" ]; then
        return 0  # Not Magento
    fi

    # Check log table sizes
    local log_size=$(echo "SELECT SUM(DATA_LENGTH) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME LIKE '%log';" | mysql_query_safe 2>/dev/null | tail -1 || echo 0)

    if [ "$log_size" -gt 524288000 ]; then  # 500MB
        save_analysis_data "framework_deep_dive.tmp" "WARNING: Magento log tables > 500MB"
        save_analysis_data "framework_deep_dive.tmp" "  Impact: Slow database operations, large backups"
        save_analysis_data "framework_deep_dive.tmp" "  Fix: Run: php bin/magento log:clean or disable logging"
    fi
}

### P6.10 - Magento Extensions Bloat
analyze_magento_extensions_bloat() {
    local docroot="$1"

    if [ ! -d "$docroot/app/code" ]; then
        return 0  # Not Magento
    fi

    # Count custom extensions (vendor directories), excluding root
    local ext_count=$(find "$docroot/app/code" -maxdepth 1 -mindepth 1 -type d 2>/dev/null | wc -l)

    if [ "$ext_count" -gt 50 ]; then
        save_analysis_data "framework_deep_dive.tmp" "WARNING: Magento has $ext_count vendor directories with extensions"
        save_analysis_data "framework_deep_dive.tmp" "  More extensions = slower page load and more memory"
        save_analysis_data "framework_deep_dive.tmp" "  Recommendation: Audit and disable unused extensions"
    fi
}

### P6.11 - Laravel Debug Mode
analyze_laravel_debug_mode() {
    local docroot="$1"

    if [ ! -f "$docroot/.env" ] && [ ! -f "$docroot/artisan" ]; then
        return 0  # Not Laravel
    fi

    # Check APP_DEBUG setting
    local debug_enabled=$(grep "APP_DEBUG=true" "$docroot/.env" 2>/dev/null | wc -l)

    if [ "$debug_enabled" -gt 0 ]; then
        save_analysis_data "framework_deep_dive.tmp" "CRITICAL: Laravel APP_DEBUG=true in production"
        save_analysis_data "framework_deep_dive.tmp" "  Impact: 30-50% performance penalty + security risk"
        save_analysis_data "framework_deep_dive.tmp" "  Fix: Set APP_DEBUG=false in .env and run cache:clear"
    fi
}

### P6.12 - Laravel Query Logging
analyze_laravel_query_logging() {
    local docroot="$1"

    if [ ! -f "$docroot/config/database.php" ]; then
        return 0  # Not Laravel
    fi

    # Check if query logging is enabled in config
    local query_log=$(grep -c "log.*=>.*true\|logging.*=>.*true" "$docroot/config/database.php" 2>/dev/null || echo 0)

    if [ "$query_log" -gt 0 ]; then
        save_analysis_data "framework_deep_dive.tmp" "WARNING: Laravel query logging enabled"
        save_analysis_data "framework_deep_dive.tmp" "  Impact: 5-10% performance penalty from logging"
        save_analysis_data "framework_deep_dive.tmp" "  Fix: Disable in config/database.php for production"
    fi
}

### P6.13 - Laravel Cache Driver
analyze_laravel_cache_driver() {
    local docroot="$1"

    if [ ! -f "$docroot/.env" ]; then
        return 0  # Not Laravel
    fi

    # Check cache driver (get first match, trim whitespace and quotes)
    local cache_driver=$(grep -m 1 "^CACHE_DRIVER=" "$docroot/.env" 2>/dev/null | cut -d= -f2- | xargs | tr -d '"'"'"'')

    if [ "$cache_driver" = "file" ] || [ -z "$cache_driver" ]; then
        save_analysis_data "framework_deep_dive.tmp" "INFO: Laravel using file cache (slower)"
        save_analysis_data "framework_deep_dive.tmp" "  Recommendation: Switch to Redis or Memcached"
        save_analysis_data "framework_deep_dive.tmp" "  Expected improvement: 5-10x faster caching"
    fi
}

### P6.14 - Laravel Vendor Size
analyze_laravel_app_size() {
    local docroot="$1"

    if [ ! -d "$docroot/vendor" ]; then
        return 0  # Not Laravel
    fi

    # Check vendor directory size (alert if >= 500MB)
    local vendor_output=$(du -sh "$docroot/vendor" 2>/dev/null)
    if echo "$vendor_output" | grep -qE "^[5-9][0-9]{2,}M|^[0-9.]+G"; then
        # Either >= 500M or >= 1G
        save_analysis_data "framework_deep_dive.tmp" "INFO: Laravel vendor > 500MB (large dependencies)"
        save_analysis_data "framework_deep_dive.tmp" "  Impacts: Deployment time, autoloader performance"
        save_analysis_data "framework_deep_dive.tmp" "  Size: $vendor_output"
        save_analysis_data "framework_deep_dive.tmp" "  Review: composer require --dev packages that aren't needed"
    fi
}

### P6.15 - Custom Framework Detection
analyze_custom_framework_detection() {
    local docroot="$1"

    # This is a catch-all for custom frameworks not covered by Phase 6
    if [ ! -f "$docroot/composer.json" ]; then
        return 0
    fi

    # Check for excessive config files (>50 is genuinely unusual)
    # Most frameworks have 5-20 config files naturally
    local config_files=$(find "$docroot" -maxdepth 3 -name "*config*" -type f 2>/dev/null | wc -l)

    if [ "$config_files" -gt 50 ]; then
        save_analysis_data "framework_deep_dive.tmp" "INFO: Custom framework with $config_files config files (unusually high)"
        save_analysis_data "framework_deep_dive.tmp" "  Recommendation: Review application structure for optimization opportunities"
        save_analysis_data "framework_deep_dive.tmp" "  Check for duplicate or unused configuration files"
    fi
}

################################################################################
# PHASE 6: SYSTEM-LEVEL DEEP DIVES (7 checks)
################################################################################

### P6.16 - System Entropy
analyze_system_entropy() {
    local entropy=$(cat /proc/sys/kernel/random/entropy_avail 2>/dev/null || echo 0)

    if [ "$entropy" -lt 1000 ]; then
        save_analysis_data "system_deep_dive.tmp" "WARNING: System entropy low ($entropy bits)"
        save_analysis_data "system_deep_dive.tmp" "  Impact: Slow cryptographic operations, SSL/TLS handshakes slow"
        save_analysis_data "system_deep_dive.tmp" "  Fix: Install haveged or rng-tools for entropy generation"
    fi
}

### P6.17 - I/O Scheduler
analyze_io_scheduler() {
    # Check common block devices (not just sda)
    local slow_scheduler_found=0
    local device_checked=""

    for device in sda sdb nvme0n1 nvme0n2 nvme1n1 vda vdb xvda xvdb; do
        if [ -f "/sys/block/$device/queue/scheduler" ]; then
            local scheduler=$(cat "/sys/block/$device/queue/scheduler" 2>/dev/null | grep -o "\[.*\]" | tr -d '[]')
            if [ "$scheduler" = "deadline" ] || [ "$scheduler" = "cfq" ]; then
                slow_scheduler_found=1
                device_checked="$device"
                break
            fi
        fi
    done

    if [ "$slow_scheduler_found" -eq 1 ]; then
        save_analysis_data "system_deep_dive.tmp" "INFO: I/O scheduler is $scheduler on $device_checked (older, slower)"
        save_analysis_data "system_deep_dive.tmp" "  Recommendation: Switch to 'mq-deadline': echo mq-deadline > /sys/block/$device_checked/queue/scheduler"
        save_analysis_data "system_deep_dive.tmp" "  Expected improvement: 10-20% for disk-heavy operations"
    fi
}

### P6.18 - Process Limits
analyze_process_limits() {
    local max_processes=$(cat /proc/sys/kernel/pid_max 2>/dev/null || echo 0)
    # Count processes excluding header line (wc -l counts header as 1)
    local used_processes=$(($(ps aux | wc -l) - 1))

    if [ "$used_processes" -gt "$((max_processes / 2))" ]; then
        save_analysis_data "system_deep_dive.tmp" "WARNING: Process table near limit ($used_processes/$max_processes)"
        save_analysis_data "system_deep_dive.tmp" "  Impact: Cannot spawn new processes, application hangs"
        save_analysis_data "system_deep_dive.tmp" "  Current: $used_processes processes (max: $max_processes)"
        save_analysis_data "system_deep_dive.tmp" "  Fix: Kill zombie processes or increase pid_max in sysctl.conf"
    fi
}

### P6.19 - Swap I/O Performance
analyze_swap_io_performance() {
    # Check if any swap is being used
    local swap_usage=$(free | grep Swap | awk '{print $3}')

    if [ "$swap_usage" -gt 0 ]; then
        # Get swap in/out rates from vmstat (third sample after 2 seconds)
        local vmstat_output=$(vmstat 1 3 2>/dev/null | tail -1)

        # vmstat shows: si (pages swapped in), so (pages swapped out)
        # These are in pages, typically columns 7 and 8
        local swap_in=$(echo "$vmstat_output" | awk '{print $7}')
        local swap_out=$(echo "$vmstat_output" | awk '{print $8}')

        if [ "$swap_in" -gt 50 ] || [ "$swap_out" -gt 50 ]; then
            save_analysis_data "system_deep_dive.tmp" "CRITICAL: Heavy swap I/O detected (${swap_in}pgs/s in, ${swap_out}pgs/s out)"
            save_analysis_data "system_deep_dive.tmp" "  Impact: 50-100x slower than RAM, killing performance"
            save_analysis_data "system_deep_dive.tmp" "  Fix: Upgrade RAM immediately or reduce memory footprint"
            save_analysis_data "system_deep_dive.tmp" "  Verify: vmstat 1 3 | tail -1"
        fi
    fi
}

### P6.20 - Network Socket Limits
analyze_network_socket_limits() {
    local max_connections=$(cat /proc/sys/net/core/somaxconn 2>/dev/null || echo 0)
    local current_connections=$(netstat -an 2>/dev/null | grep ESTABLISHED | wc -l)

    if [ "$current_connections" -gt "$((max_connections / 2))" ]; then
        save_analysis_data "system_deep_dive.tmp" "WARNING: Connection backlog limit near capacity ($current_connections/$max_connections)"
        save_analysis_data "system_deep_dive.tmp" "  Impact: Dropped connections, timeouts for users"
        save_analysis_data "system_deep_dive.tmp" "  Fix: Increase somaxconn in /etc/sysctl.conf to 4096+"
    fi
}

### P6.21 - Filesystem Inode Exhaustion
analyze_filesystem_inodes() {
    local inode_usage=$(df -i / | awk 'NR==2 {print $5}' | tr -d '%')

    if [ "$inode_usage" -gt 80 ]; then
        save_analysis_data "system_deep_dive.tmp" "WARNING: Filesystem inode usage ${inode_usage}%"
        save_analysis_data "system_deep_dive.tmp" "  Impact: Cannot create new files even if space available"
        save_analysis_data "system_deep_dive.tmp" "  Fix: Find and delete small files: find / -type f -size -1k 2>/dev/null | head -1000 | xargs rm"
    fi
}

### P6.22 - System Load Baseline
analyze_system_load_baseline() {
    local loadavg=$(cat /proc/loadavg | awk '{print $1}')
    local cpu_count=$(nproc)

    # Compare load avg per CPU - alert if > 2.0
    local load_ratio_check=$(echo "scale=2; $loadavg > 2.0 * $cpu_count" | bc)

    if [ "$load_ratio_check" -eq 1 ]; then
        local load_ratio=$(echo "scale=2; $loadavg / $cpu_count" | bc)
        save_analysis_data "system_deep_dive.tmp" "WARNING: System load average high (ratio: $load_ratio per CPU)"
        save_analysis_data "system_deep_dive.tmp" "  Over 1.0 per CPU means processes waiting for CPU"
        save_analysis_data "system_deep_dive.tmp" "  Current: $loadavg on $cpu_count CPUs"
        save_analysis_data "system_deep_dive.tmp" "  Recommendation: Identify slow processes with: ps aux --sort=-%cpu | head"
    fi
}

################################################################################
# EXPORT ALL FUNCTIONS
################################################################################

export -f analyze_backup_schedule
export -f analyze_db_optimization_schedule
export -f analyze_slow_cron_jobs
export -f analyze_missing_critical_indexes
export -f analyze_database_memory_ratio
export -f analyze_table_engine_mismatch
export -f analyze_table_statistics_age
export -f analyze_index_cardinality
export -f analyze_query_cache_memory_waste
export -f analyze_replication_lag
export -f analyze_table_size_growth
export -f analyze_timeout_errors
export -f analyze_memory_exhaustion_attempts
export -f analyze_disk_inode_usage
export -f analyze_zombie_processes
export -f analyze_swap_usage_phase4
export -f analyze_load_average_trend
export -f analyze_unoptimized_images
export -f analyze_webp_conversion
export -f analyze_large_assets
export -f analyze_render_blocking
export -f analyze_font_loading
export -f analyze_request_count
export -f analyze_third_party_scripts
export -f analyze_unused_assets
export -f analyze_content_delivery
export -f analyze_cache_headers
export -f analyze_dns_resolution_time
export -f analyze_dns_records
export -f analyze_redirect_chains
export -f analyze_ssl_certificate
export -f analyze_connection_keepalive
export -f analyze_https_redirect
export -f analyze_network_waterfall
export -f analyze_cdn_performance
export -f analyze_drupal_module_bloat
export -f analyze_drupal_cache_config
export -f analyze_drupal_database_slow
export -f analyze_joomla_component_bloat
export -f analyze_joomla_cache_type
export -f analyze_joomla_session_bloat
export -f analyze_magento_flat_catalog
export -f analyze_magento_indexing
export -f analyze_magento_log_tables
export -f analyze_magento_extensions_bloat
export -f analyze_laravel_debug_mode
export -f analyze_laravel_query_logging
export -f analyze_laravel_cache_driver
export -f analyze_laravel_app_size
export -f analyze_custom_framework_detection
export -f analyze_system_entropy
export -f analyze_io_scheduler
export -f analyze_process_limits
export -f analyze_swap_io_performance
export -f analyze_network_socket_limits
export -f analyze_filesystem_inodes
export -f analyze_system_load_baseline
