# ============================================================================
# Troostwijk Auction Monitor - Windows Task Scheduler Setup
# ============================================================================
#
# This PowerShell script creates scheduled tasks in Windows Task Scheduler
# to run the auction monitor automatically.
#
# Usage:
#   Run PowerShell as Administrator, then:
#   .\setup-windows-task.ps1
#
# ============================================================================

Write-Host "=== Auctiora Monitor - Task Scheduler Setup ===" -ForegroundColor Cyan
Write-Host ""

# Configuration
$scriptPath = $PSScriptRoot
$jarPath = Join-Path $scriptPath "target\auctiora-1.0-SNAPSHOT-jar-with-dependencies.jar"
$javaExe = "java"

# Check if JAR exists
if (-not (Test-Path $jarPath)) {
    Write-Host "ERROR: JAR file not found at: $jarPath" -ForegroundColor Red
    Write-Host "Please run 'mvn clean package' first." -ForegroundColor Yellow
    exit 1
}

Write-Host "Creating scheduled tasks..." -ForegroundColor Green
Write-Host ""

# Task 1: Complete Workflow - Every 30 minutes
$task1Name = "TroostwijkMonitor-Workflow"
$task1Description = "Runs complete auction monitoring workflow every 30 minutes"
$task1Action = New-ScheduledTaskAction -Execute $javaExe -Argument "-jar `"$jarPath`" once" -WorkingDirectory $scriptPath
$task1Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 30) -RepetitionDuration ([TimeSpan]::MaxValue)
$task1Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable

try {
    Register-ScheduledTask -TaskName $task1Name -Action $task1Action -Trigger $task1Trigger -Settings $task1Settings -Description $task1Description -Force
    Write-Host "[✓] Created task: $task1Name (every 30 min)" -ForegroundColor Green
} catch {
    Write-Host "[✗] Failed to create task: $task1Name" -ForegroundColor Red
    Write-Host "    Error: $_" -ForegroundColor Yellow
}

# Task 2: Status Check - Every 6 hours
$task2Name = "TroostwijkMonitor-StatusCheck"
$task2Description = "Checks auction monitoring status every 6 hours"
$task2Action = New-ScheduledTaskAction -Execute $javaExe -Argument "-jar `"$jarPath`" status" -WorkingDirectory $scriptPath
$task2Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Hours 6) -RepetitionDuration ([TimeSpan]::MaxValue)
$task2Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable

try {
    Register-ScheduledTask -TaskName $task2Name -Action $task2Action -Trigger $task2Trigger -Settings $task2Settings -Description $task2Description -Force
    Write-Host "[✓] Created task: $task2Name (every 6 hours)" -ForegroundColor Green
} catch {
    Write-Host "[✗] Failed to create task: $task2Name" -ForegroundColor Red
    Write-Host "    Error: $_" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "=== Setup Complete ===" -ForegroundColor Cyan
Write-Host ""
Write-Host "Created tasks:" -ForegroundColor White
Write-Host "  1. $task1Name - Runs every 30 minutes" -ForegroundColor Gray
Write-Host "  2. $task2Name - Runs every 6 hours" -ForegroundColor Gray
Write-Host ""
Write-Host "To view tasks: Open Task Scheduler and look for 'TroostwijkMonitor-*'" -ForegroundColor Yellow
Write-Host "To remove tasks: Run 'Unregister-ScheduledTask -TaskName TroostwijkMonitor-*'" -ForegroundColor Yellow
Write-Host ""
