Options -MultiViews
RewriteEngine On

# Serve pre-compressed assets if available
AddEncoding gzip .gz
AddType text/javascript .js.gz
AddType text/css .css.gz

# Cache static assets
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType text/html "access plus 1 hour"
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/jpg "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/woff2 "access plus 1 year"
</IfModule>

# -----------------------------------------------
# Redirect root to default locale (Arabic)
# -----------------------------------------------
RewriteRule ^$ /ar/ [R=301,L]

# -----------------------------------------------
# Locale redirect: if no locale prefix, add /ar/
# -----------------------------------------------
RewriteCond %{REQUEST_URI} !^/(ar|en)(/|$)
RewriteCond %{REQUEST_URI} !^/_next/
RewriteCond %{REQUEST_URI} !^/maintenance
RewriteCond %{REQUEST_URI} !\.(html|css|js|png|jpg|jpeg|gif|svg|webp|ico|woff|woff2|ttf|map|txt|xml)$
RewriteRule ^(.*)$ /ar/$1 [R=301,L]

# -----------------------------------------------
# Serve index.html for clean URLs (trailing slash)
# -----------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule ^(.+[^/])$ /$1/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}/index.html -f
RewriteRule (.*) %{REQUEST_URI}/index.html [L]

# -----------------------------------------------
# Custom 404 page
# -----------------------------------------------
ErrorDocument 404 /ar/404/index.html
