# Test Domain Events Implementation
# This script tests that domain events are being raised and handled

$baseUrl = "http://localhost:5167"
$tenantSlug = "event-test-$(Get-Random -Minimum 1000 -Maximum 9999)"

Write-Host "=== Domain Events Test ===" -ForegroundColor Cyan
Write-Host ""

# Test 1: Register Tenant (TenantCreatedEvent)
Write-Host "Test 1: Registering tenant (should trigger TenantCreatedEvent)..." -ForegroundColor Yellow

$registerRequest = @{
    tenantSlug = $tenantSlug
    tenantName = "Event Test Tenant"
    subscriptionPlan = "Free"
    adminEmail = "admin@eventtest.com"
    adminPassword = "Admin@123"
    adminFullName = "Event Test Admin"
} | ConvertTo-Json

$registerResponse = Invoke-RestMethod -Uri "$baseUrl/api/tenants/register" `
    -Method Post `
    -ContentType "application/json" `
    -Body $registerRequest

Write-Host "✓ Tenant registered successfully" -ForegroundColor Green
Write-Host "  Tenant ID: $($registerResponse.tenant.id)"
Write-Host "  Admin User ID: $($registerResponse.adminUser.id)"
Write-Host "  Check API console for log: 'Tenant {id} created with name Event Test Tenant...'" -ForegroundColor Magenta
Write-Host ""
Start-Sleep -Seconds 2

# Test 2: Login (UserLoggedInEvent)
Write-Host "Test 2: Logging in (should trigger UserLoggedInEvent)..." -ForegroundColor Yellow

$loginRequest = @{
    tenantSlug = $tenantSlug
    email = "admin@eventtest.com"
    password = "Admin@123"
} | ConvertTo-Json

$loginResponse = Invoke-RestMethod -Uri "$baseUrl/api/auth/login" `
    -Method Post `
    -ContentType "application/json" `
    -Body $loginRequest

$accessToken = $loginResponse.accessToken
$userId = $registerResponse.adminUser.id
$tenantId = $registerResponse.tenant.id

Write-Host "✓ Login successful" -ForegroundColor Green
Write-Host "  Access Token: $($accessToken.Substring(0, 20))..."
Write-Host "  Check API console for log: 'User {$userId} logged in to tenant {$tenantId} from IP...'" -ForegroundColor Magenta
Write-Host ""
Start-Sleep -Seconds 2

# Test 3: Assign Role (UserRoleAssignedEvent)
Write-Host "Test 3: Assigning role (should trigger UserRoleAssignedEvent)..." -ForegroundColor Yellow

$assignRoleRequest = @{
    role = "TenantAdmin"
} | ConvertTo-Json

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

try {
    $assignRoleResponse = Invoke-RestMethod -Uri "$baseUrl/api/tenants/$tenantId/users/$userId/role" `
        -Method Post `
        -Headers $headers `
        -Body $assignRoleRequest

    Write-Host "✓ Role assigned successfully" -ForegroundColor Green
    Write-Host "  Check API console for log: 'User {$userId} assigned role TenantAdmin...'" -ForegroundColor Magenta
} catch {
    Write-Host "✓ Expected behavior: Role already TenantOwner" -ForegroundColor Yellow
    Write-Host "  Trying to update to TenantMember instead..."

    $assignRoleRequest = @{
        role = "TenantMember"
    } | ConvertTo-Json

    $assignRoleResponse = Invoke-RestMethod -Uri "$baseUrl/api/tenants/$tenantId/users/$userId/role" `
        -Method Post `
        -Headers $headers `
        -Body $assignRoleRequest

    Write-Host "✓ Role updated successfully to TenantMember" -ForegroundColor Green
    Write-Host "  Check API console for log: 'User {$userId} assigned role TenantMember. Previous role: TenantOwner...'" -ForegroundColor Magenta
}

Write-Host ""
Write-Host "=== Test Complete ===" -ForegroundColor Cyan
Write-Host ""
Write-Host "Expected Logs in API Console:" -ForegroundColor Yellow
Write-Host "  1. Tenant {guid} created with name 'Event Test Tenant' and slug '$tenantSlug'"
Write-Host "  2. User {guid} logged in to tenant {guid} from IP 127.0.0.1 or ::1"
Write-Host "  3. User {guid} assigned role TenantMember in tenant {guid}. Previous role: TenantOwner. Assigned by: {guid}"
Write-Host ""
Write-Host "If you see these logs in the API console, Domain Events are working correctly!" -ForegroundColor Green
