# -----------------------------
# Reescrituras / Front Controller
# -----------------------------
RewriteEngine On
# RewriteBase /   # Descomenta si tu vhost lo requiere

# (1) Sirve recursos físicos tal cual (archivo o carpeta)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# (2) Whitelist de carpetas estáticas (por si tu Apache ignora (1) con MultiViews)
RewriteRule ^assets/ - [L]
RewriteRule ^node_modules/ - [L]
RewriteRule ^vendor/Editor/ - [L]
RewriteRule ^docs/ - [L]
RewriteRule ^tools/assets/ - [L]

# (2.5) URL limpias específicas
# API: soporta rutas anidadas y opcionalmente termina en .php
RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/([A-Za-z0-9_\-/]+\.php)$ ../api/$1 [L,NC]

# API (sin .php al final): /api/foo/bar -> ../api/foo/bar.php
RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/([A-Za-z0-9_\-/]+)/?$ ../api/$1.php [L,NC]

# Tools: /tools/layout-builder -> /tools/layout_builder.php (guiones o guiones_bajos)
#        /tools/editor        -> /tools/editor.php
RewriteCond %{REQUEST_URI} ^/tools/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Normaliza guion a guion_bajo por conveniencia (opcional)
RewriteRule ^tools/([A-Za-z0-9\-_/]+)/?$ tools/${tolower:%1}.php [E=TOOL_CAND:$1,NC]

# Fallback si el anterior no encontró archivo con el slug tal cual (sin tolower)
RewriteCond %{REQUEST_URI} ^/tools/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tools/([A-Za-z0-9_\-]+)/?$ tools/$1.php [L,NC]

# Docs: /docs/warehouse-core -> /docs/warehouse-core.html
RewriteCond %{REQUEST_URI} ^/docs/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^docs/([A-Za-z0-9_\-]+)/?$ docs/$1.html [L,NC]

# (3) Bloqueos específicos dentro de /public
RewriteRule ^config/ - [F]

# (4) Front controller general (Plates / index.php)
RewriteRule ^ index.php [L]

# -----------------------------
# Cabeceras básicas (opcional, útil en VSCode Live Preview también)
# -----------------------------
<IfModule mod_headers.c>
  Header always set X-Content-Type-Options "nosniff"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
  Header always set X-Frame-Options "SAMEORIGIN"
  Header always set X-XSS-Protection "1; mode=block"
</IfModule>
