# Registers the Claude issue watcher as a Windows Scheduled Task.
# Runs every 10 minutes while the machine is on; skips if a run is in progress.
# Run once:  powershell -ExecutionPolicy Bypass -File register-watcher-task.ps1

$ErrorActionPreference = 'Stop'

$taskName = 'PelagiaClaudeIssueWatcher'
$scriptPath = Join-Path $PSScriptRoot 'claude-issue-watcher.ps1'

$action = New-ScheduledTaskAction -Execute 'powershell.exe' `
    -Argument "-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$scriptPath`""

$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) `
    -RepetitionInterval (New-TimeSpan -Minutes 10)

$settings = New-ScheduledTaskSettingsSet `
    -MultipleInstances IgnoreNew `
    -ExecutionTimeLimit (New-TimeSpan -Hours 2) `
    -StartWhenAvailable

Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger `
    -Settings $settings -Description 'Polls Forgejo for claude-queue issues and runs Claude Code on them' -Force

Write-Host "Scheduled task '$taskName' registered (every 10 minutes)."
