# Verify user field fix
# This script tests the login flow and verifies user.id is correctly set

$baseUrl = "http://localhost:5000"
$loginUrl = "$baseUrl/api/auth/login"

# Login payload
$loginPayload = @{
    email = "admin@test.com"
    password = "Test@123456"
    tenantSlug = "testcompany"
} | ConvertTo-Json

Write-Host "=== Testing Login Flow ===" -ForegroundColor Cyan
Write-Host "Login URL: $loginUrl" -ForegroundColor Gray
Write-Host "Payload: $loginPayload" -ForegroundColor Gray
Write-Host ""

try {
    # Login
    $response = Invoke-RestMethod -Uri $loginUrl -Method Post -Body $loginPayload -ContentType "application/json"

    Write-Host "✓ Login successful!" -ForegroundColor Green
    Write-Host ""

    # Display user object
    Write-Host "=== User Object from Backend ===" -ForegroundColor Cyan
    Write-Host "User ID: $($response.user.id)" -ForegroundColor Yellow
    Write-Host "Email: $($response.user.email)" -ForegroundColor Gray
    Write-Host "Full Name: $($response.user.fullName)" -ForegroundColor Gray
    Write-Host "Tenant ID: $($response.user.tenantId)" -ForegroundColor Gray
    Write-Host ""

    # Check if ID exists
    if ($response.user.id) {
        Write-Host "✓ User ID field exists (camelCase)!" -ForegroundColor Green
        Write-Host "  Field name: id" -ForegroundColor Gray
        Write-Host "  Value: $($response.user.id)" -ForegroundColor Gray
    } elseif ($response.user.Id) {
        Write-Host "✓ User ID field exists (PascalCase)!" -ForegroundColor Green
        Write-Host "  Field name: Id" -ForegroundColor Gray
        Write-Host "  Value: $($response.user.Id)" -ForegroundColor Gray
    } else {
        Write-Host "✗ User ID field missing!" -ForegroundColor Red
    }
    Write-Host ""

    # Test /me endpoint
    Write-Host "=== Testing /me Endpoint ===" -ForegroundColor Cyan
    $token = $response.accessToken
    $headers = @{
        Authorization = "Bearer $token"
    }

    $meResponse = Invoke-RestMethod -Uri "$baseUrl/api/auth/me" -Method Get -Headers $headers

    Write-Host "✓ /me endpoint successful!" -ForegroundColor Green
    Write-Host ""
    Write-Host "=== User Object from /me ===" -ForegroundColor Cyan
    Write-Host "User ID field: $($meResponse.userId)" -ForegroundColor Yellow
    Write-Host "Email: $($meResponse.email)" -ForegroundColor Gray
    Write-Host "Full Name: $($meResponse.fullName)" -ForegroundColor Gray
    Write-Host "Tenant ID: $($meResponse.tenantId)" -ForegroundColor Gray
    Write-Host "Tenant Slug: $($meResponse.tenantSlug)" -ForegroundColor Gray
    Write-Host "Tenant Role: $($meResponse.tenantRole)" -ForegroundColor Gray
    Write-Host ""

    if ($meResponse.userId) {
        Write-Host "✓ userId field exists in /me response!" -ForegroundColor Green
    } else {
        Write-Host "✗ userId field missing in /me response!" -ForegroundColor Red
    }
    Write-Host ""

    Write-Host "=== Field Mapping Required ===" -ForegroundColor Cyan
    Write-Host "Login response:" -ForegroundColor Gray
    if ($response.user.id) {
        Write-Host "  - user.id -> frontend user.id" -ForegroundColor Gray
    } else {
        Write-Host "  - user.Id -> frontend user.id" -ForegroundColor Gray
    }
    Write-Host "/me response:" -ForegroundColor Gray
    Write-Host "  - userId -> frontend user.id" -ForegroundColor Gray
    Write-Host "  - tenantSlug -> frontend user.tenantName" -ForegroundColor Gray
    Write-Host ""

    Write-Host "=== Verification Complete ===" -ForegroundColor Green

} catch {
    Write-Host "✗ Error: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "Details: $($_.ErrorDetails.Message)" -ForegroundColor Red
}
