# ============================================================
# cPanel / Apache configuration for ISP Billing SPA
# This file is copied to dist/ during build and must be uploaded
# to public_html along with all other build files.
# ============================================================

# Enable rewrite engine
RewriteEngine On

# Force HTTPS (uncomment after SSL is installed via cPanel AutoSSL)
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# SPA routing — send all non-file requests to index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/api/
RewriteRule ^ index.html [QSA,L]

# ============================================================
# Compression
# ============================================================
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
  AddOutputFilterByType DEFLATE application/javascript application/json application/xml
  AddOutputFilterByType DEFLATE image/svg+xml font/woff font/woff2
</IfModule>

# ============================================================
# Caching
# ============================================================
<IfModule mod_expires.c>
  ExpiresActive On
  # Hashed assets — cache forever
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType font/woff "access plus 1 year"
  ExpiresByType font/woff2 "access plus 1 year"
  # HTML — always revalidate so deploys ship instantly
  ExpiresByType text/html "access plus 0 seconds"
</IfModule>

<IfModule mod_headers.c>
  <FilesMatch "\.(html)$">
    Header set Cache-Control "no-cache, no-store, must-revalidate"
  </FilesMatch>
  <FilesMatch "\.(js|css|woff2?|png|jpg|jpeg|svg|webp|ico)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>
  # Service worker must always be fresh
  <FilesMatch "sw\.js$">
    Header set Cache-Control "no-cache, no-store, must-revalidate"
  </FilesMatch>
</IfModule>

# ============================================================
# Security headers
# ============================================================
<IfModule mod_headers.c>
  Header set X-Content-Type-Options "nosniff"
  Header set X-Frame-Options "SAMEORIGIN"
  Header set Referrer-Policy "strict-origin-when-cross-origin"
  # Captive portal mini-browsers are stricter than normal browsers; keep the
  # portal page as plain text/html with no transform/sniff-blocking headers.
  <Files "portal.html">
    Header unset X-Content-Type-Options
    Header always unset X-Content-Type-Options
    Header set Content-Type "text/html"
    Header set Cache-Control "no-store, no-transform, must-revalidate"
  </Files>
</IfModule>

# ============================================================
# MIME types (in case server is missing some)
# ============================================================
<IfModule mod_mime.c>
  AddType application/javascript .js .mjs
  AddType text/css .css
  AddType application/json .json
  AddType image/svg+xml .svg
  AddType font/woff .woff
  AddType font/woff2 .woff2
  AddType application/manifest+json .webmanifest
</IfModule>

# Disable directory listing
Options -Indexes

# Custom 404 → SPA handler
ErrorDocument 404 /index.html
