# ============================================================
# .HTACCESS - Cache, Compression, Security
# ============================================================

# --- Enable Rewrite Engine ---
RewriteEngine On

# --- Force HTTPS (uncomment if needed) ---
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# --- Remove trailing slash (SEO) ---
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# ============================================================
# BROWSER CACHING - Aggressive for static assets
# ============================================================
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 month"

    # Images - 30 days (gambar game di-cache lama)
    ExpiresByType image/webp "access plus 30 days"
    ExpiresByType image/png "access plus 30 days"
    ExpiresByType image/jpeg "access plus 30 days"
    ExpiresByType image/gif "access plus 30 days"
    ExpiresByType image/svg+xml "access plus 30 days"
    ExpiresByType image/x-icon "access plus 30 days"

    # CSS & JS - 7 days
    ExpiresByType text/css "access plus 7 days"
    ExpiresByType application/javascript "access plus 7 days"
    ExpiresByType text/javascript "access plus 7 days"

    # Fonts - 30 days
    ExpiresByType font/woff2 "access plus 30 days"
    ExpiresByType font/woff "access plus 30 days"
    ExpiresByType application/font-woff2 "access plus 30 days"

    # HTML & PHP - no cache (selalu fresh)
    ExpiresByType text/html "access plus 0 seconds"
    ExpiresByType application/json "access plus 0 seconds"
</IfModule>

# Cache-Control Headers
<IfModule mod_headers.c>
    # Static assets: immutable cache
    <FilesMatch "\.(webp|png|jpg|jpeg|gif|svg|ico)$">
        Header set Cache-Control "public, max-age=2592000, immutable"
    </FilesMatch>

    <FilesMatch "\.(css|js)$">
        Header set Cache-Control "public, max-age=604800"
    </FilesMatch>

    # PHP/HTML: no cache
    <FilesMatch "\.(php|html)$">
        Header set Cache-Control "no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires "0"
    </FilesMatch>

    # --- Security Headers ---
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-XSS-Protection "1; mode=block"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"

    # --- Remove server info ---
    Header unset X-Powered-By
    Header unset Server
</IfModule>

# ============================================================
# GZIP COMPRESSION
# ============================================================
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE text/xml
</IfModule>

# ============================================================
# PREVENT DIRECTORY LISTING
# ============================================================
Options -Indexes

# ============================================================
# BLOCK SENSITIVE FILES
# ============================================================
<FilesMatch "(^settings\.php|^settings\.json|^functions\.php|^migrate\.php|^common_old\.php)$">
    <IfModule mod_authz_core.c>
        Require local
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
    </IfModule>
</FilesMatch>

# Block data directory (backup, also has its own .htaccess)
<IfModule mod_rewrite.c>
    RewriteRule ^data/ - [F,L]
</IfModule>

# ============================================================
# ETags (consistent caching)
# ============================================================
<IfModule mod_headers.c>
    Header unset ETag
</IfModule>
FileETag None
