# ColaFlow 重新登录脚本
# 获取新的 JWT Token

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

Write-Host "🔑 正在登录测试账号..." -ForegroundColor Cyan

# 登录请求
$loginPayload = @{
    tenantSlug = "testcompany"
    email = "admin@test.com"
    password = "Admin@123456"
} | ConvertTo-Json

try {
    $response = Invoke-RestMethod -Uri "$apiUrl/api/auth/login" -Method Post -Body $loginPayload -ContentType "application/json"

    Write-Host "✅ 登录成功！" -ForegroundColor Green
    Write-Host ""
    Write-Host "📋 用户信息：" -ForegroundColor Yellow
    Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Yellow
    Write-Host "用户 ID:     $($response.user.id)" -ForegroundColor White
    Write-Host "邮箱:        $($response.user.email)" -ForegroundColor White
    Write-Host "姓名:        $($response.user.fullName)" -ForegroundColor White
    Write-Host "租户 ID:     $($response.user.tenantId)" -ForegroundColor White
    Write-Host "租户名称:    $($response.user.tenantName)" -ForegroundColor White
    Write-Host "角色:        $($response.user.role)" -ForegroundColor White
    Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "🔑 JWT Token (前50字符):" -ForegroundColor Cyan
    Write-Host $response.accessToken.Substring(0, [Math]::Min(50, $response.accessToken.Length))... -ForegroundColor Gray
    Write-Host ""
    Write-Host "💡 提示: 请在前端浏览器重新登录以获取有效的 Token" -ForegroundColor Yellow

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

    Write-Host "❌ 登录失败" -ForegroundColor Red
    Write-Host "状态码: $statusCode" -ForegroundColor Red
    Write-Host "错误详情: $errorBody" -ForegroundColor Red
}
