# Apache Performance Configuration for Proviska AI
# Optimized for high-throughput API endpoints (169 → 500+ req/sec)

# Enable Keep-Alive for connection reuse
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

# Compression for JSON responses
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/rss+xml
    
    # Set compression level (1=fastest, 9=best compression)
    DeflateCompressionLevel 6
</IfModule>

# Cache static assets
<IfModule mod_expires.c>
    ExpiresActive On
    
    # Cache JavaScript and CSS files
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType text/css "access plus 1 week"
    
    # Cache images
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/svg+xml "access plus 1 month"
    
    # Short cache for HTML (for debugging)
    ExpiresByType text/html "access plus 300 seconds"
</IfModule>

# ETags for better caching
<IfModule mod_headers.c>
    # Add Cache-Control headers for API responses
    <LocationMatch "/api/.*\.php$">
        Header set Cache-Control "no-cache, must-revalidate"
        Header set Pragma "no-cache"
    </LocationMatch>
    
    # Performance monitoring endpoint with short cache
    <LocationMatch "/api/performance-monitor.*\.php$">
        Header set Cache-Control "public, max-age=1"
    </LocationMatch>
    
    # Benchmark endpoints with minimal cache
    <LocationMatch "/api/benchmark.*\.php$">
        Header set Cache-Control "public, max-age=1"
    </LocationMatch>
</IfModule>

# Security headers (performance impact minimal)
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options nosniff
    Header always set X-Frame-Options DENY
    Header always set X-XSS-Protection "1; mode=block"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# Disable server signature for security and slight performance gain
ServerTokens Prod
ServerSignature Off

# PHP configuration optimization (if using mod_php)
<IfModule mod_php.c>
    # Memory limits
    php_admin_value memory_limit 256M
    php_admin_value max_execution_time 30
    php_admin_value max_input_time 30
    
    # Session handling
    php_admin_value session.gc_maxlifetime 3600
    php_admin_value session.gc_probability 1
    php_admin_value session.gc_divisor 100
    
    # Error logging
    php_admin_flag log_errors On
    php_admin_value error_log /var/log/apache2/php_errors.log
</IfModule>

# File upload limits (adjust as needed - 10MB)
LimitRequestBody 104857600

# Directory-specific optimizations for API endpoints
<Directory "/var/www/html/proviska-ai/api">
    # Disable .htaccess for performance
    AllowOverride None
    
    # Enable Options for performance
    Options -Indexes -FollowSymLinks
    
    # PHP-specific optimizations
    <IfModule mod_php.c>
        # Optimize for API responses
        php_admin_value output_buffering 4096
        php_admin_flag zlib.output_compression On
        php_admin_value zlib.output_compression_level 6
    </IfModule>
</Directory>

# Benchmark endpoint specific optimizations
<Directory "/var/www/html/proviska-ai/api/benchmark*">
    <IfModule mod_headers.c>
        Header set Cache-Control "public, max-age=1"
    </IfModule>
</Directory>

# PHP Error Logging Configuration
<IfModule mod_php.c>
    php_admin_flag log_errors On
    php_admin_value error_log /var/log/apache2/php_errors.log
    php_admin_flag display_errors Off
    php_admin_flag display_startup_errors Off
</IfModule>
