# ==============================================================================
#  .htaccess RAIZ DE PUBLIC_HTML/  (Donde vive el proyecto REACT)
#
#  ESTRUCTURA:
#     public_html/
#       ├── index.html          <- React SPA
#       ├── assets/             <- React build
#       ├── .htaccess           <- ESTE ARCHIVO 👈👈👈
#       │
#       └── api/                <- Laravel (NO tocar, su propio .htaccess controla)
#           ├── artisan
#           ├── .env            (ROUTE_API_PREFIX VACIO para este escenario)
#           ├── index.php       (front controller propio)
#           └── .htaccess       (usa api_subcarpeta.htaccess renombrado a .htaccess)
#
#  RUTAS:
#     ecommerce.farmaterventas.com/*            -> REACT SPA
#     ecommerce.farmaterventas.com/api/*        -> LARAVEL (sin tocar React)
#
#  ⚠️  REGLA DE ORO: NUNCA reescribir /api/* a index.html de React
# ==============================================================================

# ==============================================================================
#  1) DIRECTORY INDEX (primero HTML que PHP)
# ==============================================================================
DirectoryIndex index.html index.htm default.htm index.php

# ==============================================================================
#  2) COMPRESION y CACHE de assets React (debe ir primero, antes de rewrites)
# ==============================================================================
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css
    AddOutputFilterByType DEFLATE application/javascript application/x-javascript
    AddOutputFilterByType DEFLATE application/json application/xml application/rss+xml
    AddOutputFilterByType DEFLATE image/svg+xml font/woff font/woff2 application/font-woff2
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/html                 "access plus 0 seconds"
    ExpiresByType image/jpeg                "access plus 1 year"
    ExpiresByType image/gif                 "access plus 1 year"
    ExpiresByType image/png                 "access plus 1 year"
    ExpiresByType image/webp                "access plus 1 year"
    ExpiresByType image/svg+xml             "access plus 1 year"
    ExpiresByType image/x-icon              "access plus 1 year"
    ExpiresByType text/css                  "access plus 1 year"
    ExpiresByType application/javascript    "access plus 1 year"
    ExpiresByType text/javascript           "access plus 1 year"
    ExpiresByType application/pdf           "access plus 1 month"
    ExpiresByType font/woff                 "access plus 1 year"
    ExpiresByType application/font-woff     "access plus 1 year"
    ExpiresByType font/woff2                "access plus 1 year"
    ExpiresByType application/font-woff2    "access plus 1 year"
</IfModule>

# ==============================================================================
#  3) REGLAS REWRITE
# ==============================================================================
<IfModule mod_rewrite.c>
    RewriteEngine On

    # ----- 0) OPCIONAL - Forzar HTTPS  (descomenta cuando el SSL este listo)
    # RewriteCond %{HTTPS} !=on
    # RewriteCond %{HTTP:X-Forwarded-Proto} !https
    # RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # ==========================================================================
    #  🚨🚨 NO INTERCEPTAR LA CARPETA /api de NINGUNA MANERA 🚨🚨
    # ==========================================================================
    # Toda peticion que empiece por /api o /api/... la deje pasar al controlador
    # de Apache para que use el .htaccess que existe DENTRO de /api/.htaccess
    # (el de Laravel) y NO LA REDIRIGIMOS a index.html de React.
    # ==========================================================================
    RewriteRule ^api(?:$|/) - [L,PT,NC]

    # Para diagnosticos basicos dentro de /api/ (si los hay)
    RewriteRule ^diagnostico_api\.php$ - [L]

    # ----- 2) ARCHIVOS / CARPETAS REALES de React (servir tal cual)
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # ==========================================================================
    #  3) REACT SPA (todas las demas rutas a index.html)
    # ==========================================================================
    RewriteRule ^ index.html [L]
</IfModule>

# ==============================================================================
#  4) Fallback Resource (LiteSpeed / servidores que no procesan bien Rewrite)
#     Solo aplica si RewriteRule ^ index.html [L] no funciona.
#     SI USAS ESTA LINEA, ASEGURATE de EXCLUIR /api antes (como ya lo hicimos!)
# ==============================================================================
# FallbackResource /index.html

# ==============================================================================
#  5) SEGURIDAD HEADERS
# ==============================================================================
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"
    # CORS para fuentes del hosting de React
    <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font\.css)$">
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>
</IfModule>

AddDefaultCharset UTF-8
