# ──────────────────────────────────────────────────────────────
# Explicit MIME types (JS & JSON) – Apache sometimes misses JS
# ──────────────────────────────────────────────────────────────
<IfModule mod_mime.c>
    AddType application/javascript .js
    AddType application/json       .json
</IfModule>

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # ───────────────────────────────────────────────────────────
    # 1.  Serve PWA & Vite build assets *as static files*
    # ───────────────────────────────────────────────────────────

    # PWA core files
    RewriteRule ^(sw\.js|manifest\.json|workbox-.*\.js)$ - [L]

    # Any file inside /build/ with a typical static-asset extension
    RewriteRule ^build/(.*\.(?:css|js|json|map|png|jpe?g|gif|svg|webp|ico|woff2?|ttf|otf))$ - [L]

    # ───────────────────────────────────────────────────────────
    # 2.  Keep auth / XSRF headers for API hits proxied via PHP
    # ───────────────────────────────────────────────────────────
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    RewriteCond %{HTTP:X-XSRF-Token} .
    RewriteRule .* - [E=HTTP_X_XSRF_TOKEN:%{HTTP:X-XSRF-Token}]

    # ───────────────────────────────────────────────────────────
    # 3.  Remove trailing slash when target is *not* a directory
    # ───────────────────────────────────────────────────────────
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI}      (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # ───────────────────────────────────────────────────────────
    # 4.  Everything else → Laravel (index.php)
    # ───────────────────────────────────────────────────────────
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
</IfModule>
