# =============================================================================
# 企微IT智能服务台 — 本地开发环境 停止脚本
# =============================================================================
# 作用:停止 docker-compose.dev.yml 启动的所有容器(数据保留)
# 用法:.\scripts\dev-stop.ps1
# 数据会保留在 postgres_dev_data / redis_dev_data 卷里
# 如需完全清空,加 -v 参数:.\scripts\dev-stop.ps1 -RemoveVolumes
# =============================================================================

param(
    [switch]$RemoveVolumes   # 加这个参数会删除数据卷(慎用!)
)

$ErrorActionPreference = 'Stop'

$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$ProjectRoot = Split-Path -Parent $ScriptDir
Set-Location $ProjectRoot

Write-Host ""
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Yellow
Write-Host "  停止本地开发环境" -ForegroundColor Yellow
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Yellow

if ($RemoveVolumes) {
    Write-Host ""
    Write-Host "⚠️  -v 参数已指定,将删除所有数据卷!" -ForegroundColor Red
    Write-Host "    (postgres_dev_data / redis_dev_data 会被清空)" -ForegroundColor Red
    Write-Host ""
    $Confirm = Read-Host "确认删除?输入 yes 继续,其他键取消"
    if ($Confirm -ne "yes") {
        Write-Host "已取消" -ForegroundColor Gray
        exit 0
    }
    docker compose -f docker-compose.dev.yml down -v
} else {
    docker compose -f docker-compose.dev.yml down
}

if ($LASTEXITCODE -eq 0) {
    Write-Host "✅ 容器已停止" -ForegroundColor Green
    Write-Host ""
    Write-Host "📌 数据保留在卷里,下次 .\scripts\dev-start.ps1 自动恢复" -ForegroundColor Cyan
    Write-Host "📌 完全清理:.\scripts\dev-stop.ps1 -RemoveVolumes" -ForegroundColor Cyan
} else {
    Write-Host "❌ 停止失败" -ForegroundColor Red
    exit 1
}
