# ColaFlow Integration Tests - Run Specific Category
# Usage: .\run-integration-tests-category.ps1 [category]
# Categories: RefreshToken, RBAC, Authentication, All

param(
    [Parameter(Position=0)]
    [ValidateSet("RefreshToken", "RBAC", "Authentication", "All")]
    [string]$Category = "All"
)

Write-Host "================================================" -ForegroundColor Cyan
Write-Host "  ColaFlow Integration Tests - Category: $Category" -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor Cyan
Write-Host ""

# Step 1: Stop any running API processes
Write-Host "[1/3] Stopping any running ColaFlow API processes..." -ForegroundColor Yellow
$processes = Get-Process | Where-Object { $_.ProcessName -like "*ColaFlow*" }
if ($processes) {
    $processes | ForEach-Object {
        Write-Host "  Killing process: $($_.ProcessName) (PID: $($_.Id))" -ForegroundColor Gray
        Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue
    }
    Start-Sleep -Seconds 2
}
Write-Host "  Done." -ForegroundColor Green
Write-Host ""

# Step 2: Build if needed
Write-Host "[2/3] Building solution (if needed)..." -ForegroundColor Yellow
dotnet build tests/Modules/Identity/ColaFlow.Modules.Identity.IntegrationTests --verbosity quiet --nologo
if ($LASTEXITCODE -ne 0) {
    Write-Host ""
    Write-Host "Build failed! Running full rebuild..." -ForegroundColor Yellow
    dotnet clean --verbosity quiet
    dotnet build --verbosity minimal --nologo
    if ($LASTEXITCODE -ne 0) {
        Write-Host "Build failed! Please check the errors above." -ForegroundColor Red
        exit 1
    }
}
Write-Host "  Done." -ForegroundColor Green
Write-Host ""

# Step 3: Run tests based on category
Write-Host "[3/3] Running $Category tests..." -ForegroundColor Yellow
Write-Host ""
Write-Host "================================================" -ForegroundColor Cyan
Write-Host ""

$filter = switch ($Category) {
    "RefreshToken" { "FullyQualifiedName~RefreshTokenTests" }
    "RBAC" { "FullyQualifiedName~RbacTests" }
    "Authentication" { "FullyQualifiedName~AuthenticationTests" }
    "All" { $null }
}

if ($filter) {
    Write-Host "Running tests with filter: $filter" -ForegroundColor Gray
    Write-Host ""
    dotnet test tests/Modules/Identity/ColaFlow.Modules.Identity.IntegrationTests `
        --no-build `
        --filter "$filter" `
        --verbosity normal `
        --logger "console;verbosity=detailed"
} else {
    dotnet test tests/Modules/Identity/ColaFlow.Modules.Identity.IntegrationTests `
        --no-build `
        --verbosity normal `
        --logger "console;verbosity=detailed"
}

$testExitCode = $LASTEXITCODE

Write-Host ""
Write-Host "================================================" -ForegroundColor Cyan
Write-Host ""

if ($testExitCode -eq 0) {
    Write-Host "SUCCESS! All $Category tests passed." -ForegroundColor Green

    switch ($Category) {
        "RefreshToken" {
            Write-Host ""
            Write-Host "Refresh Token Tests Passed (9 tests):" -ForegroundColor Cyan
            Write-Host "  - Token generation on registration/login" -ForegroundColor White
            Write-Host "  - Token refresh with new pair generation" -ForegroundColor White
            Write-Host "  - Token rotation (old token invalidated)" -ForegroundColor White
            Write-Host "  - Invalid token rejection" -ForegroundColor White
            Write-Host "  - Logout token revocation" -ForegroundColor White
            Write-Host "  - User identity preservation" -ForegroundColor White
            Write-Host "  - Multiple refresh operations" -ForegroundColor White
        }
        "RBAC" {
            Write-Host ""
            Write-Host "RBAC Tests Passed (11 tests):" -ForegroundColor Cyan
            Write-Host "  - TenantOwner role assignment" -ForegroundColor White
            Write-Host "  - JWT role claims (role, tenant_role)" -ForegroundColor White
            Write-Host "  - Role persistence across login/refresh" -ForegroundColor White
            Write-Host "  - /api/auth/me returns role information" -ForegroundColor White
            Write-Host "  - Protected endpoint authorization" -ForegroundColor White
            Write-Host "  - Role consistency across all flows" -ForegroundColor White
        }
        "Authentication" {
            Write-Host ""
            Write-Host "Authentication Tests Passed (10 tests):" -ForegroundColor Cyan
            Write-Host "  - Tenant registration" -ForegroundColor White
            Write-Host "  - Login with correct/incorrect credentials" -ForegroundColor White
            Write-Host "  - Protected endpoint access control" -ForegroundColor White
            Write-Host "  - JWT token generation" -ForegroundColor White
            Write-Host "  - Password hashing (BCrypt)" -ForegroundColor White
            Write-Host "  - Complete auth flow" -ForegroundColor White
        }
        "All" {
            Write-Host ""
            Write-Host "All Tests Passed (30 tests):" -ForegroundColor Cyan
            Write-Host "  - Authentication Tests: 10 tests" -ForegroundColor White
            Write-Host "  - Refresh Token Tests: 9 tests" -ForegroundColor White
            Write-Host "  - RBAC Tests: 11 tests" -ForegroundColor White
        }
    }
} else {
    Write-Host "FAILED! Some $Category tests did not pass." -ForegroundColor Red
    Write-Host ""
    Write-Host "Check test output above for specific failures." -ForegroundColor Yellow
}

Write-Host ""
Write-Host "================================================" -ForegroundColor Cyan

# Show usage hint
if ($testExitCode -eq 0) {
    Write-Host ""
    Write-Host "Tip: Run other test categories:" -ForegroundColor Cyan
    Write-Host "  .\run-integration-tests-category.ps1 RefreshToken" -ForegroundColor Gray
    Write-Host "  .\run-integration-tests-category.ps1 RBAC" -ForegroundColor Gray
    Write-Host "  .\run-integration-tests-category.ps1 Authentication" -ForegroundColor Gray
    Write-Host "  .\run-integration-tests-category.ps1 All" -ForegroundColor Gray
}

exit $testExitCode
