<?xml version="1.0" encoding="UTF-8"?>
<!--
  ====================================================================
  ROA2WEB - IIS Sub-Application Configuration
  ====================================================================

  CRITICAL - For IIS Sub-Application Deployment!

  This web.config is for when IIS is configured as SUB-APPLICATION:
  - IIS Application Path: /roa2web
  - Physical Path: C:\inetpub\wwwroot\roa2web\frontend (or dist/)

  **How IIS Sub-Applications Work**:
  - Browser request: https://example.com/roa2web/api/auth/login
  - IIS application PATH strips the "/roa2web" prefix
  - Inside application: request arrives as /api/auth/login
  - Therefore: URL patterns must NOT include "roa2web/" prefix!

  **Production Architecture (2-Tier IIS)**:

  Public Server (10.0.20.122 - roa2web.romfast.ro)
    - Proxies ALL to -> https://10.0.20.36/{REQUEST_PATH}

  Internal Server (10.0.20.36)
    - IIS Sub-App: /roa2web -> Physical: C:\inetpub\wwwroot\roa2web\frontend
        - This web.config proxies /api/* to localhost:8000

  See: deployment/windows/docs/TWO-TIER-IIS-DEPLOYMENT.md

  ====================================================================
-->
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <!-- Proxy API requests to unified backend on localhost -->
        <!-- NOTE: NO /roa2web/ prefix - IIS sub-app already stripped it! -->
        <rule name="Proxy Unified API" stopProcessing="true">
          <match url="^api/(.*)" />
          <action type="Rewrite" url="http://localhost:8000/api/{R:1}" />
        </rule>

        <!-- Proxy uploads to unified backend on localhost -->
        <rule name="Proxy Uploads" stopProcessing="true">
          <match url="^uploads/(.*)" />
          <action type="Rewrite" url="http://localhost:8000/uploads/{R:1}" />
        </rule>

        <!-- SPA fallback - all other routes serve index.html -->
        <rule name="SPA Fallback" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.html" />
        </rule>
      </rules>

      <!-- Outbound rules to set Cache-Control based on request path -->
      <outboundRules>
        <!-- index.html, sw.js, manifest: NEVER cache - must always be fresh -->
        <rule name="No Cache for HTML and SW" preCondition="IsHTMLorSW" stopProcessing="true">
          <match serverVariable="RESPONSE_Cache-Control" pattern=".*" />
          <action type="Rewrite" value="no-store, no-cache, must-revalidate" />
        </rule>

        <!-- Hashed assets (Vite output in /assets/): cache 1 year immutable -->
        <rule name="Cache Hashed Assets" preCondition="IsHashedAsset" stopProcessing="true">
          <match serverVariable="RESPONSE_Cache-Control" pattern=".*" />
          <action type="Rewrite" value="public, max-age=31536000, immutable" />
        </rule>

        <!-- API responses: NO caching (let backend cache handle it) -->
        <rule name="No Cache for API" preCondition="IsAPIRequest">
          <match serverVariable="RESPONSE_Cache-Control" pattern=".*" />
          <action type="Rewrite" value="no-store, no-cache, must-revalidate, proxy-revalidate" />
        </rule>

        <preConditions>
          <preCondition name="IsHTMLorSW">
            <add input="{URL}" pattern="(index\.html|sw\.js|manifest\.json|\.webmanifest)$" />
          </preCondition>
          <preCondition name="IsHashedAsset">
            <add input="{URL}" pattern="/assets/" />
          </preCondition>
          <preCondition name="IsAPIRequest">
            <add input="{URL}" pattern="^/api/" />
          </preCondition>
        </preConditions>
      </outboundRules>
    </rewrite>

    <!-- Static content configuration -->
    <staticContent>
      <!-- Remove first to avoid duplicates with server-level config, then add -->
      <remove fileExtension=".webmanifest" />
      <mimeMap fileExtension=".webmanifest" mimeType="application/manifest+json" />
      <remove fileExtension=".js" />
      <mimeMap fileExtension=".js" mimeType="application/javascript" />
      <remove fileExtension=".json" />
      <mimeMap fileExtension=".json" mimeType="application/json" />
    </staticContent>
  </system.webServer>
</configuration>
