# PyPI 发布脚本 (PowerShell)

$ErrorActionPreference = "Stop"

Write-Host "🚀 开始发布 mcp-docx-thesis-server 到 PyPI" -ForegroundColor Cyan
Write-Host ""

# 检查是否在项目根目录
if (-not (Test-Path "pyproject.toml")) {
    Write-Host "❌ 错误：请在项目根目录运行此脚本" -ForegroundColor Red
    exit 1
}

# 检查是否安装了必要的工具
Write-Host "📦 检查依赖..." -ForegroundColor Yellow
pip install --quiet build twine hatchling 2>$null

# 清理旧的构建文件
Write-Host "🧹 清理旧的构建文件..." -ForegroundColor Yellow
Remove-Item -Recurse -Force dist/, build/, *.egg-info/ -ErrorAction SilentlyContinue

# 运行测试
Write-Host "🧪 运行测试..." -ForegroundColor Yellow
python -m pytest tests/ -v --tb=short
if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ 测试失败，请修复后再发布" -ForegroundColor Red
    exit 1
}

# 运行代码检查
Write-Host "🔍 运行代码检查..." -ForegroundColor Yellow

Write-Host "  - 运行 black..." -ForegroundColor Gray
$blackResult = python -m black src/ tests/ --check --quiet 2>&1
if ($LASTEXITCODE -ne 0) {
    Write-Host "    ⚠️  代码格式需要调整，运行: black src/ tests/" -ForegroundColor Yellow
}

Write-Host "  - 运行 ruff..." -ForegroundColor Gray
$ruffResult = python -m ruff check src/ tests/ --quiet 2>&1
if ($LASTEXITCODE -ne 0) {
    Write-Host "    ⚠️  有 lint 警告" -ForegroundColor Yellow
}

Write-Host "  - 运行 mypy..." -ForegroundColor Gray
$mypyResult = python -m mypy src/ --quiet 2>&1
if ($LASTEXITCODE -ne 0) {
    Write-Host "    ⚠️  有类型检查警告" -ForegroundColor Yellow
}

# 构建包
Write-Host "📦 构建包..." -ForegroundColor Yellow
python -m build

# 检查构建结果
Write-Host "🔍 检查构建结果..." -ForegroundColor Yellow
$wheelFiles = Get-ChildItem dist/*.whl -ErrorAction SilentlyContinue
$sdistFiles = Get-ChildItem dist/*.tar.gz -ErrorAction SilentlyContinue

if (-not $wheelFiles) {
    Write-Host "❌ 构建失败，未找到 wheel 文件" -ForegroundColor Red
    exit 1
}

if (-not $sdistFiles) {
    Write-Host "❌ 构建失败，未找到 sdist 文件" -ForegroundColor Red
    exit 1
}

Write-Host "✅ 构建成功！" -ForegroundColor Green
Write-Host ""
Get-ChildItem dist/ | ForEach-Object { Write-Host "   $($_.Name) - $([math]::Round($_.Length/1KB, 2)) KB" }
Write-Host ""

# 上传到 PyPI
Write-Host "📤 上传到 PyPI..." -ForegroundColor Yellow
Write-Host ""
Write-Host "提示：" -ForegroundColor Cyan
Write-Host "  - 首次发布需要先注册 PyPI 账号: https://pypi.org/account/register/" -ForegroundColor Gray
Write-Host "  - 需要配置 PyPI API Token: https://pypi.org/manage/account/token/" -ForegroundColor Gray
Write-Host "  - 可以配置 %USERPROFILE%\.pypirc 文件或使用环境变量 TWINE_USERNAME/__token__ 和 TWINE_PASSWORD" -ForegroundColor Gray
Write-Host ""

$confirm = Read-Host "确认要上传到 PyPI 吗？首次上传前建议先上传到 TestPyPI 测试 (y/n/test)"

if ($confirm -eq "test") {
    Write-Host "📤 上传到 TestPyPI..." -ForegroundColor Yellow
    python -m twine upload --repository testpypi dist/*
    Write-Host ""
    Write-Host "✅ 已上传到 TestPyPI" -ForegroundColor Green
    Write-Host "   测试安装: pip install --index-url https://test.pypi.org/simple/ mcp-docx-thesis-server" -ForegroundColor Gray
}
elseif ($confirm -eq "y" -or $confirm -eq "Y") {
    Write-Host "📤 上传到 PyPI..." -ForegroundColor Yellow
    python -m twine upload dist/*
    Write-Host ""
    Write-Host "✅ 发布成功！" -ForegroundColor Green
    Write-Host "   安装命令: pip install mcp-docx-thesis-server" -ForegroundColor Cyan
    Write-Host ""
    Write-Host "📝 更新 Cherry Studio 配置:" -ForegroundColor Yellow
    Write-Host '   {' -ForegroundColor White
    Write-Host '     "mcpServers": {' -ForegroundColor White
    Write-Host '       "docx-thesis": {' -ForegroundColor White
    Write-Host '         "command": "python",' -ForegroundColor White
    Write-Host '         "args": ["-m", "mcp_docx_thesis_server.server"]' -ForegroundColor White
    Write-Host '       }' -ForegroundColor White
    Write-Host '     }' -ForegroundColor White
    Write-Host '   }' -ForegroundColor White
}
else {
    Write-Host "❌ 取消上传" -ForegroundColor Red
    Write-Host ""
    Write-Host "💡 你可以手动上传:" -ForegroundColor Yellow
    Write-Host "   python -m twine upload dist/*" -ForegroundColor Gray
}

Write-Host ""
Write-Host "🎉 完成！" -ForegroundColor Green
