# Windows Exporter Installation Script for HOMELAB-COMMAND
# Prometheus monitoring agent for Windows

Write-Host "=== Windows Exporter Installation ===" -ForegroundColor Cyan
Write-Host ""

# Download URL for latest version
$version = "0.28.1"
$downloadUrl = "https://github.com/prometheus-community/windows_exporter/releases/download/v$version/windows_exporter-$version-amd64.msi"
$installerPath = "$env:TEMP\windows_exporter.msi"

Write-Host "Downloading Windows Exporter v$version..." -ForegroundColor Yellow
try {
    Invoke-WebRequest -Uri $downloadUrl -OutFile $installerPath -UseBasicParsing
    Write-Host "✅ Download complete: $installerPath" -ForegroundColor Green
} catch {
    Write-Host "❌ Download failed: $_" -ForegroundColor Red
    Write-Host ""
    Write-Host "Manual download URL:" -ForegroundColor Yellow
    Write-Host $downloadUrl
    exit 1
}

Write-Host ""
Write-Host "Installing Windows Exporter..." -ForegroundColor Yellow
Write-Host "This will:"
Write-Host "  - Install as a Windows service"
Write-Host "  - Listen on port 9182"
Write-Host "  - Start automatically on boot"
Write-Host ""

# Install silently
try {
    Start-Process msiexec.exe -ArgumentList "/i `"$installerPath`" /quiet /norestart" -Wait -NoNewWindow
    Write-Host "✅ Installation complete!" -ForegroundColor Green
} catch {
    Write-Host "❌ Installation failed: $_" -ForegroundColor Red
    exit 1
}

Write-Host ""
Write-Host "Waiting for service to start..." -ForegroundColor Yellow
Start-Sleep -Seconds 5

# Verify service is running
$service = Get-Service -Name "windows_exporter" -ErrorAction SilentlyContinue
if ($service -and $service.Status -eq "Running") {
    Write-Host "✅ windows_exporter service is running" -ForegroundColor Green
} else {
    Write-Host "⚠️ Service not running, attempting to start..." -ForegroundColor Yellow
    Start-Service -Name "windows_exporter"
    Start-Sleep -Seconds 3
    $service = Get-Service -Name "windows_exporter"
    if ($service.Status -eq "Running") {
        Write-Host "✅ Service started successfully" -ForegroundColor Green
    } else {
        Write-Host "❌ Failed to start service" -ForegroundColor Red
    }
}

Write-Host ""
Write-Host "Testing metrics endpoint..." -ForegroundColor Yellow
try {
    $response = Invoke-WebRequest -Uri "http://localhost:9182/metrics" -UseBasicParsing -TimeoutSec 5
    if ($response.StatusCode -eq 200) {
        Write-Host "✅ Metrics endpoint responding!" -ForegroundColor Green
        Write-Host ""
        Write-Host "Metrics available at: http://localhost:9182/metrics" -ForegroundColor Cyan
        Write-Host "Prometheus will auto-scrape: http://10.0.10.10:9182/metrics" -ForegroundColor Cyan

        # Show sample metrics
        $content = $response.Content
        $lines = $content -split "`n" | Where-Object { $_ -match "^windows_" } | Select-Object -First 5
        Write-Host ""
        Write-Host "Sample metrics:" -ForegroundColor Yellow
        $lines | ForEach-Object { Write-Host "  $_" }
    }
} catch {
    Write-Host "❌ Metrics endpoint not responding: $_" -ForegroundColor Red
    Write-Host ""
    Write-Host "Try manually: http://localhost:9182/metrics" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "=== Installation Summary ===" -ForegroundColor Cyan
Write-Host "✅ Windows Exporter installed as service" -ForegroundColor Green
Write-Host "✅ Listening on port 9182" -ForegroundColor Green
Write-Host "✅ Auto-start enabled" -ForegroundColor Green
Write-Host ""
Write-Host "Prometheus will automatically scrape this host!" -ForegroundColor Green
Write-Host "Check: http://10.0.10.25:9090/targets" -ForegroundColor Cyan
Write-Host ""

# Cleanup
Remove-Item $installerPath -Force -ErrorAction SilentlyContinue

Write-Host "Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
