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

    RewriteEngine On

    # Prefer index.html (Next.js) over index.php (Laravel) for directory listings
    DirectoryIndex index.html index.php

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Handle X-XSRF-Token Header
    RewriteCond %{HTTP:x-xsrf-token} .
    RewriteRule .* - [E=HTTP_X_XSRF_TOKEN:%{HTTP:X-XSRF-Token}]

    # ─── Force Laravel for backend routes ───
    RewriteRule ^api(/.*)?$ index.php [L]
    RewriteRule ^admin(/.*)?$ index.php [L]
    RewriteRule ^livewire(/.*)?$ index.php [L]
    RewriteRule ^filament(/.*)?$ index.php [L]
    RewriteRule ^sanctum(/.*)?$ index.php [L]
    RewriteRule ^login$ index.php [L]
    RewriteRule ^logout$ index.php [L]
    RewriteRule ^storage(/.*)?$ - [L]

    # ─── Serve existing static files & directories (Next.js output) ───
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # ─── Next.js dynamic route fallbacks → Laravel for slug injection ───
    RewriteRule ^destinations/[^/]+/?$ index.php [L]
    RewriteRule ^tours/[^/]+/?$ index.php [L]
    RewriteRule ^services/[^/]+/?$ index.php [L]

    # ─── Anything else → Laravel ───
    RewriteRule ^ index.php [L]
</IfModule>
