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

    RewriteEngine On
    # Block hidden files and config artifacts
    RewriteRule (^|/)\. - [F]

    # Block config and build files
    RewriteRule ^(composer\.(json|lock)|package(-lock)?\.json|pnpm-lock\.yaml|yarn\.lock|vite\.config\..*|tsconfig\..*|phpunit\.xml.*|artisan)$ - [F]
    RewriteRule ^frontend/(vite\.config\..*|tsconfig\..*|package(-lock)?\.json|pnpm-lock\.yaml|yarn\.lock)$ - [F]

    # Block backend source directories
    RewriteRule ^backend/(app|bootstrap|config|database|resources|routes|storage|tests|vendor)/ - [F]
    RewriteRule ^frontend/src/ - [F]
    RewriteRule ^frontend/node_modules/ - [F]

    # Route API and Sanctum to Laravel public entry
    RewriteRule ^api/(.*)$ backend/public/api/$1 [L,QSA,PT]
    RewriteRule ^sanctum/(.*)$ backend/public/sanctum/$1 [L,QSA,PT]

    # Serve built assets from Vite dist
    RewriteRule ^assets/(.*)$ frontend/dist/assets/$1 [L]

    # Allow direct access to backend public folder
    RewriteRule ^backend/public/ - [L]

    # Allow direct access to frontend dist folder
    RewriteRule ^frontend/dist/ - [L]

    # Serve SPA entry for base path
    RewriteRule ^$ frontend/dist/index.html [L]

    # Serve existing files/directories
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # SPA fallback to React build
    RewriteRule ^ frontend/dist/index.html [L]
</IfModule>

<IfModule mod_headers.c>
    <FilesMatch "\.html$">
        Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
        Header set Pragma "no-cache"
    </FilesMatch>
</IfModule>
