<#
.SYNOPSIS
  AgentsMeeting â€?å›žæ»šåˆ°ä¸Šä¸€ä¸ªç¨³å®šç‰ˆæœ?.DESCRIPTION
  ä¿å­˜å½“å‰ç‰ˆæœ¬åŽï¼Œä»?Git æ‹‰å–ä¸Šä¸€ä¸?tag æˆ?commitã€?#>

param([string]$Target = "")

$root = "D:\F\NewI\opencode\daily-workspace"
$gateway = "$root\projects\AgentsMeeting\\gateway"

Write-Host "=== AgentsMeeting Rollback ===" -ForegroundColor Cyan

# Stop services
Write-Host "[1/3] Stopping services..." -ForegroundColor Yellow
& "$root\projects\AgentsMeeting\deploy\windows\stop.ps1"
Start-Sleep 2

# Backup current
$backupDir = "$gateway\backups\$(Get-Date -Format 'yyyyMMdd_HHmmss')"
New-Item -ItemType Directory -Path $backupDir -Force | Out-Null
Copy-Item "$gateway\scripts\*.py" $backupDir
Write-Host "[2/3] Backup saved to $backupDir" -ForegroundColor Green

# Restore target
if ($Target) {
    # Could restore from specific backup
    $restoreDir = "$gateway\backups\$Target"
    if (Test-Path $restoreDir) {
        Copy-Item "$restoreDir\*" "$gateway\scripts\" -Force
        Write-Host "[3/3] Restored from $Target" -ForegroundColor Green
    }
    else { Write-Host "[3/3] Target not found: $Target" -ForegroundColor Red; exit 1 }
}
else { Write-Host "[3/3] No rollback target specified. Skipped." -ForegroundColor Yellow }

Write-Host "`nRun start.ps1 to restart services" -ForegroundColor Cyan
