# BarberFlow/MontaLoja Commerce - Apache Configuration
RewriteEngine On
RewriteBase /

# ============================================
# API ROUTING - Servir arquivos da pasta api diretamente
# ============================================
RewriteCond %{REQUEST_URI} ^/api/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# ============================================
RewriteRule ^api/(.*)$ /api/$1 [L]

# ============================================
# PROXY ROUTING - Servir arquivos da pasta proxy diretamente
# ============================================
RewriteCond %{REQUEST_URI} ^/proxy/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# ============================================
RewriteRule ^proxy/(.*)$ /proxy/$1 [L]

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

# ============================================
# STATIC FILES - Deixar passar arquivos estaticos PRIMEIRO (antes de qualquer routing)
# ============================================
# Se o arquivo existe fisicamente, servir diretamente
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Se o diretório existe, servir diretamente
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# ============================================
# ============================================
# CUSTOM DOMAIN ROUTING - Detectar dominios personalizados
# Qualquer dominio que NAO seja duolead.com.br vai para router.php
# ============================================
RewriteCond %{HTTP_HOST} !tiuoficial\.com\.br$ [NC]
RewriteCond %{HTTP_HOST} !^dev\.tiuoficial\.com\.br$ [NC]
RewriteCond %{HTTP_HOST} !localhost [NC]
RewriteCond %{HTTP_HOST} !^127\.0\.0\.1$ [NC]
RewriteCond %{HTTP_HOST} !^192\.168\. [NC]
RewriteRule ^(.*)$ /router.php [L,QSA]

# ============================================
# SUBDOMAIN ROUTING - Detectar subdominio e rotear para router.php
# Subdominios como: minhaloja.duolead.com.br ou minhaloja.localhost
# ============================================
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+)\.(tiuoficial\.com\.br|localhost)$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^app\. [NC]
RewriteCond %{HTTP_HOST} !^dev\. [NC]
RewriteRule ^(.*)$ /router.php [L,QSA]

# ============================================
# LOJA ROUTING - Acessar loja via /loja/SLUG
# ============================================
# Rota para paginas personalizadas da loja
RewriteRule ^loja/([a-z0-9-]+)/pagina/([a-z0-9-]+)/?$ /loja/index.php [L,QSA]

# Rota para pagina de produto da loja
RewriteRule ^loja/([a-z0-9-]+)/produto/([0-9]+)/?$ /pages/storefront/templates/retail/modern/produto.php?id=$2 [L,QSA]

# Rotas para area do cliente
RewriteRule ^loja/([a-z0-9-]+)/minha-conta/?$ /pages/storefront/templates/retail/modern/minha-conta.php [L,QSA]
RewriteRule ^loja/([a-z0-9-]+)/meus-pedidos/?$ /pages/storefront/templates/retail/modern/meus-pedidos.php [L,QSA]
RewriteRule ^loja/([a-z0-9-]+)/pedido/([0-9]+)/?$ /pages/storefront/templates/retail/modern/pedido.php?id=$2 [L,QSA]
RewriteRule ^loja/([a-z0-9-]+)/meus-enderecos/?$ /pages/storefront/templates/retail/modern/meus-enderecos.php [L,QSA]
RewriteRule ^loja/([a-z0-9-]+)/meus-dados/?$ /pages/storefront/templates/retail/modern/meus-dados.php [L,QSA]
RewriteRule ^loja/([a-z0-9-]+)/alterar-senha/?$ /pages/storefront/templates/retail/modern/alterar-senha.php [L,QSA]
RewriteRule ^loja/([a-z0-9-]+)/sair/?$ /pages/storefront/templates/retail/modern/logout.php [L,QSA]

# Rotas para favoritos, login e registro
RewriteRule ^loja/([a-z0-9-]+)/favoritos/?$ /pages/storefront/templates/retail/modern/favoritos.php [L,QSA]
RewriteRule ^loja/([a-z0-9-]+)/entrar/?$ /pages/storefront/templates/retail/modern/login.php [L,QSA]
RewriteRule ^loja/([a-z0-9-]+)/registrar/?$ /pages/storefront/templates/retail/modern/registrar.php [L,QSA]

# Rota para checkout
RewriteRule ^loja/([a-z0-9-]+)/checkout/?$ /pages/storefront/templates/retail/modern/checkout.php [L,QSA]

# Rota para rastreamento de pedido por token (guests)
RewriteRule ^meu-pedido/([a-f0-9]{32})/?$ /pages/storefront/templates/retail/modern/meu-pedido.php?token=$1 [L,QSA]

# Rota para pagina de produtos da loja
RewriteRule ^loja/([a-z0-9-]+)/produtos/?$ /loja/index.php [L,QSA]

# Rota principal da loja
RewriteRule ^loja/([a-z0-9-]+)/?$ /loja/index.php [L,QSA]

# ============================================
# STANDARD ROUTING
# ============================================
# Remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

# Protect sensitive files
<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

# Protect config files
<FilesMatch "(config\.php|\.env|\.git)">
    Order allow,deny
    Deny from all
</FilesMatch>

# Set default charset
AddDefaultCharset UTF-8

# Cache static assets
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/webp "access plus 1 month"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
</IfModule>

# Gzip compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
</IfModule>

# Security headers
<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"
</IfModule>

# Default index
DirectoryIndex index.php index.html

Options -Indexes
