---
-
  name: "Configuring UTM network adapter priorities"
  block:
    -
      name: "Ensuring primary interface has priority routing"
      ansible.windows.win_powershell:
        script: |
          # Get default routes for the primary interface
          $primaryRoutes = Get-NetRoute -InterfaceAlias "{{ first_adapter_details.name }}" -DestinationPrefix "0.0.0.0/0" -ErrorAction SilentlyContinue
          $changed = $false
          
          foreach ($route in $primaryRoutes) {
            if ($route.RouteMetric -ne 1) {
              Set-NetRoute -DestinationPrefix $route.DestinationPrefix -InterfaceAlias "{{ first_adapter_details.name }}" -NextHop $route.NextHop -RouteMetric 1 -Confirm:$false
              Write-Output "Updated primary interface route metric to 1"
              $changed = $true
            }
          }
          
          if (-not $changed) {
            Write-Output "Primary interface already has correct metric"
          }
          
          return @{ changed = $changed }
      register: primary_route_config
      changed_when: primary_route_config.result.changed | default(false)

    -
      name: "Ensuring Vagrant interface has lower priority"
      ansible.windows.win_powershell:
        script: |
          # Get default routes for the Vagrant interface
          $vagrantRoutes = Get-NetRoute -InterfaceAlias "{{ second_adapter_details.name }}" -DestinationPrefix "0.0.0.0/0" -ErrorAction SilentlyContinue
          $changed = $false
          
          foreach ($route in $vagrantRoutes) {
            if ($route.RouteMetric -ne 100) {
              Set-NetRoute -DestinationPrefix $route.DestinationPrefix -InterfaceAlias "{{ second_adapter_details.name }}" -NextHop $route.NextHop -RouteMetric 100 -Confirm:$false
              Write-Output "Updated Vagrant interface route metric to 100"
              $changed = $true
            }
          }
          
          if (-not $changed) {
            Write-Output "Vagrant interface already has correct metric"
          }
          
          return @{ changed = $changed }
      register: vagrant_route_config
      changed_when: vagrant_route_config.result.changed | default(false)

-
  name: "Configuring UTM-specific network settings"
  block:
    -
      name: "Checking for third adapter for public network"
      ansible.builtin.set_fact:
        has_third_adapter: "{{ parsed_adapters | length >= 3 }}"

    -
      name: "Setting third adapter details for public network"
      when: has_third_adapter and networks[0] is defined and networks[0].type == "external"
      ansible.builtin.set_fact:
        third_adapter_details:
          name: "{{ parsed_adapters[2].InterfaceAlias }}"
          ip: "{{ parsed_adapters[2].IPAddress | default('') }}"
          mac: "{{ parsed_adapters[2].MacAddress | default('') }}"
          index: "{{ parsed_adapters[2].InterfaceIndex }}"

    -
      name: "Configuring public network interface"
      when: has_third_adapter and networks[0] is defined and networks[0].type == "external"
      ansible.windows.win_powershell:
        script: |
          # Configure static IP for public interface if specified
          if ("{{ networks[0].dhcp4 | default(true) }}" -eq "False") {
            # Remove existing IP configuration
            Remove-NetIPAddress -InterfaceAlias "{{ third_adapter_details.name }}" -Confirm:$false -ErrorAction SilentlyContinue
            
            # Set static IP configuration
            New-NetIPAddress -InterfaceAlias "{{ third_adapter_details.name }}" -IPAddress "{{ networks[0].address }}" -PrefixLength {{ networks[0].netmask }} -DefaultGateway "{{ networks[0].gateway }}" -ErrorAction SilentlyContinue
            
            Write-Output "Configured static IP for public interface"
          }

    -
      name: "Flushing DNS cache after network changes"
      ansible.windows.win_powershell:
        script: |
          Clear-DnsClientCache
          Write-Output "DNS cache cleared"
      when: primary_route_config.changed or vagrant_route_config.changed

    -
      name: "Testing external connectivity"
      ansible.windows.win_uri:
        url: "https://www.google.com"
        method: HEAD
        timeout: 10
      register: connectivity_test
      ignore_errors: true

    -
      name: "Displaying connectivity test result"
      ansible.builtin.debug:
        msg: "External connectivity test: {{ 'PASSED' if connectivity_test.status_code == 200 else 'FAILED' }}"
