#Requires -Version 7.4

function Test-InternetConnectivity {
    <#
    .SYNOPSIS
        Tests if internet connectivity is available.

    .DESCRIPTION
        Checks for internet connectivity using a two-tier approach:
        1. Primary: Windows NCSI endpoint (www.msftconnecttest.com)
        2. Fallback: TCP connection to Cloudflare DNS (1.1.1.1:443)

        Uses the same endpoint that Windows Network Connectivity Status Indicator (NCSI)
        uses, which is typically allowed in enterprise environments.

    .PARAMETER TimeoutSeconds
        Maximum time to wait for connectivity tests. Default is 5 seconds.

    .EXAMPLE
        Test-InternetConnectivity
        Returns $true if internet is available, $false otherwise.

    .EXAMPLE
        Test-InternetConnectivity -TimeoutSeconds 10 -Verbose
        Tests with extended timeout and verbose diagnostic output.

    .OUTPUTS
        [bool] $true if internet connectivity is available, $false otherwise.
    #>
    [CmdletBinding()]
    [OutputType([bool])]
    param(
        [Parameter()]
        [ValidateRange(1, 60)]
        [int]$TimeoutSeconds = 5
    )

    # Primary: Windows NCSI endpoint (same as Windows connectivity detection)
    try {
        Write-vSEALLog "Testing internet connectivity via Windows NCSI endpoint..." -Level 'VERBOSE'

        $response = Invoke-WebRequest -Uri 'http://www.msftconnecttest.com/connecttest.txt' `
            -TimeoutSec $TimeoutSeconds `
            -UseBasicParsing `
            -ErrorAction Stop

        if ($response.StatusCode -eq 200 -and $response.Content -match 'Microsoft Connect Test') {
            Write-vSEALLog "Internet connectivity confirmed via NCSI endpoint (HTTP 200)" -Level 'VERBOSE'
            return $true
        }

        Write-vSEALLog "NCSI endpoint returned unexpected response: StatusCode=$($response.StatusCode)" -Level 'VERBOSE'
    }
    catch [System.Net.WebException] {
        Write-vSEALLog "NCSI endpoint test failed (WebException): $($_.Exception.Message)" -Level 'VERBOSE'
    }
    catch [System.Net.Http.HttpRequestException] {
        Write-vSEALLog "NCSI endpoint test failed (HttpRequestException): $($_.Exception.Message)" -Level 'VERBOSE'
    }
    catch {
        Write-vSEALLog "NCSI endpoint test failed: $($_.Exception.Message)" -Level 'VERBOSE'
    }

    # Fallback: TCP connection to Cloudflare DNS on port 443
    try {
        Write-vSEALLog "Falling back to TCP connection test (1.1.1.1:443)..." -Level 'VERBOSE'

        $tcpResult = Test-NetConnection -ComputerName '1.1.1.1' -Port 443 `
            -WarningAction SilentlyContinue `
            -ErrorAction Stop

        if ($tcpResult.TcpTestSucceeded) {
            Write-vSEALLog "Internet connectivity confirmed via TCP to 1.1.1.1:443" -Level 'VERBOSE'
            return $true
        }

        Write-vSEALLog "TCP connection to 1.1.1.1:443 failed" -Level 'VERBOSE'
    }
    catch {
        Write-vSEALLog "TCP fallback test failed: $($_.Exception.Message)" -Level 'VERBOSE'
    }

    Write-vSEALLog "All internet connectivity tests failed" -Level 'VERBOSE'
    return $false
}
