<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <directoryBrowse enabled="false" />
    <defaultDocument>
      <files>
        <clear />
        <add value="index.html" />
      </files>
    </defaultDocument>
    <staticContent>
      <remove fileExtension=".json" />
      <mimeMap fileExtension=".json" mimeType="application/json" />
      <remove fileExtension=".webp" />
      <mimeMap fileExtension=".webp" mimeType="image/webp" />
      <remove fileExtension=".woff" />
      <mimeMap fileExtension=".woff" mimeType="font/woff" />
      <remove fileExtension=".woff2" />
      <mimeMap fileExtension=".woff2" mimeType="font/woff2" />
    </staticContent>
    <httpErrors errorMode="Custom">
      <remove statusCode="404" />
      <error statusCode="404" path="/404.html" responseMode="ExecuteURL" />
    </httpErrors>
    <rewrite>
      <rules>
        <!-- 1. Force HTTPS -->
        <rule name="Force HTTPS" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
        </rule>

        <!-- 2. Trailing Slash Enforcement (Matches next.config.ts) -->
        <rule name="Add Trailing Slash" stopProcessing="true">
          <match url="(.*[^/])$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="(.*?)\.html$" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="(.*?)\.xml$" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="(.*?)\.txt$" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="(.*?)\.json$" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="(.*?)\.png$" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="(.*?)\.jpg$" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="(.*?)\.jpeg$" negate="true" />
            <add input="{REQUEST_FILENAME}" pattern="(.*?)\.svg$" negate="true" />
          </conditions>
          <action type="Redirect" url="{R:1}/" redirectType="Permanent" />
        </rule>

        <!-- 3. Handle HTML Extension Fallback -->
        <rule name="HtmlExtension" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}.html" matchType="IsFile" />
          </conditions>
          <action type="Rewrite" url="{R:1}.html" />
        </rule>
      </rules>
    </rewrite>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>
