<#
.SYNOPSIS
    Hyper-V Prometheus Metrics Exporter
.DESCRIPTION
    Prometheus exporter for Hyper-V virtual machines - VM state, CPU usage,
    memory allocation, disk usage, network throughput, snapshot counts,
    replication status, and host resource consumption. Exports metrics as
    Prometheus-compatible text format.
.PARAMETER Mode
    Output mode: 'stdout' (default), 'textfile', or 'http'
.PARAMETER Port
    HTTP port for http mode (default: 9516)
.PARAMETER TextfileDir
    Directory for textfile collector output (default: C:\ProgramData\node_exporter)
.PARAMETER InstallScheduledTask
    Switch to create a scheduled task for auto-start on system boot
.PARAMETER TaskIntervalMinutes
    Interval in minutes for the scheduled task (default: 2)
.NOTES
    Author:  Phil Connor
    Contact: contact@mylinux.work
    Website: https://mylinux.work
    License: MIT
    Version: 1.0

    Metrics Exported:
      Core Status:
        - hyperv_up
        - hyperv_exporter_info{version}

      VM State:
        - hyperv_vm_state{vm,state}
        - hyperv_vm_uptime_seconds{vm}
        - hyperv_vm_generation{vm}

      CPU:
        - hyperv_vm_cpu_count{vm}
        - hyperv_vm_cpu_usage_percent{vm}

      Memory:
        - hyperv_vm_memory_assigned_bytes{vm}
        - hyperv_vm_memory_demand_bytes{vm}
        - hyperv_vm_memory_startup_bytes{vm}
        - hyperv_vm_dynamic_memory_enabled{vm}

      Disk:
        - hyperv_vm_vhd_size_bytes{vm,path}
        - hyperv_vm_vhd_current_size_bytes{vm,path}

      Network:
        - hyperv_vm_network_adapter_bytes_sent{vm,adapter}
        - hyperv_vm_network_adapter_bytes_received{vm,adapter}

      Snapshots:
        - hyperv_vm_snapshot_count{vm}

      Replication:
        - hyperv_vm_replication_state{vm,state}
        - hyperv_vm_replication_health{vm,health}

      Host:
        - hyperv_host_logical_processors
        - hyperv_host_memory_total_bytes
        - hyperv_host_memory_available_bytes
        - hyperv_host_vm_count_total
        - hyperv_host_vm_running_count

      Summary:
        - hyperv_vm_total
        - hyperv_vm_running_total
        - hyperv_vm_stopped_total

      Exporter:
        - hyperv_exporter_duration_seconds
        - hyperv_exporter_last_run_timestamp
#>

param(
    [ValidateSet('stdout', 'textfile', 'http')]
    [string]$Mode = 'stdout',

    [int]$Port = 9516,

    [string]$TextfileDir = 'C:\ProgramData\node_exporter',

    [switch]$InstallScheduledTask,

    [int]$TaskIntervalMinutes = 2
)

# Create a scheduled task to run this script every $TaskIntervalMinutes minutes
if ($InstallScheduledTask) {
    $taskName = "HyperVMetricsExporter"
    $existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue

    if (-not $existingTask) {
        $taskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Path)`" -Mode textfile"

        if (-not $TaskIntervalMinutes -or $TaskIntervalMinutes -le 0) {
            throw "TaskIntervalMinutes must be a positive integer"
        }

        $taskTrigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) -RepetitionInterval (New-TimeSpan -Minutes $TaskIntervalMinutes) -RepetitionDuration (New-TimeSpan -Days 365)
        $taskPrincipal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest

        try {
            Write-Host "Creating scheduled task: $taskName"
            Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Principal $taskPrincipal -Description "Exports Hyper-V metrics for Prometheus every $TaskIntervalMinutes minutes"

            $createdTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
            if (-not $createdTask) {
                throw "Failed to verify scheduled task creation"
            }
            Write-Host "Successfully created scheduled task: $taskName" -ForegroundColor Green
        } catch {
            Write-Error "Failed to create scheduled task: $($_.Exception.Message)"
            throw
        }
    } else {
        Write-Host "Scheduled task '$taskName' already exists, skipping creation"
    }
}

$ErrorActionPreference = 'SilentlyContinue'

# ============================================================================
# HELPER FUNCTIONS
# ============================================================================

function Get-UnixTimestamp {
    [int][double]::Parse((Get-Date -UFormat '%s'))
}

function Format-MetricValue {
    param([double]$Value, [int]$Decimals = 2)
    [math]::Round($Value, $Decimals)
}

# ============================================================================
# VM METRICS
# ============================================================================

function Get-VMMetrics {
    $sb = [System.Text.StringBuilder]::new()

    # Get all VMs
    $vms = @()
    try {
        $vms = Get-VM -ErrorAction Stop
    } catch {
        Write-Warning "Failed to query Hyper-V VMs: $_"
        return $sb.ToString()
    }

    # --- hyperv_vm_state ---
    $states = @('Running', 'Off', 'Saved', 'Paused', 'Starting', 'Stopping', 'Reset')
    [void]$sb.AppendLine('# HELP hyperv_vm_state Current state of the VM (1=current state, 0=other states)')
    [void]$sb.AppendLine('# TYPE hyperv_vm_state gauge')

    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        $currentState = $vm.State.ToString()
        foreach ($state in $states) {
            $val = if ($currentState -eq $state) { 1 } else { 0 }
            [void]$sb.AppendLine("hyperv_vm_state{vm=`"$vmName`",state=`"$state`"} $val")
        }
    }
    [void]$sb.AppendLine('')

    # --- hyperv_vm_uptime_seconds ---
    [void]$sb.AppendLine('# HELP hyperv_vm_uptime_seconds VM uptime in seconds')
    [void]$sb.AppendLine('# TYPE hyperv_vm_uptime_seconds gauge')

    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        $uptime = if ($vm.Uptime) { Format-MetricValue $vm.Uptime.TotalSeconds 0 } else { 0 }
        [void]$sb.AppendLine("hyperv_vm_uptime_seconds{vm=`"$vmName`"} $uptime")
    }
    [void]$sb.AppendLine('')

    # --- hyperv_vm_generation ---
    [void]$sb.AppendLine('# HELP hyperv_vm_generation VM generation (1 or 2)')
    [void]$sb.AppendLine('# TYPE hyperv_vm_generation gauge')

    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        $gen = if ($vm.Generation) { $vm.Generation } else { 1 }
        [void]$sb.AppendLine("hyperv_vm_generation{vm=`"$vmName`"} $gen")
    }
    [void]$sb.AppendLine('')

    # --- hyperv_vm_cpu_count ---
    [void]$sb.AppendLine('# HELP hyperv_vm_cpu_count Number of virtual processors assigned to VM')
    [void]$sb.AppendLine('# TYPE hyperv_vm_cpu_count gauge')

    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        $cpuCount = if ($vm.ProcessorCount) { $vm.ProcessorCount } else { 0 }
        [void]$sb.AppendLine("hyperv_vm_cpu_count{vm=`"$vmName`"} $cpuCount")
    }
    [void]$sb.AppendLine('')

    # --- hyperv_vm_cpu_usage_percent ---
    [void]$sb.AppendLine('# HELP hyperv_vm_cpu_usage_percent Current CPU usage percentage of the VM')
    [void]$sb.AppendLine('# TYPE hyperv_vm_cpu_usage_percent gauge')

    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        $cpuUsage = if ($vm.CPUUsage -ne $null) { $vm.CPUUsage } else { 0 }
        [void]$sb.AppendLine("hyperv_vm_cpu_usage_percent{vm=`"$vmName`"} $cpuUsage")
    }
    [void]$sb.AppendLine('')

    # --- hyperv_vm_memory_assigned_bytes ---
    [void]$sb.AppendLine('# HELP hyperv_vm_memory_assigned_bytes Currently assigned memory in bytes')
    [void]$sb.AppendLine('# TYPE hyperv_vm_memory_assigned_bytes gauge')

    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        $memAssigned = if ($vm.MemoryAssigned) { $vm.MemoryAssigned } else { 0 }
        [void]$sb.AppendLine("hyperv_vm_memory_assigned_bytes{vm=`"$vmName`"} $memAssigned")
    }
    [void]$sb.AppendLine('')

    # --- hyperv_vm_memory_demand_bytes ---
    [void]$sb.AppendLine('# HELP hyperv_vm_memory_demand_bytes Current memory demand in bytes')
    [void]$sb.AppendLine('# TYPE hyperv_vm_memory_demand_bytes gauge')

    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        $memDemand = if ($vm.MemoryDemand) { $vm.MemoryDemand } else { 0 }
        [void]$sb.AppendLine("hyperv_vm_memory_demand_bytes{vm=`"$vmName`"} $memDemand")
    }
    [void]$sb.AppendLine('')

    # --- hyperv_vm_memory_startup_bytes ---
    [void]$sb.AppendLine('# HELP hyperv_vm_memory_startup_bytes Configured startup memory in bytes')
    [void]$sb.AppendLine('# TYPE hyperv_vm_memory_startup_bytes gauge')

    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        $memStartup = if ($vm.MemoryStartup) { $vm.MemoryStartup } else { 0 }
        [void]$sb.AppendLine("hyperv_vm_memory_startup_bytes{vm=`"$vmName`"} $memStartup")
    }
    [void]$sb.AppendLine('')

    # --- hyperv_vm_dynamic_memory_enabled ---
    [void]$sb.AppendLine('# HELP hyperv_vm_dynamic_memory_enabled Whether dynamic memory is enabled (1=yes, 0=no)')
    [void]$sb.AppendLine('# TYPE hyperv_vm_dynamic_memory_enabled gauge')

    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        $dynMem = if ($vm.DynamicMemoryEnabled) { 1 } else { 0 }
        [void]$sb.AppendLine("hyperv_vm_dynamic_memory_enabled{vm=`"$vmName`"} $dynMem")
    }
    [void]$sb.AppendLine('')

    # --- hyperv_vm_vhd_size_bytes / hyperv_vm_vhd_current_size_bytes ---
    [void]$sb.AppendLine('# HELP hyperv_vm_vhd_size_bytes Maximum size of the VHD/VHDX in bytes')
    [void]$sb.AppendLine('# TYPE hyperv_vm_vhd_size_bytes gauge')

    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        try {
            $hdds = Get-VMHardDiskDrive -VMName $vm.Name -ErrorAction Stop
            foreach ($hdd in $hdds) {
                $vhdPath = $hdd.Path -replace '\\', '/'
                try {
                    $vhd = Get-VHD -Path $hdd.Path -ErrorAction Stop
                    [void]$sb.AppendLine("hyperv_vm_vhd_size_bytes{vm=`"$vmName`",path=`"$vhdPath`"} $($vhd.Size)")
                } catch {}
            }
        } catch {}
    }
    [void]$sb.AppendLine('')

    [void]$sb.AppendLine('# HELP hyperv_vm_vhd_current_size_bytes Current file size of the VHD/VHDX in bytes')
    [void]$sb.AppendLine('# TYPE hyperv_vm_vhd_current_size_bytes gauge')

    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        try {
            $hdds = Get-VMHardDiskDrive -VMName $vm.Name -ErrorAction Stop
            foreach ($hdd in $hdds) {
                $vhdPath = $hdd.Path -replace '\\', '/'
                try {
                    $vhd = Get-VHD -Path $hdd.Path -ErrorAction Stop
                    [void]$sb.AppendLine("hyperv_vm_vhd_current_size_bytes{vm=`"$vmName`",path=`"$vhdPath`"} $($vhd.FileSize)")
                } catch {}
            }
        } catch {}
    }
    [void]$sb.AppendLine('')

    # --- hyperv_vm_network_adapter ---
    [void]$sb.AppendLine('# HELP hyperv_vm_network_adapter_bytes_sent Total bytes sent by VM network adapter')
    [void]$sb.AppendLine('# TYPE hyperv_vm_network_adapter_bytes_sent counter')

    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        try {
            $adapters = Get-VMNetworkAdapter -VMName $vm.Name -ErrorAction Stop
            foreach ($adapter in $adapters) {
                $adapterName = if ($adapter.Name) { $adapter.Name } else { "Network Adapter" }
                $adapterName = $adapterName -replace '[\\"]', ''
                $sent = if ($adapter.BandwidthSetting) { 0 } else { 0 }
                # Use performance counters for actual byte counts
                try {
                    $counterPath = "\Hyper-V Virtual Network Adapter($vmName - $adapterName)\Bytes Sent/sec"
                    $counter = Get-Counter $counterPath -ErrorAction Stop
                    $sent = [long]$counter.CounterSamples[0].CookedValue
                } catch { $sent = 0 }
                [void]$sb.AppendLine("hyperv_vm_network_adapter_bytes_sent{vm=`"$vmName`",adapter=`"$adapterName`"} $sent")
            }
        } catch {}
    }
    [void]$sb.AppendLine('')

    [void]$sb.AppendLine('# HELP hyperv_vm_network_adapter_bytes_received Total bytes received by VM network adapter')
    [void]$sb.AppendLine('# TYPE hyperv_vm_network_adapter_bytes_received counter')

    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        try {
            $adapters = Get-VMNetworkAdapter -VMName $vm.Name -ErrorAction Stop
            foreach ($adapter in $adapters) {
                $adapterName = if ($adapter.Name) { $adapter.Name } else { "Network Adapter" }
                $adapterName = $adapterName -replace '[\\"]', ''
                $received = 0
                try {
                    $counterPath = "\Hyper-V Virtual Network Adapter($vmName - $adapterName)\Bytes Received/sec"
                    $counter = Get-Counter $counterPath -ErrorAction Stop
                    $received = [long]$counter.CounterSamples[0].CookedValue
                } catch { $received = 0 }
                [void]$sb.AppendLine("hyperv_vm_network_adapter_bytes_received{vm=`"$vmName`",adapter=`"$adapterName`"} $received")
            }
        } catch {}
    }
    [void]$sb.AppendLine('')

    # --- hyperv_vm_snapshot_count ---
    [void]$sb.AppendLine('# HELP hyperv_vm_snapshot_count Number of checkpoints (snapshots) for the VM')
    [void]$sb.AppendLine('# TYPE hyperv_vm_snapshot_count gauge')

    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        $snapCount = 0
        try {
            $snapCount = (Get-VMSnapshot -VMName $vm.Name -ErrorAction Stop | Measure-Object).Count
        } catch {}
        [void]$sb.AppendLine("hyperv_vm_snapshot_count{vm=`"$vmName`"} $snapCount")
    }
    [void]$sb.AppendLine('')

    # --- hyperv_vm_replication_state ---
    [void]$sb.AppendLine('# HELP hyperv_vm_replication_state VM replication state (1=current state)')
    [void]$sb.AppendLine('# TYPE hyperv_vm_replication_state gauge')

    $replStates = @('Disabled', 'ReadyForInitialReplication', 'InitialReplicationInProgress', 'WaitingForInitialReplication', 'Replicating', 'Suspended', 'Error', 'FailedOver', 'Recovered')
    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        $currentReplState = $vm.ReplicationState.ToString()
        foreach ($rs in $replStates) {
            $val = if ($currentReplState -eq $rs) { 1 } else { 0 }
            [void]$sb.AppendLine("hyperv_vm_replication_state{vm=`"$vmName`",state=`"$rs`"} $val")
        }
    }
    [void]$sb.AppendLine('')

    # --- hyperv_vm_replication_health ---
    [void]$sb.AppendLine('# HELP hyperv_vm_replication_health VM replication health (1=current health)')
    [void]$sb.AppendLine('# TYPE hyperv_vm_replication_health gauge')

    $replHealths = @('NotApplicable', 'Normal', 'Warning', 'Critical')
    foreach ($vm in $vms) {
        $vmName = $vm.Name -replace '[\\"]', ''
        $currentHealth = $vm.ReplicationHealth.ToString()
        foreach ($rh in $replHealths) {
            $val = if ($currentHealth -eq $rh) { 1 } else { 0 }
            [void]$sb.AppendLine("hyperv_vm_replication_health{vm=`"$vmName`",health=`"$rh`"} $val")
        }
    }
    [void]$sb.AppendLine('')

    # --- Summary metrics ---
    $runningCount = ($vms | Where-Object { $_.State -eq 'Running' } | Measure-Object).Count
    $stoppedCount = ($vms | Where-Object { $_.State -eq 'Off' } | Measure-Object).Count
    $totalCount = $vms.Count

    [void]$sb.AppendLine('# HELP hyperv_vm_total Total number of VMs')
    [void]$sb.AppendLine('# TYPE hyperv_vm_total gauge')
    [void]$sb.AppendLine("hyperv_vm_total $totalCount")
    [void]$sb.AppendLine('')

    [void]$sb.AppendLine('# HELP hyperv_vm_running_total Number of running VMs')
    [void]$sb.AppendLine('# TYPE hyperv_vm_running_total gauge')
    [void]$sb.AppendLine("hyperv_vm_running_total $runningCount")
    [void]$sb.AppendLine('')

    [void]$sb.AppendLine('# HELP hyperv_vm_stopped_total Number of stopped VMs')
    [void]$sb.AppendLine('# TYPE hyperv_vm_stopped_total gauge')
    [void]$sb.AppendLine("hyperv_vm_stopped_total $stoppedCount")
    [void]$sb.AppendLine('')

    $sb.ToString()
}

# ============================================================================
# HOST METRICS
# ============================================================================

function Get-HostMetrics {
    $sb = [System.Text.StringBuilder]::new()

    # --- hyperv_host_logical_processors ---
    [void]$sb.AppendLine('# HELP hyperv_host_logical_processors Number of logical processors on the host')
    [void]$sb.AppendLine('# TYPE hyperv_host_logical_processors gauge')
    try {
        $cpuCount = (Get-CimInstance Win32_Processor -ErrorAction Stop | Measure-Object -Property NumberOfLogicalProcessors -Sum).Sum
        [void]$sb.AppendLine("hyperv_host_logical_processors $cpuCount")
    } catch {
        [void]$sb.AppendLine("hyperv_host_logical_processors 0")
    }
    [void]$sb.AppendLine('')

    # --- hyperv_host_memory_total_bytes ---
    [void]$sb.AppendLine('# HELP hyperv_host_memory_total_bytes Total physical memory on the host')
    [void]$sb.AppendLine('# TYPE hyperv_host_memory_total_bytes gauge')
    try {
        $totalMem = (Get-CimInstance Win32_ComputerSystem -ErrorAction Stop).TotalPhysicalMemory
        [void]$sb.AppendLine("hyperv_host_memory_total_bytes $totalMem")
    } catch {
        [void]$sb.AppendLine("hyperv_host_memory_total_bytes 0")
    }
    [void]$sb.AppendLine('')

    # --- hyperv_host_memory_available_bytes ---
    [void]$sb.AppendLine('# HELP hyperv_host_memory_available_bytes Available physical memory on the host')
    [void]$sb.AppendLine('# TYPE hyperv_host_memory_available_bytes gauge')
    try {
        $availMem = (Get-CimInstance Win32_OperatingSystem -ErrorAction Stop).FreePhysicalMemory * 1024
        [void]$sb.AppendLine("hyperv_host_memory_available_bytes $availMem")
    } catch {
        [void]$sb.AppendLine("hyperv_host_memory_available_bytes 0")
    }
    [void]$sb.AppendLine('')

    # --- hyperv_host_vm_count_total ---
    [void]$sb.AppendLine('# HELP hyperv_host_vm_count_total Total VMs configured on host')
    [void]$sb.AppendLine('# TYPE hyperv_host_vm_count_total gauge')
    try {
        $vmCount = (Get-VM -ErrorAction Stop | Measure-Object).Count
        [void]$sb.AppendLine("hyperv_host_vm_count_total $vmCount")
    } catch {
        [void]$sb.AppendLine("hyperv_host_vm_count_total 0")
    }
    [void]$sb.AppendLine('')

    # --- hyperv_host_vm_running_count ---
    [void]$sb.AppendLine('# HELP hyperv_host_vm_running_count Number of VMs currently running')
    [void]$sb.AppendLine('# TYPE hyperv_host_vm_running_count gauge')
    try {
        $runCount = (Get-VM -ErrorAction Stop | Where-Object { $_.State -eq 'Running' } | Measure-Object).Count
        [void]$sb.AppendLine("hyperv_host_vm_running_count $runCount")
    } catch {
        [void]$sb.AppendLine("hyperv_host_vm_running_count 0")
    }
    [void]$sb.AppendLine('')

    $sb.ToString()
}

# ============================================================================
# COLLECT ALL METRICS
# ============================================================================

function Get-AllMetrics {
    $scriptStart = Get-Date
    $sb = [System.Text.StringBuilder]::new()

    # Exporter up
    [void]$sb.AppendLine('# HELP hyperv_up Exporter status (1=up, 0=down)')
    [void]$sb.AppendLine('# TYPE hyperv_up gauge')
    [void]$sb.AppendLine('hyperv_up 1')
    [void]$sb.AppendLine('')

    # Exporter info
    [void]$sb.AppendLine('# HELP hyperv_exporter_info Exporter version information')
    [void]$sb.AppendLine('# TYPE hyperv_exporter_info gauge')
    [void]$sb.AppendLine('hyperv_exporter_info{version="1.0"} 1')
    [void]$sb.AppendLine('')

    # Collect VM metrics
    [void]$sb.Append((Get-VMMetrics))

    # Collect host metrics
    [void]$sb.Append((Get-HostMetrics))

    # Exporter runtime
    $scriptEnd = Get-Date
    $duration = Format-MetricValue ($scriptEnd - $scriptStart).TotalSeconds
    $timestamp = Get-UnixTimestamp

    [void]$sb.AppendLine('# HELP hyperv_exporter_duration_seconds Time to generate all metrics')
    [void]$sb.AppendLine('# TYPE hyperv_exporter_duration_seconds gauge')
    [void]$sb.AppendLine("hyperv_exporter_duration_seconds $duration")
    [void]$sb.AppendLine('')
    [void]$sb.AppendLine('# HELP hyperv_exporter_last_run_timestamp Unix timestamp of last successful run')
    [void]$sb.AppendLine('# TYPE hyperv_exporter_last_run_timestamp gauge')
    [void]$sb.AppendLine("hyperv_exporter_last_run_timestamp $timestamp")
    [void]$sb.AppendLine('')

    $sb.ToString()
}

# ============================================================================
# HTTP SERVER MODE
# ============================================================================

function Start-HttpServer {
    param([int]$ListenPort)

    $prefix = "http://+:$ListenPort/"
    $listener = [System.Net.HttpListener]::new()
    $listener.Prefixes.Add($prefix)

    try {
        $listener.Start()
        Write-Host "Starting Hyper-V metrics exporter on port $ListenPort..." -ForegroundColor Green
        Write-Host "Metrics available at http://localhost:$ListenPort/metrics"

        while ($listener.IsListening) {
            $context  = $listener.GetContext()
            $request  = $context.Request
            $response = $context.Response

            if ($request.Url.AbsolutePath -eq '/metrics') {
                $metrics = Get-AllMetrics
                $buffer  = [System.Text.Encoding]::UTF8.GetBytes($metrics)
                $response.ContentType = 'text/plain; version=0.0.4; charset=utf-8'
            }
            else {
                $html = @"
<!DOCTYPE html>
<html>
<head><title>Hyper-V Metrics Exporter v1.0</title></head>
<body>
<h1>Hyper-V Metrics Exporter v1.0</h1>
<p><a href="/metrics">Metrics</a></p>
<h2>Metrics</h2>
<ul>
<li>VM state, uptime, generation</li>
<li>CPU count and usage</li>
<li>Memory assigned, demand, startup, dynamic memory</li>
<li>VHD size and current file size</li>
<li>Network adapter bytes sent/received</li>
<li>Snapshot count</li>
<li>Replication state and health</li>
<li>Host resource stats</li>
</ul>
</body>
</html>
"@
                $buffer = [System.Text.Encoding]::UTF8.GetBytes($html)
                $response.ContentType = 'text/html; charset=utf-8'
            }

            $response.ContentLength64 = $buffer.Length
            $response.OutputStream.Write($buffer, 0, $buffer.Length)
            $response.OutputStream.Close()
        }
    }
    catch {
        Write-Error "HTTP server error: $_"
        Write-Error "If access denied, run: netsh http add urlacl url=http://+:$ListenPort/ user=Everyone"
    }
    finally {
        if ($listener.IsListening) {
            $listener.Stop()
        }
    }
}

# ============================================================================
# MAIN EXECUTION
# ============================================================================

switch ($Mode) {
    'http' {
        Start-HttpServer -ListenPort $Port
    }
    'textfile' {
        $OutputFile = Join-Path $TextfileDir 'hyperv_metrics.prom'

        $outputDir = Split-Path $OutputFile -Parent
        if (-not (Test-Path $outputDir)) {
            New-Item -Path $outputDir -ItemType Directory -Force | Out-Null
        }

        $tempFile = Join-Path $outputDir ".hyperv_metrics.$PID.tmp"

        try {
            $metrics = Get-AllMetrics
            $metrics | Out-File -FilePath $tempFile -Encoding utf8 -NoNewline

            $lineCount = ($metrics -split "`n").Count
            if ($lineCount -lt 10) {
                Remove-Item -Path $tempFile -Force -ErrorAction SilentlyContinue
                Write-Error "Metrics file too small ($lineCount lines), keeping previous"
                exit 1
            }

            Move-Item -Path $tempFile -Destination $OutputFile -Force
            Write-Host "Metrics written to $OutputFile ($lineCount lines)" -ForegroundColor Green
        }
        catch {
            Remove-Item -Path $tempFile -Force -ErrorAction SilentlyContinue
            Write-Error "Failed to generate metrics: $_"
            exit 1
        }
    }
    default {
        Get-AllMetrics | Write-Output
    }
}
