# =============================================================================
# 企微智能IT支持服务台 — 打包 + 构建后端镜像 + 部署脚本
# =============================================================================
# 功能：
#   1. 打包前端构建产物 + nginx配置 + docker-compose.yml + .env
#   2. 构建后端 Docker 镜像（修复 AIHandler 后）
#   3. 导出镜像为 tar 文件
#   4. 一键部署到服务器（可选）
# 用法：
#   .\build-and-deploy.ps1 -Mode local    # 仅本地打包
#   .\build-and-deploy.ps1 -Mode deploy   # 打包 + 部署到服务器
# =============================================================================

param(
    [Parameter(Mandatory=$false)]
    [ValidateSet("local", "deploy")]
    [string]$Mode = "local",

    [Parameter(Mandatory=$false)]
    [string]$ServerHost = "10.90.5.110"
)

$ErrorActionPreference = "Stop"

$projectRoot = "D:\资料\03-项目开发\wecom_it_smart_desk"
$deployDir = "$projectRoot\deploy-server"
$packageDir = "$deployDir\_package"
$zipFile = "$deployDir\it-smart-desk-server-deploy.zip"
$backendTar = "$deployDir\deploy-backend.tar"

# 彩色输出函数
function Write-Step {
    param([string]$Text)
    Write-Host "[STEP] $Text" -ForegroundColor Cyan
}

function Write-Success {
    param([string]$Text)
    Write-Host "[OK] $Text" -ForegroundColor Green
}

function Write-Warn {
    param([string]$Text)
    Write-Host "[WARN] $Text" -ForegroundColor Yellow
}

function Write-Error {
    param([string]$Text)
    Write-Host "[ERROR] $Text" -ForegroundColor Red
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " 企微智能IT支持服务台 — 打包部署自动化" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " 模式：$Mode" -ForegroundColor White
Write-Host ""

# =============================================================================
# 步骤 1: 清理旧打包目录
# =============================================================================
Write-Step "清理旧打包目录..."
if (Test-Path $packageDir) {
    Remove-Item $packageDir -Recurse -Force
}
New-Item -ItemType Directory -Path $packageDir -Force | Out-Null
New-Item -ItemType Directory -Path "$packageDir\nginx" -Force | Out-Null
Write-Success "清理完成"

# =============================================================================
# 步骤 2: 复制 docker-compose.yml
# =============================================================================
Write-Step "复制 docker-compose.yml..."
Copy-Item "$deployDir\docker-compose.yml" "$packageDir\docker-compose.yml"
Write-Success "已复制"

# =============================================================================
# 步骤 3: 复制 .env（含真实配置）
# =============================================================================
Write-Step "复制 .env..."
if (Test-Path "$deployDir\.env") {
    Copy-Item "$deployDir\.env" "$packageDir\.env"
    Write-Success "已复制"
} else {
    Write-Warn ".env 文件不存在，跳过"
}

# =============================================================================
# 步骤 4: 复制 nginx 配置
# =============================================================================
Write-Step "复制 nginx 配置..."
Copy-Item "$deployDir\nginx.conf" "$packageDir\nginx\nginx.conf"
Write-Success "已复制"

# =============================================================================
# 步骤 5: 复制后端代码（排除 __pycache__、.pyc）
# =============================================================================
Write-Step "复制后端代码..."
$backendSrc = "$projectRoot\backend"
$backendDst = "$packageDir\backend"
New-Item -ItemType Directory -Path $backendDst -Force | Out-Null

$backendFiles = @("Dockerfile", "requirements.txt", "alembic.ini", "alembic", "app")
foreach ($item in $backendFiles) {
    $src = "$backendSrc\$item"
    if (Test-Path $src) {
        if ((Get-Item $src).PSIsContainer) {
            robocopy $src "$backendDst\$item" /E /XD __pycache__ /XF *.pyc *.db /NFL /NDL /NJH /NJS /NC /NS /NP | Out-Null
        } else {
            Copy-Item $src "$backendDst\$item"
        }
    }
}
$backendFileCount = (Get-ChildItem $backendDst -Recurse -File).Count
Write-Success "已复制 $backendFileCount 个文件"

# =============================================================================
# 步骤 6: 复制前端构建产物
# =============================================================================
Write-Step "复制前端构建产物..."
@("frontend-h5", "frontend-agent", "frontend-admin", "frontend-portal") | ForEach-Object {
    $src = "$projectRoot\$_\dist"
    $dst = "$packageDir\$_\dist"
    if (Test-Path $src) {
        New-Item -ItemType Directory -Path $dst -Force | Out-Null
        robocopy $src $dst /E /NFL /NDL /NJH /NJS /NC /NS /NP | Out-Null
        $count = (Get-ChildItem $dst -Recurse -File).Count
        Write-Host "  $_ : $count files" -ForegroundColor Gray
    } else {
        Write-Host "  $_ : dist not found, skipping" -ForegroundColor Yellow
    }
}
Write-Success "前端构建产物复制完成"

# =============================================================================
# 步骤 7: 打包成 zip
# =============================================================================
Write-Step "打包成 zip..."
if (Test-Path $zipFile) {
    Remove-Item $zipFile -Force
}
Compress-Archive -Path "$packageDir\*" -DestinationPath $zipFile -CompressionLevel Optimal
$zipSize = [math]::Round((Get-Item $zipFile).Length / 1MB, 2)
Write-Success "zip 打包完成: $zipSize MB"

# =============================================================================
# 步骤 8: 构建后端 Docker 镜像
# =============================================================================
Write-Step "构建后端 Docker 镜像..."
Write-Host "  镜像名: wecom-it-desk-backend:latest" -ForegroundColor Gray

# 先检查 Docker 是否运行
$docker ps > $null 2>&1
if ($LASTEXITCODE -ne 0) {
    Write-Error "Docker 未运行，请先启动 Docker Desktop"
    exit 1
}

# 构建镜像
$buildStart = Get-Date
docker build -t wecom-it-desk-backend:latest "$packageDir\backend" 2>&1 | ForEach-Object { Write-Host "  $_" -ForegroundColor Gray }
if ($LASTEXITCODE -ne 0) {
    Write-Error "Docker 镜像构建失败"
    exit 1
}
$buildTime = ((Get-Date) - $buildStart).TotalSeconds
Write-Success "镜像构建完成 (耗时: $([math]::Round($buildTime, 1))s)"

# =============================================================================
# 步骤 9: 导出镜像为 tar
# =============================================================================
Write-Step "导出镜像为 tar..."
if (Test-Path $backendTar) {
    Remove-Item $backendTar -Force
}
docker save -o $backendTar wecom-it-desk-backend:latest
$tarSize = [math]::Round((Get-Item $backendTar).Length / 1MB, 2)
Write-Success "tar 导出完成: $tarSize MB"

# =============================================================================
# 步骤 10: 清理临时目录
# =============================================================================
Write-Step "清理临时文件..."
Remove-Item $packageDir -Recurse -Force
Write-Success "清理完成"

# =============================================================================
# 输出结果汇总
# =============================================================================
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host " 本地打包完成！" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "生成文件：" -ForegroundColor White
Write-Host "  1. $zipFile (${zipSize} MB)" -ForegroundColor Cyan
Write-Host "  2. $backendTar (${tarSize} MB)" -ForegroundColor Cyan
Write-Host ""

# =============================================================================
# 步骤 11: 部署到服务器（如果指定了 -Mode deploy）
# =============================================================================
if ($Mode -eq "deploy") {
    Write-Host ""
    Write-Host "========================================" -ForegroundColor Cyan
    Write-Host " 开始部署到服务器 $ServerHost..." -ForegroundColor Cyan
    Write-Host "========================================" -ForegroundColor Cyan
    Write-Host ""

    # 步骤 11.1: 上传 zip 和 tar 到服务器
    Write-Step "上传部署文件到服务器..."
    # 使用 SCP 上传（需要配置 SSH 密钥或输入密码）
    scp -o StrictHostKeyChecking=no "$zipFile" "root@${ServerHost}:/tmp/it-smart-desk-server-deploy.zip"
    if ($LASTEXITCODE -ne 0) {
        Write-Warn "SCP 上传失败，请手动上传: $zipFile"
    } else {
        Write-Success "zip 已上传"
    }

    scp -o StrictHostKeyChecking=no "$backendTar" "root@${ServerHost}:/tmp/deploy-backend.tar"
    if ($LASTEXITCODE -ne 0) {
        Write-Warn "SCP 上传失败，请手动上传: $backendTar"
    } else {
        Write-Success "tar 已上传"
    }

    # 步骤 11.2: SSH 到服务器执行部署
    Write-Step "执行服务器部署..."

    $deployCommands = @'
# 进入部署目录
cd /opt/wecom-it-desk

# 停止服务
docker compose down

# 备份旧镜像
docker images | grep wecom-it-desk-backend | awk '{print $1":"$2}' | xargs -I {} docker tag {} wecom-it-desk-backend:backup-$(date +%Y%m%d%H%M%S) 2>/dev/null || true

# 导入新镜像
docker load -i /tmp/deploy-backend.tar

# 解压部署包
unzip -o /tmp/it-smart-desk-server-deploy.zip -d . 2>/dev/null || unzip -o /tmp/it-smart-desk-server-deploy.zip

# 启动服务（重新构建并启动）
docker compose up -d --build

# 等待服务启动
echo "等待服务启动..."
sleep 10

# 检查容器状态
docker compose ps

# 检查后端日志
docker logs --tail 20 wecom_it_backend
'@

    # 执行远程部署命令
    ssh -o StrictHostKeyChecking=no -o ConnectTimeout=30 "root@${ServerHost}" $deployCommands

    if ($LASTEXITCODE -eq 0) {
        Write-Success "部署完成！"
        Write-Host ""
        Write-Host "验证地址：" -ForegroundColor White
        Write-Host "  - H5: https://itsupport.servyou.com.cn/itdesk/" -ForegroundColor Cyan
        Write-Host "  - 坐席: https://itsupport.servyou.com.cn/itagent/" -ForegroundColor Cyan
        Write-Host "  - 管理: https://itsupport.servyou.com.cn/itadmin/" -ForegroundColor Cyan
    } else {
        Write-Error "部署命令执行失败，请检查服务器日志"
    }
} else {
    Write-Host "提示：使用 -Mode deploy 参数可一键部署到服务器" -ForegroundColor Yellow
}

Write-Host ""
