# Test Login Script using PowerShell
# Tests the login endpoint with tenant credentials

$email = "epsone@blackcarride.com"
$password = "12345678"
$apiUrl = "http://127.0.0.1:8000/api/auth/login"

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Testing Login Endpoint" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Email: $email" -ForegroundColor Yellow
Write-Host "API URL: $apiUrl" -ForegroundColor Yellow
Write-Host ""

# Prepare the request body
$body = @{
    email = $email
    password = $password
} | ConvertTo-Json

try {
    Write-Host "Sending login request..." -ForegroundColor Green
    $response = Invoke-RestMethod -Uri $apiUrl -Method POST -ContentType "application/json" -Body $body

    # Check if successful
    if ($response.success) {
        Write-Host "✅ Login Successful!" -ForegroundColor Green
        Write-Host ""
        Write-Host "User Information:" -ForegroundColor Cyan
        Write-Host "───────────────────────────────────────"
        Write-Host "ID: $($response.user.id)"
        Write-Host "Name: $($response.user.name)"
        Write-Host "Email: $($response.user.email)"
        Write-Host "Role: $($response.user.role)"
        Write-Host "Tenant ID: $($response.user.tenant_id)"
        
        if ($response.user.tenant) {
            Write-Host ""
            Write-Host "✅ Tenant Data Found!" -ForegroundColor Green
            Write-Host "───────────────────────────────────────"
            Write-Host "Tenant ID: $($response.user.tenant.id)"
            Write-Host "Tenant Name: $($response.user.tenant.name)"
            Write-Host "Tenant Slug: $($response.user.tenant.slug)"
        } else {
            Write-Host ""
            Write-Host "⚠️  Tenant is NULL" -ForegroundColor Yellow
        }
        
        Write-Host ""
        Write-Host "Token Information:" -ForegroundColor Cyan
        Write-Host "───────────────────────────────────────"
        Write-Host "Token Type: $($response.token_type)"
        Write-Host "Expires In: $($response.expires_in) seconds"
        Write-Host "Token Length: $($response.token.Length) characters"
        Write-Host "Token: $($response.token.Substring(0, 50))..."
        
        Write-Host ""
        Write-Host "Full Response:" -ForegroundColor Cyan
        Write-Host "───────────────────────────────────────"
        $response | ConvertTo-Json -Depth 10
    } else {
        Write-Host "❌ Login Failed!" -ForegroundColor Red
        Write-Host "Message: $($response.message)" -ForegroundColor Red
    }
}
catch {
    Write-Host "❌ Error: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "Make sure the API server is running on http://127.0.0.1:8000" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Test Completed" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
