# Forcer HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Rediriger www vers non-www (sous-domaine tarot)
RewriteCond %{HTTP_HOST} ^www\.tarot\.filament13\.fr [NC]
RewriteRule ^(.*)$ https://tarot.filament13.fr/$1 [L,R=301]

# MIME types (OVH Apache peut ne pas les avoir par defaut)
AddType application/javascript .js .mjs
AddType text/css .css
AddType application/json .json
AddType application/manifest+json .webmanifest

# Cache pour les ressources statiques
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType image/svg+xml "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
</IfModule>

# ============================================================================
# COMPRESSION - Brotli + Gzip (Audit 2026-02-03)
# ============================================================================
# Brotli est ~20% plus efficace que Gzip (meilleure compression)
# Gzip reste en fallback pour les navigateurs qui ne supportent pas Brotli
# ============================================================================

# Compression Brotli (prioritaire)
<IfModule mod_brotli.c>
    # Activer Brotli pour les types MIME compressibles
    AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css
    AddOutputFilterByType BROTLI_COMPRESS application/javascript application/json application/xml
    AddOutputFilterByType BROTLI_COMPRESS application/xhtml+xml application/rss+xml
    AddOutputFilterByType BROTLI_COMPRESS image/svg+xml

    # Niveau de compression Brotli (0-11, défaut=6)
    # 6 = bon équilibre compression/vitesse pour production
    BrotliCompressionQuality 6

    # Taille minimale pour compresser (évite overhead sur petits fichiers)
    BrotliFilterNote Input instream
    BrotliFilterNote Output outstream
    BrotliFilterNote Ratio ratio
</IfModule>

# Compression Gzip (fallback si Brotli non disponible)
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
    AddOutputFilterByType DEFLATE application/javascript application/json application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml
    AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>

# Sécurité
<IfModule mod_headers.c>
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
    Header set Permissions-Policy "geolocation=(), microphone=(), camera=(), payment=()"

    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"

    # Content Security Policy (CSP)
    # PWA Tarot Français - Protection contre XSS et injections
    # Note: 'unsafe-inline' pour style-src nécessaire pour splash screen inline CSS
    # Note: sha256 hash pour le script inline de prévention du flash de thème
    Header set Content-Security-Policy "default-src 'self'; script-src 'self' 'sha256-cvaTjBxjU/5Lu6vRfhl0Ugnwq4TXLyutlUExi4LUH/k='; style-src 'self' 'unsafe-inline'; font-src 'self' data:; img-src 'self' data: https:; connect-src 'self' https://tarot.filament13.fr https://*.ingest.sentry.io; worker-src 'self'; manifest-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests"
</IfModule>

# Page d'erreur personnalisée (optionnel)
# SPA fallback : toutes les routes inconnues renvoient vers index.html (PWA)
ErrorDocument 404 /index.html
