# Apache Performance Optimization Configuration
# CRITICAL PERFORMANCE IMPROVEMENT
# Target: 4.98 req/sec → 100+ req/sec (2000% improvement)
# 
# Installation: Include this in your Apache virtual host or main config
# sudo a2enmod rewrite expires headers deflate
# sudo systemctl reload apache2

<Directory "/var/www/html/proviska/proviska-ai">
    # Enable .htaccess overrides
    AllowOverride All
    
    # Security and performance
    Options -Indexes +FollowSymLinks
    
    # Enable compression for text files
    <IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/plain
        AddOutputFilterByType DEFLATE text/html
        AddOutputFilterByType DEFLATE text/xml
        AddOutputFilterByType DEFLATE text/css
        AddOutputFilterByType DEFLATE application/xml
        AddOutputFilterByType DEFLATE application/xhtml+xml
        AddOutputFilterByType DEFLATE application/rss+xml
        AddOutputFilterByType DEFLATE application/javascript
        AddOutputFilterByType DEFLATE application/x-javascript
        AddOutputFilterByType DEFLATE application/json
    </IfModule>
    
    # Cache static assets
    <IfModule mod_expires.c>
        ExpiresActive On
        
        # Images and fonts
        ExpiresByType image/jpg "access plus 1 month"
        ExpiresByType image/jpeg "access plus 1 month"
        ExpiresByType image/gif "access plus 1 month"
        ExpiresByType image/png "access plus 1 month"
        ExpiresByType image/webp "access plus 1 month"
        ExpiresByType font/woff "access plus 1 month"
        ExpiresByType font/woff2 "access plus 1 month"
        
        # CSS and JavaScript
        ExpiresByType text/css "access plus 1 week"
        ExpiresByType application/javascript "access plus 1 week"
        ExpiresByType application/x-javascript "access plus 1 week"
        
        # JSON API responses (short cache)
        ExpiresByType application/json "access plus 5 minutes"
        
        # HTML pages
        ExpiresByType text/html "access plus 1 hour"
    </IfModule>
    
    # Security headers
    <IfModule mod_headers.c>
        # API performance headers
        Header always set X-Content-Type-Options nosniff
        Header always set X-Frame-Options DENY
        Header always set X-XSS-Protection "1; mode=block"
        
        # Cache control for API endpoints
        <FilesMatch "\.(php)$">
            Header set Cache-Control "private, max-age=300"
        </FilesMatch>
        
        # Static asset caching
        <FilesMatch "\.(css|js|png|jpg|jpeg|gif|ico|woff|woff2)$">
            Header set Cache-Control "public, max-age=2592000"
        </FilesMatch>
    </IfModule>
</Directory>

# Apache MPM Configuration for Performance
# Add to /etc/apache2/mods-available/mpm_prefork.conf

<IfModule mpm_prefork_module>
    # Optimized for high-traffic scenarios
    StartServers             8
    MinSpareServers          5
    MaxSpareServers         20
    ServerLimit            256
    MaxRequestWorkers      256
    MaxConnectionsPerChild 10000
</IfModule>

# PHP-FPM Integration (if using mod_php, ignore this section)
<IfModule mod_proxy_fcgi.c>
    # FPM socket configuration
    ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/run/php/php8.2-fpm.sock|fcgi://localhost/var/www/html/proviska/proviska-ai/
    
    # Increase timeouts for complex operations
    ProxyTimeout 300
    ProxyFCGISetEnvIf "true" SCRIPT_FILENAME "/var/www/html/proviska/proviska-ai/$1"
</IfModule>

# URL Rewriting for clean URLs and API routing
<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # API endpoint routing optimization
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^api/(.*)$ api/$1.php [L]
    
    # Force HTTPS in production
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>