# WorkRoot Backup Automation - Windows Task Scheduler Setup
#
# Run this PowerShell script as Administrator to set up automated backups on Windows
#
# Usage: .\windows-tasks.ps1

# Get the project root directory
$ProjectRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
$LogPath = Join-Path $ProjectRoot "backups"

Write-Host "Setting up Windows Task Scheduler for backups..." -ForegroundColor Cyan
Write-Host "Project root: $ProjectRoot" -ForegroundColor Yellow

# Ensure log directory exists
if (-not (Test-Path $LogPath)) {
    New-Item -ItemType Directory -Path $LogPath -Force | Out-Null
}

# Daily Full Backup at 2:00 AM
Write-Host "`nCreating daily backup task..." -ForegroundColor Green
$DailyAction = New-ScheduledTaskAction -Execute "npm" -Argument "run backup:full" -WorkingDirectory $ProjectRoot
$DailyTrigger = New-ScheduledTaskTrigger -Daily -At 2am
$DailySettings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
Register-ScheduledTask -TaskName "WorkRoot-DailyBackup" -Action $DailyAction -Trigger $DailyTrigger -Settings $DailySettings -Description "WorkRoot daily full backup" -Force | Out-Null
Write-Host "✓ Daily backup scheduled for 2:00 AM" -ForegroundColor Green

# Incremental Backup Every 6 Hours
Write-Host "`nCreating incremental backup task..." -ForegroundColor Green
$IncrementalAction = New-ScheduledTaskAction -Execute "npm" -Argument "run backup:content" -WorkingDirectory $ProjectRoot
$IncrementalTrigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Hours 6) -RepetitionDuration ([TimeSpan]::MaxValue)
Register-ScheduledTask -TaskName "WorkRoot-IncrementalBackup" -Action $IncrementalAction -Trigger $IncrementalTrigger -Settings $DailySettings -Description "WorkRoot incremental content backup" -Force | Out-Null
Write-Host "✓ Incremental backup scheduled every 6 hours" -ForegroundColor Green

# Weekly Cleanup on Sunday at 3:00 AM
Write-Host "`nCreating cleanup task..." -ForegroundColor Green
$CleanupAction = New-ScheduledTaskAction -Execute "npm" -Argument "run backup:cleanup" -WorkingDirectory $ProjectRoot
$CleanupTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 3am
Register-ScheduledTask -TaskName "WorkRoot-BackupCleanup" -Action $CleanupAction -Trigger $CleanupTrigger -Settings $DailySettings -Description "WorkRoot backup cleanup" -Force | Out-Null
Write-Host "✓ Cleanup scheduled for Sunday 3:00 AM" -ForegroundColor Green

# Daily Verification at 9:00 AM
Write-Host "`nCreating verification task..." -ForegroundColor Green
$VerifyAction = New-ScheduledTaskAction -Execute "npm" -Argument "run backup:verify" -WorkingDirectory $ProjectRoot
$VerifyTrigger = New-ScheduledTaskTrigger -Daily -At 9am
Register-ScheduledTask -TaskName "WorkRoot-BackupVerify" -Action $VerifyAction -Trigger $VerifyTrigger -Settings $DailySettings -Description "WorkRoot backup verification" -Force | Out-Null
Write-Host "✓ Verification scheduled for 9:00 AM daily" -ForegroundColor Green

Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host "Backup automation setup complete!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan

Write-Host "`nScheduled Tasks Created:" -ForegroundColor Yellow
Write-Host "  • Daily Full Backup - Every day at 2:00 AM"
Write-Host "  • Incremental Backup - Every 6 hours"
Write-Host "  • Weekly Cleanup - Sunday at 3:00 AM"
Write-Host "  • Daily Verification - Every day at 9:00 AM"

Write-Host "`nTo view scheduled tasks:" -ForegroundColor Yellow
Write-Host "  Get-ScheduledTask | Where-Object {`$_.TaskName -like 'WorkRoot-*'}"

Write-Host "`nTo remove all tasks:" -ForegroundColor Yellow
Write-Host "  Get-ScheduledTask | Where-Object {`$_.TaskName -like 'WorkRoot-*'} | Unregister-ScheduledTask -Confirm:`$false"

Write-Host "`nTo run a backup now (test):" -ForegroundColor Yellow
Write-Host "  npm run backup:full"

Write-Host "`n" -ForegroundColor Green
