<#
.SYNOPSIS
    Windows Service Health Prometheus Metrics Exporter
.DESCRIPTION
    Prometheus exporter for Windows service health - monitors service state,
    startup type, process ID, memory usage, CPU time, and uptime. The Windows
    equivalent of the systemd-service-exporter. Exports metrics as Prometheus-
    compatible text format for windows_exporter textfile collector.
.PARAMETER ServiceList
    Comma-separated list of services to monitor (optional, monitors all
    auto-start services by default)
.PARAMETER ConfigFile
    Path to config file with one service name per line
    (default: C:\ProgramData\windows-service-health-exporter\services.conf)
.PARAMETER Mode
    Output mode: 'stdout' (default), 'textfile', or 'http'
.PARAMETER Port
    HTTP port for http mode (default: 9199)
.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:
        - windows_service_up
        - windows_service_exporter_info{version}

      Service State:
        - windows_service_state{service,display_name,state}
        - windows_service_start_type{service,start_type}
        - windows_service_pid{service}
        - windows_service_memory_bytes{service}
        - windows_service_cpu_seconds_total{service}
        - windows_service_uptime_seconds{service}

      Summary:
        - windows_service_autostart_stopped_total
        - windows_service_total_monitored

      Exporter:
        - windows_service_exporter_duration_seconds
        - windows_service_exporter_last_run_timestamp
#>

param(
    [string]$ServiceList,

    [string]$ConfigFile = 'C:\ProgramData\windows-service-health-exporter\services.conf',

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

    [int]$Port = 9199,

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

    [switch]$InstallScheduledTask,

    [int]$TaskIntervalMinutes = 2
)

# Create a scheduled task to run this script every $TaskIntervalMinutes minutes
# The task will run as SYSTEM and will be set to run at startup
if ($InstallScheduledTask) {
    $taskName = "WindowsServiceHealthExporter"
    $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 Windows service health 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)
}

function Get-MonitoredServices {
    $services = @()

    # Priority 1: -ServiceList parameter
    if ($ServiceList) {
        $services = $ServiceList -split ',' | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne '' }
    }
    # Priority 2: Config file
    elseif (Test-Path $ConfigFile) {
        $services = Get-Content $ConfigFile | ForEach-Object { $_.Trim() } |
            Where-Object { $_ -ne '' -and $_ -notmatch '^\s*#' }
    }

    # Default: all auto-start services
    if ($services.Count -eq 0) {
        $services = Get-CimInstance Win32_Service -Filter "StartMode='Auto'" -ErrorAction SilentlyContinue |
            Select-Object -ExpandProperty Name
    }

    return $services
}

# ============================================================================
# SERVICE METRICS
# ============================================================================

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

    if (-not $serviceNames -or $serviceNames.Count -eq 0) {
        return $sb.ToString()
    }

    # Fetch all CIM service objects in one call for performance
    $cimServices = @{}
    try {
        Get-CimInstance Win32_Service -ErrorAction Stop | ForEach-Object {
            $cimServices[$_.Name] = $_
        }
    } catch {
        Write-Warning "Failed to query Win32_Service: $_"
    }

    # Cache running processes by PID
    $processes = @{}
    try {
        Get-Process -ErrorAction Stop | ForEach-Object {
            $processes[$_.Id] = $_
        }
    } catch {
        Write-Warning "Failed to query processes: $_"
    }

    $states = @('Running', 'Stopped', 'Paused', 'StartPending', 'StopPending')
    $autostartStoppedCount = 0
    $now = Get-Date

    # --- windows_service_state ---
    [void]$sb.AppendLine('# HELP windows_service_state Current state of the Windows service (1=current state, 0=other states)')
    [void]$sb.AppendLine('# TYPE windows_service_state gauge')

    foreach ($svcName in $serviceNames) {
        try {
            $svc = Get-Service -Name $svcName -ErrorAction Stop
            $displayName = $svc.DisplayName -replace '[\\"]', ''
            $currentState = $svc.Status.ToString()

            foreach ($state in $states) {
                $val = if ($currentState -eq $state) { 1 } else { 0 }
                [void]$sb.AppendLine("windows_service_state{service=`"$svcName`",display_name=`"$displayName`",state=`"$state`"} $val")
            }
        } catch {
            foreach ($state in $states) {
                [void]$sb.AppendLine("windows_service_state{service=`"$svcName`",display_name=`"`",state=`"$state`"} 0")
            }
        }
    }
    [void]$sb.AppendLine('')

    # --- windows_service_start_type ---
    [void]$sb.AppendLine('# HELP windows_service_start_type Startup type of the Windows service (1=current type, 0=other types)')
    [void]$sb.AppendLine('# TYPE windows_service_start_type gauge')

    $startTypes = @('Automatic', 'Manual', 'Disabled')

    foreach ($svcName in $serviceNames) {
        $cimSvc = $cimServices[$svcName]
        $currentType = ''
        if ($cimSvc) {
            $currentType = switch ($cimSvc.StartMode) {
                'Auto'     { 'Automatic' }
                'Manual'   { 'Manual' }
                'Disabled' { 'Disabled' }
                default    { $cimSvc.StartMode }
            }
        }

        foreach ($type in $startTypes) {
            $val = if ($currentType -eq $type) { 1 } else { 0 }
            [void]$sb.AppendLine("windows_service_start_type{service=`"$svcName`",start_type=`"$type`"} $val")
        }
    }
    [void]$sb.AppendLine('')

    # --- windows_service_pid ---
    [void]$sb.AppendLine('# HELP windows_service_pid Process ID of the running service')
    [void]$sb.AppendLine('# TYPE windows_service_pid gauge')

    foreach ($svcName in $serviceNames) {
        $cimSvc = $cimServices[$svcName]
        $pid_val = if ($cimSvc -and $cimSvc.ProcessId -and $cimSvc.ProcessId -ne 0) { $cimSvc.ProcessId } else { 0 }
        [void]$sb.AppendLine("windows_service_pid{service=`"$svcName`"} $pid_val")
    }
    [void]$sb.AppendLine('')

    # --- windows_service_memory_bytes ---
    [void]$sb.AppendLine('# HELP windows_service_memory_bytes Working set memory in bytes of the service process')
    [void]$sb.AppendLine('# TYPE windows_service_memory_bytes gauge')

    foreach ($svcName in $serviceNames) {
        $memBytes = 0
        $cimSvc = $cimServices[$svcName]
        if ($cimSvc -and $cimSvc.ProcessId -and $cimSvc.ProcessId -ne 0) {
            $proc = $processes[[int]$cimSvc.ProcessId]
            if ($proc) {
                $memBytes = $proc.WorkingSet64
            }
        }
        [void]$sb.AppendLine("windows_service_memory_bytes{service=`"$svcName`"} $memBytes")
    }
    [void]$sb.AppendLine('')

    # --- windows_service_cpu_seconds_total ---
    [void]$sb.AppendLine('# HELP windows_service_cpu_seconds_total Total CPU time in seconds consumed by the service process')
    [void]$sb.AppendLine('# TYPE windows_service_cpu_seconds_total counter')

    foreach ($svcName in $serviceNames) {
        $cpuSeconds = 0
        $cimSvc = $cimServices[$svcName]
        if ($cimSvc -and $cimSvc.ProcessId -and $cimSvc.ProcessId -ne 0) {
            $proc = $processes[[int]$cimSvc.ProcessId]
            if ($proc) {
                $cpuSeconds = Format-MetricValue $proc.CPU
            }
        }
        [void]$sb.AppendLine("windows_service_cpu_seconds_total{service=`"$svcName`"} $cpuSeconds")
    }
    [void]$sb.AppendLine('')

    # --- windows_service_uptime_seconds ---
    [void]$sb.AppendLine('# HELP windows_service_uptime_seconds Seconds since the service process started')
    [void]$sb.AppendLine('# TYPE windows_service_uptime_seconds gauge')

    foreach ($svcName in $serviceNames) {
        $uptime = 0
        $cimSvc = $cimServices[$svcName]
        if ($cimSvc -and $cimSvc.ProcessId -and $cimSvc.ProcessId -ne 0) {
            $proc = $processes[[int]$cimSvc.ProcessId]
            if ($proc -and $proc.StartTime) {
                try {
                    $uptime = Format-MetricValue ($now - $proc.StartTime).TotalSeconds
                } catch {}
            }
        }
        [void]$sb.AppendLine("windows_service_uptime_seconds{service=`"$svcName`"} $uptime")
    }
    [void]$sb.AppendLine('')

    # --- windows_service_autostart_stopped_total ---
    foreach ($svcName in $serviceNames) {
        $cimSvc = $cimServices[$svcName]
        if ($cimSvc -and $cimSvc.StartMode -eq 'Auto' -and $cimSvc.State -ne 'Running') {
            $autostartStoppedCount++
        }
    }

    [void]$sb.AppendLine('# HELP windows_service_autostart_stopped_total Count of auto-start services that are not running')
    [void]$sb.AppendLine('# TYPE windows_service_autostart_stopped_total gauge')
    [void]$sb.AppendLine("windows_service_autostart_stopped_total $autostartStoppedCount")
    [void]$sb.AppendLine('')

    # --- windows_service_total_monitored ---
    [void]$sb.AppendLine('# HELP windows_service_total_monitored Total number of services being monitored')
    [void]$sb.AppendLine('# TYPE windows_service_total_monitored gauge')
    [void]$sb.AppendLine("windows_service_total_monitored $($serviceNames.Count)")
    [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 windows_service_up Exporter status (1=up, 0=down)')
    [void]$sb.AppendLine('# TYPE windows_service_up gauge')
    [void]$sb.AppendLine('windows_service_up 1')
    [void]$sb.AppendLine('')

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

    # Collect service metrics
    [void]$sb.Append((Get-ServiceMetrics))

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

    [void]$sb.AppendLine('# HELP windows_service_exporter_duration_seconds Time to generate all metrics')
    [void]$sb.AppendLine('# TYPE windows_service_exporter_duration_seconds gauge')
    [void]$sb.AppendLine("windows_service_exporter_duration_seconds $duration")
    [void]$sb.AppendLine('')
    [void]$sb.AppendLine('# HELP windows_service_exporter_last_run_timestamp Unix timestamp of last successful run')
    [void]$sb.AppendLine('# TYPE windows_service_exporter_last_run_timestamp gauge')
    [void]$sb.AppendLine("windows_service_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 Windows service health 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>Windows Service Health Exporter v1.0</title></head>
<body>
<h1>Windows Service Health Exporter v1.0</h1>
<p><a href="/metrics">Metrics</a></p>
<h2>Metrics</h2>
<ul>
<li>Service state (Running, Stopped, Paused, StartPending, StopPending)</li>
<li>Startup type (Automatic, Manual, Disabled)</li>
<li>Process ID, memory usage, CPU time</li>
<li>Service uptime</li>
<li>Auto-start services stopped count</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 'windows_service_health.prom'

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

        $tempFile = Join-Path $outputDir ".windows_service_health_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
    }
}
