# Quick Issue Management Test
$TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2ODM4NTcwOC0yZjJiLTQzMTItYjdiOS1hOGFiMjI3NTliMDkiLCJlbWFpbCI6ImFkbWluQHF1aWNrdGVzdDk5OS5jb20iLCJqdGkiOiJjMmRjNDI2ZS0yODA5LTRiNWMtYTY2YS1kZWI3ZjU2YWNkMmIiLCJ1c2VyX2lkIjoiNjgzODU3MDgtMmYyYi00MzEyLWI3YjktYThhYjIyNzU5YjA5IiwidGVuYW50X2lkIjoiYjM4OGI4N2EtMDQ2YS00MTM0LWEyNmMtNWRjZGY3ZjkyMWRmIiwidGVuYW50X3NsdWciOiJxdWlja3Rlc3Q5OTkiLCJ0ZW5hbnRfcGxhbiI6IlByb2Zlc3Npb25hbCIsImZ1bGxfbmFtZSI6IlRlc3QgQWRtaW4iLCJhdXRoX3Byb3ZpZGVyIjoiTG9jYWwiLCJ0ZW5hbnRfcm9sZSI6IlRlbmFudE93bmVyIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjoiVGVuYW50T3duZXIiLCJleHAiOjE3NjIyNTQ3MzgsImlzcyI6IkNvbGFGbG93LkFQSSIsImF1ZCI6IkNvbGFGbG93LldlYiJ9.RWL-wWNgOleP4eT6uEN-3FXLhS5EijPfjlsu4N82_80"
$PROJECT_ID = "2ffdedc9-7daf-4e11-b9b1-14e9684e91f8"
$ISSUE_ID = "93abd52a-0839-49bf-b58e-985b86c23e33"
$baseUrl = "http://localhost:5167"

$headers = @{
    "Authorization" = "Bearer $TOKEN"
    "Content-Type" = "application/json"
}

Write-Host "=== Issue Management Quick Test ===" -ForegroundColor Cyan
Write-Host ""

# Test 1: List all issues
Write-Host "[1] List all issues" -ForegroundColor Yellow
$issues = Invoke-RestMethod -Uri "$baseUrl/api/v1/projects/$PROJECT_ID/issues" -Headers $headers
Write-Host "Total issues: $($issues.Count)" -ForegroundColor Green
Write-Host ""

# Test 2: Create Bug
Write-Host "[2] Create Bug (Critical)" -ForegroundColor Yellow
$bugBody = @{
    title = "Null reference error in login"
    description = "Critical bug causing crashes"
    type = "Bug"
    priority = "Critical"
} | ConvertTo-Json

$bug = Invoke-RestMethod -Uri "$baseUrl/api/v1/projects/$PROJECT_ID/issues" -Method Post -Headers $headers -Body $bugBody
Write-Host "Created Bug ID: $($bug.id)" -ForegroundColor Green
Write-Host ""

# Test 3: Create Task
Write-Host "[3] Create Task (Medium)" -ForegroundColor Yellow
$taskBody = @{
    title = "Update documentation"
    description = "Document new features"
    type = "Task"
    priority = "Medium"
} | ConvertTo-Json

$task = Invoke-RestMethod -Uri "$baseUrl/api/v1/projects/$PROJECT_ID/issues" -Method Post -Headers $headers -Body $taskBody
Write-Host "Created Task ID: $($task.id)" -ForegroundColor Green
Write-Host ""

# Test 4: List by status (Backlog)
Write-Host "[4] List by status (Backlog)" -ForegroundColor Yellow
$backlog = Invoke-RestMethod -Uri "$baseUrl/api/v1/projects/$PROJECT_ID/issues?status=Backlog" -Headers $headers
Write-Host "Backlog count: $($backlog.Count)" -ForegroundColor Green
Write-Host ""

# Test 5: Change status to InProgress
Write-Host "[5] Change status to InProgress" -ForegroundColor Yellow
$statusBody = @{ status = "InProgress" } | ConvertTo-Json
Invoke-RestMethod -Uri "$baseUrl/api/v1/projects/$PROJECT_ID/issues/$ISSUE_ID/status" -Method Put -Headers $headers -Body $statusBody
Write-Host "Status changed" -ForegroundColor Green
Write-Host ""

# Test 6: List by status (InProgress)
Write-Host "[6] List by status (InProgress)" -ForegroundColor Yellow
$inProgress = Invoke-RestMethod -Uri "$baseUrl/api/v1/projects/$PROJECT_ID/issues?status=InProgress" -Headers $headers
Write-Host "InProgress count: $($inProgress.Count)" -ForegroundColor Green
if ($inProgress.Count -gt 0) {
    Write-Host "First item: $($inProgress[0].title)" -ForegroundColor Gray
}
Write-Host ""

# Test 7: Update issue
Write-Host "[7] Update issue title" -ForegroundColor Yellow
$updateBody = @{
    title = "Implement authentication - Updated"
    description = "Add JWT-based auth with refresh tokens"
    priority = "Critical"
} | ConvertTo-Json
Invoke-RestMethod -Uri "$baseUrl/api/v1/projects/$PROJECT_ID/issues/$ISSUE_ID" -Method Put -Headers $headers -Body $updateBody
Write-Host "Issue updated" -ForegroundColor Green
Write-Host ""

# Test 8: Get updated issue
Write-Host "[8] Get updated issue" -ForegroundColor Yellow
$updated = Invoke-RestMethod -Uri "$baseUrl/api/v1/projects/$PROJECT_ID/issues/$ISSUE_ID" -Headers $headers
Write-Host "Title: $($updated.title)" -ForegroundColor Gray
Write-Host "Priority: $($updated.priority)" -ForegroundColor Gray
Write-Host "Status: $($updated.status)" -ForegroundColor Gray
Write-Host ""

Write-Host "=== All tests completed successfully! ===" -ForegroundColor Green
