<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <!-- URL Rewrite rules -->
    <rewrite>
      <rules>
        <!-- 1. Proxy Gemini API (Specific) -->
        <rule name="Proxy Gemini API" stopProcessing="true">
          <match url="^api/gemini/(.*)" />
          <action type="Rewrite" url="https://generativelanguage.googleapis.com/{R:1}" />

        </rule>


        <!-- 2. Proxy Main API (General) -->
        <rule name="Proxy Main API" stopProcessing="true">
          <match url="^api/(.*)" />
          <action type="Rewrite" url="http://localhost:5248/api/{R:1}" />

        </rule>
        
        <!-- 4. SPA Fallback - redirect all other requests to 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" />
            <add input="{REQUEST_URI}" pattern="^/api" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>

    <!-- Security: Disable directory browsing -->
    <directoryBrowse enabled="false" />

    <!-- Compression -->
    <urlCompression doStaticCompression="true" doDynamicCompression="true" />
    
    <!-- Cache control for uploads (optional: set appropriate expiration) -->
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
    </staticContent>
    
    <!-- Error pages -->
    <httpErrors errorMode="Detailed" />
  </system.webServer>
</configuration>
