$base = "http://localhost:7070/admin"
$headers = @{
    "adminAccessToken" = ""
    "Content-Type" = "application/json"
}

Write-Host "Logging in first..."
$loginBody = Get-Content -Raw "d:\快与慢充电桩\code\haha\docs\login.json"
$login = Invoke-RestMethod -Uri "$base/login" -Method POST -ContentType "application/json" -Body $loginBody
Write-Host "login code: $($login.code), message: $($login.message)"
$token = $login.data.token
$headers["adminAccessToken"] = $token
Write-Host "Got token: $token"
Write-Host ""

Write-Host "========================================"
Write-Host "1. Test GET /menu/routes (user menu tree)"
Write-Host "========================================"
$r = Invoke-RestMethod -Uri "$base/menu/routes" -Method GET -Headers $headers
Write-Host "code: $($r.code), message: $($r.message)"
Write-Host "data count: $($r.data.Count)"
if ($r.data.Count -gt 0) {
    Write-Host "first item path: $($r.data[0].path)"
    Write-Host "first item meta title: $($r.data[0].meta.title)"
}
Write-Host ""

Write-Host "========================================"
Write-Host "2. Test POST /menu (all menus)"
Write-Host "========================================"
$body = '{}'
$r = Invoke-RestMethod -Uri "$base/menu" -Method POST -Headers $headers -Body $body
Write-Host "code: $($r.code), message: $($r.message)"
Write-Host "total menus: $($r.data.Count)"
if ($r.data.Count -gt 0) {
    Write-Host "first menu: id=$($r.data[0].id), title=$($r.data[0].title)"
}
Write-Host ""

Write-Host "========================================"
Write-Host "3. Test POST /menu (search by title)"
Write-Host "========================================"
$body = '{"title":"仪表"}'
$r = Invoke-RestMethod -Uri "$base/menu" -Method POST -Headers $headers -Body $body
Write-Host "code: $($r.code), message: $($r.message)"
Write-Host "found menus: $($r.data.Count)"
if ($r.data.Count -gt 0) {
    Write-Host "first: id=$($r.data[0].id), title=$($r.data[0].title)"
}
Write-Host ""

Write-Host "========================================"
Write-Host "4. Test POST /menu/role-menu (menus for role assignment)"
Write-Host "========================================"
$body = '{}'
$r = Invoke-RestMethod -Uri "$base/menu/role-menu" -Method POST -Headers $headers -Body $body
Write-Host "code: $($r.code), message: $($r.message)"
Write-Host "total menus: $($r.data.Count)"
Write-Host ""

Write-Host "========================================"
Write-Host "5. Test POST /menu/role-menu-ids (menu IDs for role 1)"
Write-Host "========================================"
$body = '{"id":1}'
$r = Invoke-RestMethod -Uri "$base/menu/role-menu-ids" -Method POST -Headers $headers -Body $body
Write-Host "code: $($r.code), message: $($r.message)"
Write-Host "menu IDs for role 1: $($r.data)"
Write-Host "count: $($r.data.Count)"
Write-Host ""

Write-Host "========================================"
Write-Host "6. Test POST /menu/create (create new menu)"
Write-Host "========================================"
$body = @'
{
    "parentId": 3001,
    "menuType": 0,
    "title": "测试菜单",
    "name": "TestMenu",
    "path": "/system/test",
    "component": "",
    "rank": 99,
    "icon": "ri:test-tube-line",
    "showLink": true,
    "showParent": false,
    "keepAlive": false
}
'@
$r = Invoke-RestMethod -Uri "$base/menu/create" -Method POST -Headers $headers -Body $body
Write-Host "code: $($r.code), message: $($r.message)"
if ($r.data) {
    Write-Host "created id: $($r.data.id), title: $($r.data.title)"
    $newId = $r.data.id
}

Write-Host ""
Write-Host "========================================"
Write-Host "7. Test PUT /menu/{id} (update menu)"
Write-Host "========================================"
$body = @'
{
    "parentId": 3001,
    "menuType": 0,
    "title": "测试菜单-已更新",
    "name": "TestMenuUpdated",
    "path": "/system/test-updated",
    "component": "",
    "rank": 88,
    "icon": "ri:test-tube-fill",
    "showLink": true,
    "showParent": false,
    "keepAlive": false
}
'@
$r = Invoke-RestMethod -Uri "$base/menu/$newId" -Method PUT -Headers $headers -Body $body
Write-Host "code: $($r.code), message: $($r.message)"
if ($r.data) {
    Write-Host "updated title: $($r.data.title)"
}
Write-Host ""

Write-Host "========================================"
Write-Host "8. Test DELETE /menu/{id} (delete menu)"
Write-Host "========================================"
$r = Invoke-RestMethod -Uri "$base/menu/$newId" -Method DELETE -Headers $headers
Write-Host "code: $($r.code), message: $($r.message)"
Write-Host ""

Write-Host "========================================"
Write-Host "9. Test POST /menu/save-role-menus (save role menu)"
Write-Host "========================================"
$body = @"
{"roleId": 1, "menuIds": [1001, 2001, 3001, 4001, 5001]}
"@
$r = Invoke-RestMethod -Uri "$base/menu/save-role-menus" -Method POST -Headers $headers -Body $body
Write-Host "code: $($r.code), message: $($r.message)"
Write-Host ""

Write-Host "=== ALL TESTS COMPLETE ==="
