param(
    [string]$PythonPath = "C:\Users\13636\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe",
    [string]$TaskPrefix = "FeishuDailyReport",
    [string]$ReminderTime = "18:00",
    [string]$SummaryTime = "19:00"
)

$ErrorActionPreference = "Stop"
$ProjectRoot = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path

if (-not (Test-Path $PythonPath)) {
    throw "Python not found: $PythonPath"
}

$reminderAction = New-ScheduledTaskAction `
    -Execute $PythonPath `
    -Argument "-m daily_report.scheduled reminder" `
    -WorkingDirectory $ProjectRoot

$summaryAction = New-ScheduledTaskAction `
    -Execute $PythonPath `
    -Argument "-m daily_report.scheduled summary" `
    -WorkingDirectory $ProjectRoot

$reminderTrigger = New-ScheduledTaskTrigger -Daily -At $ReminderTime
$summaryTrigger = New-ScheduledTaskTrigger -Daily -At $SummaryTime
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries

Register-ScheduledTask `
    -TaskName "$TaskPrefix-Reminder" `
    -Action $reminderAction `
    -Trigger $reminderTrigger `
    -Settings $settings `
    -Description "Runs daily at $ReminderTime; script sends only on configured workdays" `
    -Force | Out-Null

Register-ScheduledTask `
    -TaskName "$TaskPrefix-Summary" `
    -Action $summaryAction `
    -Trigger $summaryTrigger `
    -Settings $settings `
    -Description "Runs daily at $SummaryTime; script sends only on configured workdays" `
    -Force | Out-Null

Write-Host "Windows scheduled tasks installed:"
Write-Host " - $TaskPrefix-Reminder: runs daily at $ReminderTime and skips non-workdays"
Write-Host " - $TaskPrefix-Summary: runs daily at $SummaryTime and skips non-workdays"
Write-Host "Project root: $ProjectRoot"
