Param(
    [string]$BaseUrl = "http://localhost:5116",
    [string]$AdminKey = $env:ADMIN_PASSWORD
)

if (-not $AdminKey) {
    Write-Error "Set ADMIN_PASSWORD env var or pass -AdminKey."
    exit 1
}

$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession

function Invoke-Json {
    param(
        [Parameter(Mandatory=$true)][string]$Method,
        [Parameter(Mandatory=$true)][string]$Path,
        [object]$Body = $null,
        [hashtable]$Headers = @{}
    )
    $uri = "$BaseUrl$Path"
    $params = @{
        Method = $Method
        Uri    = $uri
        WebSession = $session
        Headers = $Headers
    }
    if ($Body) { $params.Body = ($Body | ConvertTo-Json -Depth 5); $params.ContentType = "application/json" }
    try {
        $response = Invoke-WebRequest @params -ErrorAction Stop
        if ($response.Content) { return ($response.Content | ConvertFrom-Json) }
        return $null
    } catch {
        Write-Error "Request failed: $Method $uri`n$($_.Exception.Message)"
        if ($_.Exception.Response -and $_.Exception.Response.GetResponseStream()) {
            $reader = New-Object IO.StreamReader $_.Exception.Response.GetResponseStream()
            Write-Error "Response body:`n$($reader.ReadToEnd())"
        }
        exit 1
    }
}

Write-Host "1) Health check"
Invoke-Json -Method GET -Path "/health" | Out-Host

Write-Host "`n2) Admin factory reset (clears players, suggestions, votes)"
Invoke-Json -Method POST -Path "/api/admin/factory-reset" -Headers @{ "X-Admin-Key" = $AdminKey } | Out-Host

Write-Host "`n3) Set player name"
$me = Invoke-Json -Method POST -Path "/api/me/name" -Body @{ name = "SmokeTester" }
$me | Out-Host

Write-Host "`n4) Submit three suggestions"
$ids = @()
1..3 | ForEach-Object {
    $resp = Invoke-Json -Method POST -Path "/api/suggestions" -Body @{
        name = "Game $_"
        genre = "Genre $_"
        description = "Autogenerated suggestion $_"
    }
    $ids += $resp.id
    $resp | Out-Host
}

Write-Host "`n4b) Delete the third suggestion (Suggest phase only)"
Invoke-Json -Method DELETE -Path "/api/suggestions/$($ids[2])" | Out-Host
$ids = $ids[0..1]

Write-Host "`n5) Reveal phase"
Invoke-Json -Method POST -Path "/api/admin/phase" -Headers @{ "X-Admin-Key" = $AdminKey } -Body @{ phase = "Reveal" } | Out-Host
Invoke-Json -Method GET -Path "/api/suggestions/all" | Out-Host

Write-Host "`n6) Vote phase"
Invoke-Json -Method POST -Path "/api/admin/phase" -Headers @{ "X-Admin-Key" = $AdminKey } -Body @{ phase = "Vote" } | Out-Host
Invoke-Json -Method POST -Path "/api/votes" -Body @{ suggestionId = $ids[0]; score = 10 } | Out-Host
Invoke-Json -Method POST -Path "/api/votes" -Body @{ suggestionId = $ids[1]; score = 7 } | Out-Host
if ($ids.Count -ge 3) {
    Invoke-Json -Method POST -Path "/api/votes" -Body @{ suggestionId = $ids[2]; score = 5 } | Out-Host
}

Write-Host "`n7) Results phase"
Invoke-Json -Method POST -Path "/api/admin/phase" -Headers @{ "X-Admin-Key" = $AdminKey } -Body @{ phase = "Results" } | Out-Host
Invoke-Json -Method GET -Path "/api/results" | Out-Host

Write-Host "`n8) Admin factory reset (clears players, suggestions, votes)"
Invoke-Json -Method POST -Path "/api/admin/factory-reset" -Headers @{ "X-Admin-Key" = $AdminKey } | Out-Host

Write-Host "`nSmoke test completed."
