# Diagnose 500 errors in detail

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

Write-Host "=== DIAGNOSTIC TEST: Token Refresh 500 Error ===" -ForegroundColor Cyan

# Step 1: Register a tenant
Write-Host "`n1. Registering tenant..." -ForegroundColor Yellow
$slug = "diag-$(Get-Random -Minimum 1000 -Maximum 9999)"
$registerBody = @{
    tenantName = "Diagnostic Test"
    tenantSlug = $slug
    subscriptionPlan = "Free"
    adminEmail = "diag@test.com"
    adminPassword = "Admin@1234"
    adminFullName = "Diag Admin"
} | ConvertTo-Json

try {
    $regResponse = Invoke-RestMethod -Uri "$baseUrl/api/tenants/register" `
        -Method Post -ContentType "application/json" -Body $registerBody
    Write-Host "  Success! Got tokens" -ForegroundColor Green
    Write-Host "  Access Token: $($regResponse.accessToken.Substring(0,30))..." -ForegroundColor Gray
    Write-Host "  Refresh Token: $($regResponse.refreshToken.Substring(0,30))..." -ForegroundColor Gray

    $accessToken = $regResponse.accessToken
    $refreshToken = $regResponse.refreshToken
} catch {
    Write-Host "  FAILED: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# Step 2: Try to refresh the token
Write-Host "`n2. Attempting token refresh..." -ForegroundColor Yellow
$refreshBody = @{
    refreshToken = $refreshToken
} | ConvertTo-Json

Write-Host "  Request Body: $refreshBody" -ForegroundColor Gray

try {
    $refreshResponse = Invoke-WebRequest -Uri "$baseUrl/api/auth/refresh" `
        -Method Post -ContentType "application/json" -Body $refreshBody `
        -UseBasicParsing -ErrorAction Stop

    Write-Host "  Success! Status: $($refreshResponse.StatusCode)" -ForegroundColor Green
    $responseContent = $refreshResponse.Content | ConvertFrom-Json
    Write-Host "  New Access Token: $($responseContent.accessToken.Substring(0,30))..." -ForegroundColor Gray
} catch {
    Write-Host "  FAILED: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "  Status Code: $($_.Exception.Response.StatusCode.value__)" -ForegroundColor Red

    # Try to get response body
    if ($_.Exception.Response) {
        try {
            $stream = $_.Exception.Response.GetResponseStream()
            $reader = New-Object System.IO.StreamReader($stream)
            $responseBody = $reader.ReadToEnd()
            Write-Host "  Response Body: $responseBody" -ForegroundColor DarkRed
        } catch {
            Write-Host "  Could not read response body" -ForegroundColor DarkRed
        }
    }
}

# Step 3: Try to login
Write-Host "`n3. Attempting login..." -ForegroundColor Yellow
$loginBody = @{
    tenantSlug = $slug
    email = "diag@test.com"
    password = "Admin@1234"
} | ConvertTo-Json

Write-Host "  Request Body: $loginBody" -ForegroundColor Gray

try {
    $loginResponse = Invoke-WebRequest -Uri "$baseUrl/api/auth/login" `
        -Method Post -ContentType "application/json" -Body $loginBody `
        -UseBasicParsing -ErrorAction Stop

    Write-Host "  Success! Status: $($loginResponse.StatusCode)" -ForegroundColor Green
    $loginContent = $loginResponse.Content | ConvertFrom-Json
    Write-Host "  Access Token: $($loginContent.accessToken.Substring(0,30))..." -ForegroundColor Gray
} catch {
    Write-Host "  FAILED: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "  Status Code: $($_.Exception.Response.StatusCode.value__)" -ForegroundColor Red

    # Try to get response body
    if ($_.Exception.Response) {
        try {
            $stream = $_.Exception.Response.GetResponseStream()
            $reader = New-Object System.IO.StreamReader($stream)
            $responseBody = $reader.ReadToEnd()
            Write-Host "  Response Body: $responseBody" -ForegroundColor DarkRed
        } catch {
            Write-Host "  Could not read response body" -ForegroundColor DarkRed
        }
    }
}

Write-Host "`n=== END DIAGNOSTIC ===" -ForegroundColor Cyan
