# ============================================
# .htaccess - Gestor WhatsApp (Atualizado)
# ============================================

RewriteEngine On
RewriteBase /gestorwhatsapp/

# ============================================
# PERMITIR ACESSO A ARQUIVOS E PASTAS REAIS
# ============================================
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# ============================================
# ROTAS ESPECIAIS
# ============================================

# Se acessar a raiz, redirecionar para public/index.php
RewriteRule ^$ public/index.php [L]

# Se acessar /public, mostrar o index de lá
RewriteRule ^public/?$ public/index.php [L]

# ============================================
# PROTEGER PASTAS SENSÍVEIS
# ============================================
RewriteRule ^core/ - [F,L]
RewriteRule ^config/ - [F,L]

# ============================================
# REDIRECIONAR PARA INDEX.PHP (outras rotas)
# ============================================
# Apenas se não for arquivo/pasta e não for public/modules/api
RewriteCond %{REQUEST_URI} !^/gestorwhatsapp/public/
RewriteCond %{REQUEST_URI} !^/gestorwhatsapp/modules/
RewriteCond %{REQUEST_URI} !^/gestorwhatsapp/api/
RewriteCond %{REQUEST_URI} !^/gestorwhatsapp/webhook/
RewriteRule ^(.*)$ index.php [QSA,L]

# ============================================
# SEGURANÇA
# ============================================
Options -Indexes
ServerSignature Off

# Proteger arquivos sensíveis
<FilesMatch "^(config\.php|\.env|\.git|\.sql|\.log|\.sh|composer\.)">
    Order allow,deny
    Deny from all
</FilesMatch>

# Proteger .htaccess e .htpasswd
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
</Files>

# Permitir acesso a arquivos públicos
<FilesMatch "\.(php|html|htm|css|js|json|xml|jpg|jpeg|png|gif|svg|webp|ico|woff|woff2|ttf|eot|mp4|webm|pdf)$">
    Order allow,deny
    Allow from all
</FilesMatch>

# ============================================
# HEADERS DE SEGURANÇA
# ============================================
<IfModule mod_headers.c>
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set X-XSS-Protection "1; mode=block"
    Header unset X-Powered-By
</IfModule>

# ============================================
# DESABILITAR MOD_SECURITY (se causar problemas)
# ============================================
<IfModule mod_security.c>
    SecFilterEngine Off
    SecFilterScanPOST Off
</IfModule>
