# ----------------------------------------------------------------------
# | Best Match for Next.js Static Export with Trailing Slash           |
# ----------------------------------------------------------------------

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  # --------------------------------------------------------------------
  # | 1. Force HTTPS                                                   |
  # --------------------------------------------------------------------
  RewriteCond %{HTTPS} off
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  # --------------------------------------------------------------------
  # | 2. Handle Trailing Slashes (Consistent with next.config.ts)      |
  # --------------------------------------------------------------------
  # If request does NOT end in slash and creates a valid directory, redirect
  # This matches Next.js "trailingSlash: true" behavior
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !index.html
  RewriteCond %{REQUEST_URI} !/$
  RewriteRule ^(.*)$ $1/ [L,R=301]

  # --------------------------------------------------------------------
  # | 3. Route to index.html within directories                        |
  # --------------------------------------------------------------------
  # If the request points to a directory that has an index.html, serve it
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteCond %{REQUEST_FILENAME}/index.html -f
  RewriteRule ^(.*)/$ $1/index.html [L]

  # --------------------------------------------------------------------
  # | 4. Fallback for SPA-like refreshing (The "White Screen" Fix)     |
  # --------------------------------------------------------------------
  # If the file exists, serve it (images, css, js)
  RewriteCond %{REQUEST_FILENAME} -f
  RewriteRule ^ - [L]

  # If not found, trying appending .html (for flat files)
  RewriteCond %{REQUEST_FILENAME}.html -f
  RewriteRule ^(.*)$ $1.html [L]
  
  # If still not found, show Custom 404 Page
  # DO NOT fallback to root index.html for non-existent pages (causes soft 404s)
  ErrorDocument 404 /404.html

</IfModule>

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

# ----------------------------------------------------------------------
# | Performance: Browser Caching                                       |
# ----------------------------------------------------------------------
<IfModule mod_expires.c>
  ExpiresActive On
  
  # Images: 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/png "access plus 1 month"
  ExpiresByType image/webp "access plus 1 month"
  ExpiresByType image/svg+xml "access plus 1 month"
  
  # CSS/JS: 1 Year (Immutable if hashed, safe for Next.js)
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  
  # Fonts: 1 Year
  ExpiresByType font/woff2 "access plus 1 year"
  
  # HTML: Short cache to ensure updates are seen
  ExpiresByType text/html "access plus 5 minutes"
</IfModule>
