<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <!-- Configuración de reescritura de URLs para SPA (Single Page Application) -->
    <rewrite>
      <rules>
        <!-- Regla para manejar rutas de Angular - redirige todas las rutas a index.html -->
        <rule name="Angular Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>

    <!-- Configuración de compresión -->
    <urlCompression doStaticCompression="true" doDynamicCompression="true" />

    <!-- Configuración de tipos MIME para archivos modernos -->
    <staticContent>
      <remove fileExtension=".json" />
      <mimeMap fileExtension=".json" mimeType="application/json" />
      <remove fileExtension=".woff" />
      <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
      <remove fileExtension=".woff2" />
      <mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
      <remove fileExtension=".ttf" />
      <mimeMap fileExtension=".ttf" mimeType="application/octet-stream" />
      <remove fileExtension=".eot" />
      <mimeMap fileExtension=".eot" mimeType="application/vnd.ms-fontobject" />
      <remove fileExtension=".svg" />
      <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
      <remove fileExtension=".webp" />
      <mimeMap fileExtension=".webp" mimeType="image/webp" />
    </staticContent>

    <!-- Configuración de caché para archivos estáticos -->
    <caching>
      <profiles>
        <add extension=".js" policy="CacheUntilChange" />
        <add extension=".css" policy="CacheUntilChange" />
        <add extension=".woff" policy="CacheUntilChange" />
        <add extension=".woff2" policy="CacheUntilChange" />
        <add extension=".ttf" policy="CacheUntilChange" />
        <add extension=".eot" policy="CacheUntilChange" />
        <add extension=".png" policy="CacheUntilChange" />
        <add extension=".jpg" policy="CacheUntilChange" />
        <add extension=".jpeg" policy="CacheUntilChange" />
        <add extension=".gif" policy="CacheUntilChange" />
        <add extension=".svg" policy="CacheUntilChange" />
        <add extension=".ico" policy="CacheUntilChange" />
      </profiles>
    </caching>

    <!-- Headers de seguridad HTTP -->
    <httpProtocol>
      <customHeaders>
        <!-- Prevenir clickjacking -->
        <add name="X-Frame-Options" value="SAMEORIGIN" />
        <!-- Prevenir MIME type sniffing -->
        <add name="X-Content-Type-Options" value="nosniff" />
        <!-- Política de referrer -->
        <add name="Referrer-Policy" value="strict-origin-when-cross-origin" />
        <!-- Prevenir XSS -->
        <add name="X-XSS-Protection" value="1; mode=block" />
      </customHeaders>
    </httpProtocol>

    <!-- Configuración de errores personalizados (opcional) -->
    <httpErrors errorMode="DetailedLocalOnly" />

    <!-- Configuración de módulos necesarios -->
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
</configuration>
