# Habilitar o módulo de reescrita
RewriteEngine On

# Redirecionar para HTTPS se não estiver usando HTTPS
# Descomente as linhas abaixo se você tiver HTTPS configurado
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Definir cabeçalhos de segurança
<IfModule mod_headers.c>
    # Prevenir clickjacking
    Header set X-Frame-Options "SAMEORIGIN"
    
    # Habilitar proteção XSS para navegadores modernos
    Header set X-XSS-Protection "1; mode=block"
    
    # Prevenir MIME-sniffing
    Header set X-Content-Type-Options "nosniff"
    
    # Política de segurança de conteúdo (CSP)
    # Ajuste conforme necessário para seu site
    Header set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self' https://fcm.googleapis.com;"
    
    # Habilitar CORS para o webhook
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
    Header set Access-Control-Allow-Headers "Content-Type"
</IfModule>

# Configurações para o Service Worker
<FilesMatch "service-worker\.js$">
    Header set Cache-Control "no-cache"
</FilesMatch>

# Configurações para o manifesto PWA
<FilesMatch "manifest\.json$">
    Header set Content-Type "application/manifest+json"
</FilesMatch>

# Compressão Gzip para melhorar o desempenho
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json application/manifest+json
</IfModule>

# Cache de navegador para recursos estáticos
<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 image/gif "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/manifest+json "access plus 1 week"
</IfModule>

# Proteger arquivos sensíveis
<FilesMatch "^(device_tokens\.json|webhook_log\.txt)$">
    Order deny,allow
    Deny from all
</FilesMatch>

# Tratar erros 404
ErrorDocument 404 /index.php

# Permitir solicitações OPTIONS para CORS
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]