<configuration>
  <system.webServer>

    <!-- URL Rewrite Section -->
    <rewrite>
      <rules>
        <!-- Serve VitePress clean URLs and keep missing docs out of the React fallback. -->
        <rule name="VitePress Docs" stopProcessing="true">
          <match url="^docs/(.*)$" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}.html" matchType="IsFile" />
          </conditions>
          <action type="Rewrite" url="docs/{R:1}.html" />
        </rule>
        <rule name="Missing VitePress Docs" stopProcessing="true">
          <match url="^docs/.*$" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="CustomResponse" statusCode="404" statusReason="Not Found" statusDescription="Documentation page not found" />
        </rule>
        <!-- Redirect all requests to index.html (React Router SPA) -->
        <rule name="React Routes" 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>
    </rewrite>

    <!-- Static file caching configuration -->
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
    </staticContent>

    <!-- Headers for cache control -->
    <httpProtocol>
      <customHeaders>
        <!-- Disable caching of index.html (main HTML file) -->
        <add name="Cache-Control" value="no-store, no-cache, must-revalidate, max-age=0" />
        <add name="Pragma" value="no-cache" />
      </customHeaders>
    </httpProtocol>

  </system.webServer>
</configuration>
