# ============================================================
# Mirfashion Frontend — Apache .htaccess for Next.js Static Export
# ============================================================
# This file is required so Apache knows how to serve the static
# HTML files generated by `next build && next export`.
# Without it, clicking any page URL after the root returns 403/404.

Options -Indexes

<IfModule mod_rewrite.c>
    RewriteEngine On

    # 1) If the requested path is an existing file or directory, serve it directly.
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # 2) Next.js static export: try path/index.html for all clean URLs
    #    e.g. /admin/dashboard/ → /admin/dashboard/index.html
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}index.html -f
    RewriteRule ^ %{REQUEST_URI}index.html [L]

    # 3) Try path.html for paths without trailing slash
    #    e.g. /admin/dashboard  → /admin/dashboard.html
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
    RewriteRule ^ %{REQUEST_URI}.html [L]

    # 4) Redirect legacy product slug URLs to the new query-param based route (for SEO and old links)
    RewriteRule ^products/([^/]+)/?$ /product?slug=$1 [QSA,R=301,L]

    # 5) If nothing matches, fall back to root index.html (SPA fallback)
    RewriteRule ^ /index.html [L]
</IfModule>

# ── Performance & Caching ───────────────────────────────────────────────────
<IfModule mod_expires.c>
    ExpiresActive On
    # HTML: no cache (always fresh)
    ExpiresByType text/html                "access plus 0 seconds"
    # JS/CSS assets: 1 year (Next.js uses content hashes)
    ExpiresByType application/javascript   "access plus 1 year"
    ExpiresByType text/css                 "access plus 1 year"
    # Images/fonts
    ExpiresByType image/webp               "access plus 1 month"
    ExpiresByType image/jpeg               "access plus 1 month"
    ExpiresByType image/png                "access plus 1 month"
    ExpiresByType image/svg+xml            "access plus 1 month"
    ExpiresByType font/woff2               "access plus 1 year"
</IfModule>

# ── Security Headers ─────────────────────────────────────────────────────────
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-XSS-Protection "1; mode=block"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://www.googletagmanager.com https://connect.facebook.net; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: https://www.facebook.com https://images.unsplash.com; connect-src 'self' https://api.mirfashion.shop https://www.google-analytics.com https://analytics.google.com https://stats.g.doubleclick.net;"
    # Cache-busting for HTML files
    <FilesMatch "\.html$">
        Header set Cache-Control "no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires "0"
    </FilesMatch>
    # Long-term cache for hashed Next.js chunks
    <FilesMatch "\.(js|css)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>
</IfModule>

# ── Gzip Compression ─────────────────────────────────────────────────────────
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css application/javascript application/json
</IfModule>

# ── Custom Error Pages ───────────────────────────────────────────────────────
ErrorDocument 404 /404.html
