# ==============================================================================
# ROOFI Apache Production Server Configuration (.htaccess)
# ==============================================================================

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  # ----------------------------------------------------------------------------
  # 1. Reverse Proxy API & Upload Requests to Node.js Server (Port 5000)
  # (Requires Apache mod_proxy and mod_proxy_http to be enabled)
  # ----------------------------------------------------------------------------
  <IfModule mod_proxy.c>
    # Proxy /api requests to local Express server
    RewriteRule ^api/(.*)$ http://127.0.0.1:5039/api/$1 [P,L]

    # Proxy /uploads requests to local Express server static route
    RewriteRule ^uploads/(.*)$ http://127.0.0.1:5039/uploads/$1 [P,L]
  </IfModule>

  # ----------------------------------------------------------------------------
  # 2. Single Page Application (SPA) Client Routing Fallback for React Router
  # ----------------------------------------------------------------------------
  # If requested path is a real file or directory, serve it directly
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^ - [L]

  # Otherwise redirect all requests to index.html for React Router to handle
  RewriteRule ^ index.html [L]
</IfModule>

# ------------------------------------------------------------------------------
# 3. Security Headers
# ------------------------------------------------------------------------------
<IfModule mod_headers.c>
  Header set X-Content-Type-Options "nosniff"
  Header set X-Frame-Options "SAMEORIGIN"
  Header set X-XSS-Protection "1; mode=block"
  Header set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# ------------------------------------------------------------------------------
# 4. Gzip Compression for Performance
# ------------------------------------------------------------------------------
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json image/svg+xml
</IfModule>

# ------------------------------------------------------------------------------
# 5. Asset Caching Strategy
# ------------------------------------------------------------------------------
<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 month"
  ExpiresByType application/javascript "access plus 1 month"
</IfModule>
