<#
.SYNOPSIS
    Windows Scheduled Task Prometheus Metrics Exporter
.DESCRIPTION
    Prometheus exporter for Windows scheduled tasks - monitors task state,
    last result, run timestamps, missed runs, and enabled status. Exports
    metrics as Prometheus-compatible text format for windows_exporter textfile
    collector.
.PARAMETER TaskPath
    The scheduled task folder path to monitor (default: "\" for all)
.PARAMETER ExcludePattern
    Regex pattern to exclude task paths (default: '\\Microsoft\\' to filter
    built-in Windows tasks)
.PARAMETER Mode
    Output mode: 'stdout' (default), 'textfile', or 'http'
.PARAMETER Port
    HTTP port for http mode (default: 9584)
.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: 5)
.NOTES
    Author:  Phil Connor
    Contact: contact@mylinux.work
    Website: https://mylinux.work
    License: MIT
    Version: 1.0

    Metrics Exported:
      Core Status:
        - windows_scheduled_task_up
        - windows_scheduled_task_exporter_info{version}

      Task State:
        - windows_scheduled_task_state{name,path,state}
        - windows_scheduled_task_last_result{name,path}
        - windows_scheduled_task_last_run_timestamp{name,path}
        - windows_scheduled_task_next_run_timestamp{name,path}
        - windows_scheduled_task_missed_runs{name,path}
        - windows_scheduled_task_enabled{name,path}

      Summary:
        - windows_scheduled_task_total
        - windows_scheduled_task_failed_total
        - windows_scheduled_task_disabled_total
        - windows_scheduled_task_running_total

      Exporter:
        - windows_scheduled_task_exporter_duration_seconds
        - windows_scheduled_task_exporter_last_run_timestamp
#>

param(
    [string]$TaskPath = '\',

    [string]$ExcludePattern = '\\Microsoft\\',

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

    [int]$Port = 9584,

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

    [switch]$InstallScheduledTask,

    [int]$TaskIntervalMinutes = 5
)

# 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 = "WindowsScheduledTaskExporter"
    $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 scheduled task 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)
}

# ============================================================================
# SCHEDULED TASK METRICS
# ============================================================================

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

    # Fetch all scheduled tasks
    $tasks = @()
    try {
        $allTasks = Get-ScheduledTask -TaskPath "$TaskPath*" -ErrorAction Stop
        if ($ExcludePattern) {
            $tasks = $allTasks | Where-Object { $_.TaskPath -notmatch $ExcludePattern }
        } else {
            $tasks = $allTasks
        }
    } catch {
        Write-Warning "Failed to query scheduled tasks: $_"
    }

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

    # Fetch task info for all tasks
    $taskInfoMap = @{}
    foreach ($task in $tasks) {
        try {
            $info = Get-ScheduledTaskInfo -TaskName $task.TaskName -TaskPath $task.TaskPath -ErrorAction Stop
            $taskInfoMap["$($task.TaskPath)$($task.TaskName)"] = $info
        } catch {}
    }

    $states = @('Ready', 'Running', 'Disabled', 'Queued')
    $failedCount = 0
    $disabledCount = 0
    $runningCount = 0

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

    foreach ($task in $tasks) {
        try {
            $name = $task.TaskName -replace '[\\"]', ''
            $path = $task.TaskPath -replace '[\\"]', ''
            $currentState = $task.State.ToString()

            foreach ($state in $states) {
                $val = if ($currentState -eq $state) { 1 } else { 0 }
                [void]$sb.AppendLine("windows_scheduled_task_state{name=`"$name`",path=`"$path`",state=`"$state`"} $val")
            }
        } catch {
            $name = $task.TaskName -replace '[\\"]', ''
            $path = $task.TaskPath -replace '[\\"]', ''
            foreach ($state in $states) {
                [void]$sb.AppendLine("windows_scheduled_task_state{name=`"$name`",path=`"$path`",state=`"$state`"} 0")
            }
        }
    }
    [void]$sb.AppendLine('')

    # --- windows_scheduled_task_last_result ---
    [void]$sb.AppendLine('# HELP windows_scheduled_task_last_result Last result code of the scheduled task (0=success)')
    [void]$sb.AppendLine('# TYPE windows_scheduled_task_last_result gauge')

    foreach ($task in $tasks) {
        try {
            $name = $task.TaskName -replace '[\\"]', ''
            $path = $task.TaskPath -replace '[\\"]', ''
            $info = $taskInfoMap["$($task.TaskPath)$($task.TaskName)"]
            $lastResult = if ($info) { $info.LastTaskResult } else { -1 }
            [void]$sb.AppendLine("windows_scheduled_task_last_result{name=`"$name`",path=`"$path`"} $lastResult")

            if ($lastResult -ne 0) {
                $failedCount++
            }
        } catch {
            $name = $task.TaskName -replace '[\\"]', ''
            $path = $task.TaskPath -replace '[\\"]', ''
            [void]$sb.AppendLine("windows_scheduled_task_last_result{name=`"$name`",path=`"$path`"} -1")
        }
    }
    [void]$sb.AppendLine('')

    # --- windows_scheduled_task_last_run_timestamp ---
    [void]$sb.AppendLine('# HELP windows_scheduled_task_last_run_timestamp Unix timestamp of the last run')
    [void]$sb.AppendLine('# TYPE windows_scheduled_task_last_run_timestamp gauge')

    foreach ($task in $tasks) {
        try {
            $name = $task.TaskName -replace '[\\"]', ''
            $path = $task.TaskPath -replace '[\\"]', ''
            $info = $taskInfoMap["$($task.TaskPath)$($task.TaskName)"]
            $lastRun = 0
            if ($info -and $info.LastRunTime -and $info.LastRunTime -ne [datetime]::MinValue) {
                $lastRun = [int][double]::Parse((Get-Date $info.LastRunTime -UFormat '%s'))
            }
            [void]$sb.AppendLine("windows_scheduled_task_last_run_timestamp{name=`"$name`",path=`"$path`"} $lastRun")
        } catch {
            $name = $task.TaskName -replace '[\\"]', ''
            $path = $task.TaskPath -replace '[\\"]', ''
            [void]$sb.AppendLine("windows_scheduled_task_last_run_timestamp{name=`"$name`",path=`"$path`"} 0")
        }
    }
    [void]$sb.AppendLine('')

    # --- windows_scheduled_task_next_run_timestamp ---
    [void]$sb.AppendLine('# HELP windows_scheduled_task_next_run_timestamp Unix timestamp of the next scheduled run')
    [void]$sb.AppendLine('# TYPE windows_scheduled_task_next_run_timestamp gauge')

    foreach ($task in $tasks) {
        try {
            $name = $task.TaskName -replace '[\\"]', ''
            $path = $task.TaskPath -replace '[\\"]', ''
            $info = $taskInfoMap["$($task.TaskPath)$($task.TaskName)"]
            $nextRun = 0
            if ($info -and $info.NextRunTime -and $info.NextRunTime -ne [datetime]::MinValue) {
                $nextRun = [int][double]::Parse((Get-Date $info.NextRunTime -UFormat '%s'))
            }
            [void]$sb.AppendLine("windows_scheduled_task_next_run_timestamp{name=`"$name`",path=`"$path`"} $nextRun")
        } catch {
            $name = $task.TaskName -replace '[\\"]', ''
            $path = $task.TaskPath -replace '[\\"]', ''
            [void]$sb.AppendLine("windows_scheduled_task_next_run_timestamp{name=`"$name`",path=`"$path`"} 0")
        }
    }
    [void]$sb.AppendLine('')

    # --- windows_scheduled_task_missed_runs ---
    [void]$sb.AppendLine('# HELP windows_scheduled_task_missed_runs Number of missed runs for the scheduled task')
    [void]$sb.AppendLine('# TYPE windows_scheduled_task_missed_runs gauge')

    foreach ($task in $tasks) {
        try {
            $name = $task.TaskName -replace '[\\"]', ''
            $path = $task.TaskPath -replace '[\\"]', ''
            $info = $taskInfoMap["$($task.TaskPath)$($task.TaskName)"]
            $missed = if ($info) { $info.NumberOfMissedRuns } else { 0 }
            [void]$sb.AppendLine("windows_scheduled_task_missed_runs{name=`"$name`",path=`"$path`"} $missed")
        } catch {
            $name = $task.TaskName -replace '[\\"]', ''
            $path = $task.TaskPath -replace '[\\"]', ''
            [void]$sb.AppendLine("windows_scheduled_task_missed_runs{name=`"$name`",path=`"$path`"} 0")
        }
    }
    [void]$sb.AppendLine('')

    # --- windows_scheduled_task_enabled ---
    [void]$sb.AppendLine('# HELP windows_scheduled_task_enabled Whether the scheduled task is enabled (1=enabled, 0=disabled)')
    [void]$sb.AppendLine('# TYPE windows_scheduled_task_enabled gauge')

    foreach ($task in $tasks) {
        try {
            $name = $task.TaskName -replace '[\\"]', ''
            $path = $task.TaskPath -replace '[\\"]', ''
            $enabled = if ($task.State -ne 'Disabled') { 1 } else { 0 }
            [void]$sb.AppendLine("windows_scheduled_task_enabled{name=`"$name`",path=`"$path`"} $enabled")

            if ($task.State.ToString() -eq 'Disabled') {
                $disabledCount++
            }
            if ($task.State.ToString() -eq 'Running') {
                $runningCount++
            }
        } catch {
            $name = $task.TaskName -replace '[\\"]', ''
            $path = $task.TaskPath -replace '[\\"]', ''
            [void]$sb.AppendLine("windows_scheduled_task_enabled{name=`"$name`",path=`"$path`"} 0")
        }
    }
    [void]$sb.AppendLine('')

    # --- windows_scheduled_task_total ---
    [void]$sb.AppendLine('# HELP windows_scheduled_task_total Total number of monitored scheduled tasks')
    [void]$sb.AppendLine('# TYPE windows_scheduled_task_total gauge')
    [void]$sb.AppendLine("windows_scheduled_task_total $($tasks.Count)")
    [void]$sb.AppendLine('')

    # --- windows_scheduled_task_failed_total ---
    [void]$sb.AppendLine('# HELP windows_scheduled_task_failed_total Count of scheduled tasks with non-zero last result')
    [void]$sb.AppendLine('# TYPE windows_scheduled_task_failed_total gauge')
    [void]$sb.AppendLine("windows_scheduled_task_failed_total $failedCount")
    [void]$sb.AppendLine('')

    # --- windows_scheduled_task_disabled_total ---
    [void]$sb.AppendLine('# HELP windows_scheduled_task_disabled_total Count of disabled scheduled tasks')
    [void]$sb.AppendLine('# TYPE windows_scheduled_task_disabled_total gauge')
    [void]$sb.AppendLine("windows_scheduled_task_disabled_total $disabledCount")
    [void]$sb.AppendLine('')

    # --- windows_scheduled_task_running_total ---
    [void]$sb.AppendLine('# HELP windows_scheduled_task_running_total Count of currently running scheduled tasks')
    [void]$sb.AppendLine('# TYPE windows_scheduled_task_running_total gauge')
    [void]$sb.AppendLine("windows_scheduled_task_running_total $runningCount")
    [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_scheduled_task_up Exporter status (1=up, 0=down)')
    [void]$sb.AppendLine('# TYPE windows_scheduled_task_up gauge')
    [void]$sb.AppendLine('windows_scheduled_task_up 1')
    [void]$sb.AppendLine('')

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

    # Collect task metrics
    [void]$sb.Append((Get-TaskMetrics))

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

    [void]$sb.AppendLine('# HELP windows_scheduled_task_exporter_duration_seconds Time to generate all metrics')
    [void]$sb.AppendLine('# TYPE windows_scheduled_task_exporter_duration_seconds gauge')
    [void]$sb.AppendLine("windows_scheduled_task_exporter_duration_seconds $duration")
    [void]$sb.AppendLine('')
    [void]$sb.AppendLine('# HELP windows_scheduled_task_exporter_last_run_timestamp Unix timestamp of last successful run')
    [void]$sb.AppendLine('# TYPE windows_scheduled_task_exporter_last_run_timestamp gauge')
    [void]$sb.AppendLine("windows_scheduled_task_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 scheduled task 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 Scheduled Task Exporter v1.0</title></head>
<body>
<h1>Windows Scheduled Task Exporter v1.0</h1>
<p><a href="/metrics">Metrics</a></p>
<h2>Metrics</h2>
<ul>
<li>Task state (Ready, Running, Disabled, Queued)</li>
<li>Last result code, last run time, next run time</li>
<li>Missed runs, enabled status</li>
<li>Total, failed, disabled, and running task counts</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_scheduled_task.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_scheduled_task_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
    }
}
