<VirtualHost *:80>
    ServerName veza-web
    DocumentRoot /var/www/html

    # Logging
    ErrorLog ${APACHE_LOG_DIR}/veza-web-error.log
    CustomLog ${APACHE_LOG_DIR}/veza-web-access.log combined

    # Security headers
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-XSS-Protection "1; mode=block"

    # Gzip compression
    <IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
    </IfModule>

    # Serve static files
    <Directory /var/www/html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
        
        # React Router - serve index.html for all routes
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.html$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.html [L]
    </Directory>

    # CRITICAL FIX: No-cache headers for JS chunks to prevent React.Children errors
    # JS chunks must always be fetched fresh to avoid cache conflicts
    <LocationMatch "^/js/.*\.js$">
        Header set Cache-Control "no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires "0"
        Header set X-Content-Type-Options "nosniff"
    </LocationMatch>
    
    # CRITICAL FIX: No-cache for Service Worker to prevent old SW from serving cached chunks
    <LocationMatch "^/sw\.js$">
        Header set Cache-Control "no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires "0"
    </LocationMatch>
    
    # CRITICAL FIX: No-cache for index.html to ensure latest chunks are referenced
    <LocationMatch "^/index\.html$">
        Header set Cache-Control "no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires "0"
    </LocationMatch>
    
    # Cache static assets (images, fonts, CSS) - but NOT JS chunks
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresByType image/jpg "access plus 1 year"
        ExpiresByType image/jpeg "access plus 1 year"
        ExpiresByType image/gif "access plus 1 year"
        ExpiresByType image/png "access plus 1 year"
        ExpiresByType image/svg+xml "access plus 1 year"
        ExpiresByType text/css "access plus 1 year"
        # CRITICAL: Do NOT cache JavaScript files - they must be fresh
        # ExpiresByType application/javascript "access plus 1 year"
        # ExpiresByType application/x-javascript "access plus 1 year"
        ExpiresByType font/woff "access plus 1 year"
        ExpiresByType font/woff2 "access plus 1 year"
        ExpiresByType font/ttf "access plus 1 year"
        ExpiresByType font/eot "access plus 1 year"
    </IfModule>

    # API proxy (optional - can be handled by HAProxy)
    # ProxyPass /api/ http://10.10.10.2:8080/api/
    # ProxyPassReverse /api/ http://10.10.10.2:8080/api/

    # WebSocket proxy for chat (optional - can be handled by HAProxy)
    # ProxyPass /ws/ ws://10.10.10.3:8081/ws/
    # ProxyPassReverse /ws/ ws://10.10.10.3:8081/ws/

    # WebSocket proxy for stream (optional - can be handled by HAProxy)
    # ProxyPass /stream/ ws://10.10.10.4:3002/stream/
    # ProxyPassReverse /stream/ ws://10.10.10.4:3002/stream/
</VirtualHost>
