# ColaFlow Integration Tests - Run Script
# This script helps you run the integration tests with proper setup and cleanup

Write-Host "================================================" -ForegroundColor Cyan
Write-Host "  ColaFlow Integration Tests - Run Script" -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor Cyan
Write-Host ""

# Step 1: Stop any running API processes
Write-Host "[1/4] 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
} else {
    Write-Host "  No running processes found." -ForegroundColor Green
}
Write-Host ""

# Step 2: Clean build artifacts
Write-Host "[2/4] Cleaning build artifacts..." -ForegroundColor Yellow
dotnet clean --verbosity quiet
if ($LASTEXITCODE -eq 0) {
    Write-Host "  Done." -ForegroundColor Green
} else {
    Write-Host "  Warning: Clean failed, but continuing..." -ForegroundColor DarkYellow
}
Write-Host ""

# Step 3: Build solution
Write-Host "[3/4] Building solution..." -ForegroundColor Yellow
dotnet build --verbosity minimal --nologo
if ($LASTEXITCODE -ne 0) {
    Write-Host ""
    Write-Host "Build failed! Please check the errors above." -ForegroundColor Red
    exit 1
}
Write-Host "  Done." -ForegroundColor Green
Write-Host ""

# Step 4: Run integration tests
Write-Host "[4/4] Running integration tests..." -ForegroundColor Yellow
Write-Host ""
Write-Host "================================================" -ForegroundColor Cyan
Write-Host ""

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 tests passed." -ForegroundColor Green
    Write-Host ""
    Write-Host "Test Summary:" -ForegroundColor Cyan
    Write-Host "  - Authentication Tests (Day 4 Regression): 10 tests" -ForegroundColor White
    Write-Host "  - Refresh Token Tests (Phase 1): 9 tests" -ForegroundColor White
    Write-Host "  - RBAC Tests (Phase 2): 11 tests" -ForegroundColor White
    Write-Host "  - Total: 30 integration tests" -ForegroundColor White
    Write-Host ""
    Write-Host "Day 5 implementation verified successfully!" -ForegroundColor Green
} else {
    Write-Host "FAILED! Some tests did not pass." -ForegroundColor Red
    Write-Host ""
    Write-Host "Troubleshooting:" -ForegroundColor Yellow
    Write-Host "  1. Check test output above for specific failures" -ForegroundColor White
    Write-Host "  2. Verify Day 5 implementation is complete" -ForegroundColor White
    Write-Host "  3. Check that /api/auth/refresh endpoint exists" -ForegroundColor White
    Write-Host "  4. Verify RBAC roles are being assigned correctly" -ForegroundColor White
    Write-Host ""
    Write-Host "For detailed documentation, see:" -ForegroundColor Yellow
    Write-Host "  - README.md (comprehensive guide)" -ForegroundColor White
    Write-Host "  - QUICK_START.md (quick start guide)" -ForegroundColor White
}

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

exit $testExitCode
