# ColaFlow 测试用户注册脚本
# 创建一个测试租户和管理员账号

$apiUrl = "http://localhost:5167"

Write-Host "🚀 正在注册测试租户和用户..." -ForegroundColor Cyan

# 注册租户
$registerPayload = @{
    companyName = "测试公司"
    slug = "testcompany"
    ownerEmail = "admin@test.com"
    ownerPassword = "Admin@123456"
    ownerFullName = "测试管理员"
} | ConvertTo-Json

try {
    $response = Invoke-RestMethod -Uri "$apiUrl/api/tenants/register" -Method Post -Body $registerPayload -ContentType "application/json"

    Write-Host "✅ 注册成功！" -ForegroundColor Green
    Write-Host ""
    Write-Host "📋 测试账号信息：" -ForegroundColor Yellow
    Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Yellow
    Write-Host "租户 Slug:  testcompany" -ForegroundColor White
    Write-Host "邮箱:       admin@test.com" -ForegroundColor White
    Write-Host "密码:       Admin@123456" -ForegroundColor White
    Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "🌐 访问地址：" -ForegroundColor Cyan
    Write-Host "前端: http://localhost:3000" -ForegroundColor White
    Write-Host "API:  http://localhost:5167" -ForegroundColor White
    Write-Host ""
    Write-Host "返回数据：" -ForegroundColor Gray
    $response | ConvertTo-Json -Depth 5

} catch {
    $statusCode = $_.Exception.Response.StatusCode.value__
    $errorBody = $_.ErrorDetails.Message

    if ($statusCode -eq 400) {
        Write-Host "⚠️  租户可能已存在，尝试登录..." -ForegroundColor Yellow
        Write-Host ""
        Write-Host "📋 使用以下账号登录：" -ForegroundColor Yellow
        Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Yellow
        Write-Host "租户 Slug:  testcompany" -ForegroundColor White
        Write-Host "邮箱:       admin@test.com" -ForegroundColor White
        Write-Host "密码:       Admin@123456" -ForegroundColor White
        Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Yellow
    } else {
        Write-Host "❌ 注册失败：$errorBody" -ForegroundColor Red
    }
}
