param(
  [string]$ProjectRoot = "C:\xampp\htdocs\ddb-new",
  [string]$PhpExe = "C:\xampp\php\php.exe",
  [string]$Hospital = "all",
  [int]$IntervalMinutes = 15,
  [string]$TaskName = "HMH Executive Summary Refresh"
)

$ErrorActionPreference = "Stop"
$runner = Join-Path $ProjectRoot "tools\run_executive_summary_refresh.ps1"
if (!(Test-Path $runner)) {
  throw "Runner not found: $runner. Copy the tools folder into the project first."
}

$actionArgs = "-NoProfile -ExecutionPolicy Bypass -File `"$runner`" -ProjectRoot `"$ProjectRoot`" -PhpExe `"$PhpExe`" -Hospital `"$Hospital`""
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $actionArgs
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) -RepetitionInterval (New-TimeSpan -Minutes $IntervalMinutes) -RepetitionDuration (New-TimeSpan -Days 3650)
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -MultipleInstances IgnoreNew

Register-ScheduledTask -TaskName $TaskName -Action $action -Trigger $trigger -Settings $settings -RunLevel Highest -Force | Out-Null
Write-Host "Scheduled task installed:" $TaskName
Write-Host "Interval minutes:" $IntervalMinutes
Write-Host "Hospital:" $Hospital
Write-Host "Test now with: Start-ScheduledTask -TaskName `"$TaskName`""
