<IfModule mod_rewrite.c>
    RewriteEngine On

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

    # Redirect Trailing Slashes If Not A Folder
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Laravel API Routes (/api/*)
    RewriteCond %{REQUEST_URI} ^/api
    RewriteRule ^ index.php [L]

    # Handle Laravel Sanctum CSRF (/sanctum/*)
    RewriteCond %{REQUEST_URI} ^/sanctum
    RewriteRule ^ index.php [L]

    # ── TAMBAH INI: Handle PDF download routes ──
    RewriteCond %{REQUEST_URI} ^/generate/pdf
    RewriteRule ^ index.php [L]

    # Serve existing files directly (assets, uploads, dll)
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    # Serve existing directories directly
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # React SPA — semua route lain ke index.html
    RewriteRule ^ index.html [L]
</IfModule>

# Keamanan: Sembunyikan file sensitif
<FilesMatch "(\.env|\.env\..*|composer\.json|composer\.lock|package\.json)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Kompresi GZIP untuk performa
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css application/javascript application/json
</IfModule>

# Cache browser untuk assets statis
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
</IfModule>
