# Test Database Initialization Script
# This script tests the database initialization and seed data
# Usage: .\scripts\test-db-init.ps1

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Testing ColaFlow Database Initialization" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Check if Docker is running
Write-Host "Checking Docker..." -ForegroundColor Yellow
try {
    docker info | Out-Null
    Write-Host "  Docker is running" -ForegroundColor Green
} catch {
    Write-Host "  ERROR: Docker is not running" -ForegroundColor Red
    exit 1
}

Write-Host ""
Write-Host "This test will:" -ForegroundColor White
Write-Host "  1. Stop and remove existing containers" -ForegroundColor Gray
Write-Host "  2. Delete database volumes (fresh start)" -ForegroundColor Gray
Write-Host "  3. Start PostgreSQL container" -ForegroundColor Gray
Write-Host "  4. Wait for initialization scripts to run" -ForegroundColor Gray
Write-Host "  5. Verify database extensions" -ForegroundColor Gray
Write-Host "  6. Verify seed data was created" -ForegroundColor Gray
Write-Host ""

$confirm = Read-Host "Continue? (yes/no)"
if ($confirm -ne "yes") {
    Write-Host "Test cancelled" -ForegroundColor Yellow
    exit 0
}

Write-Host ""
Write-Host "Step 1: Cleaning up existing containers..." -ForegroundColor Yellow
docker-compose down -v | Out-Null
Write-Host "  Containers stopped and volumes removed" -ForegroundColor Green

Write-Host ""
Write-Host "Step 2: Starting PostgreSQL container..." -ForegroundColor Yellow
docker-compose up -d postgres

Write-Host ""
Write-Host "Step 3: Waiting for PostgreSQL to be ready..." -ForegroundColor Yellow
$maxWait = 60
$elapsed = 0
$interval = 2

while ($elapsed -lt $maxWait) {
    $health = docker inspect --format='{{.State.Health.Status}}' colaflow-postgres 2>$null
    if ($health -eq "healthy") {
        Write-Host "  PostgreSQL is ready!" -ForegroundColor Green
        break
    }
    Start-Sleep -Seconds $interval
    $elapsed += $interval
    Write-Host "  Waiting... ($elapsed/$maxWait seconds)" -ForegroundColor Gray
}

if ($elapsed -ge $maxWait) {
    Write-Host "  ERROR: PostgreSQL did not become healthy in time" -ForegroundColor Red
    docker-compose logs postgres
    exit 1
}

Write-Host ""
Write-Host "Step 4: Checking initialization logs..." -ForegroundColor Yellow
docker-compose logs postgres | Select-String -Pattern "ColaFlow"

Write-Host ""
Write-Host "Step 5: Verifying database extensions..." -ForegroundColor Yellow
$extensions = docker exec colaflow-postgres psql -U colaflow -d colaflow -t -c "\dx" 2>$null

if ($extensions -match "uuid-ossp") {
    Write-Host "  uuid-ossp extension: INSTALLED" -ForegroundColor Green
} else {
    Write-Host "  uuid-ossp extension: MISSING" -ForegroundColor Red
}

if ($extensions -match "pg_trgm") {
    Write-Host "  pg_trgm extension: INSTALLED" -ForegroundColor Green
} else {
    Write-Host "  pg_trgm extension: MISSING" -ForegroundColor Red
}

if ($extensions -match "btree_gin") {
    Write-Host "  btree_gin extension: INSTALLED" -ForegroundColor Green
} else {
    Write-Host "  btree_gin extension: MISSING" -ForegroundColor Red
}

Write-Host ""
Write-Host "Step 6: Checking if schemas exist (need migrations)..." -ForegroundColor Yellow
$schemas = docker exec colaflow-postgres psql -U colaflow -d colaflow -t -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name IN ('identity', 'project_management');" 2>$null

if ($schemas -match "identity") {
    Write-Host "  identity schema: EXISTS" -ForegroundColor Green

    Write-Host ""
    Write-Host "Step 7: Verifying seed data in identity schema..." -ForegroundColor Yellow

    $tenantCount = docker exec colaflow-postgres psql -U colaflow -d colaflow -t -c "SELECT COUNT(*) FROM identity.tenants;" 2>$null
    if ($tenantCount -and $tenantCount -gt 0) {
        Write-Host "  Tenants: $tenantCount records" -ForegroundColor Green
    } else {
        Write-Host "  Tenants: NO DATA (seed script may not have run)" -ForegroundColor Yellow
    }

    $userCount = docker exec colaflow-postgres psql -U colaflow -d colaflow -t -c "SELECT COUNT(*) FROM identity.users;" 2>$null
    if ($userCount -and $userCount -gt 0) {
        Write-Host "  Users: $userCount records" -ForegroundColor Green
    } else {
        Write-Host "  Users: NO DATA (seed script may not have run)" -ForegroundColor Yellow
    }

} else {
    Write-Host "  identity schema: DOES NOT EXIST" -ForegroundColor Yellow
    Write-Host "  NOTE: Run EF Core migrations to create schema" -ForegroundColor Gray
    Write-Host "  Command: docker-compose exec backend dotnet ef database update" -ForegroundColor Gray
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Test Summary" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Extensions Installation:" -ForegroundColor White
Write-Host "  Script files are mounted correctly if PostgreSQL started" -ForegroundColor Green
Write-Host ""
Write-Host "Seed Data:" -ForegroundColor White
Write-Host "  Seed data will be created AFTER EF Core migrations" -ForegroundColor Yellow
Write-Host "  To complete setup:" -ForegroundColor White
Write-Host "    1. Start backend: docker-compose up -d backend" -ForegroundColor Gray
Write-Host "    2. Apply migrations: docker-compose exec backend dotnet ef database update" -ForegroundColor Gray
Write-Host "    3. Re-check seed data: docker-compose logs postgres | Select-String 'Seed Data'" -ForegroundColor Gray
Write-Host ""
Write-Host "Demo Accounts:" -ForegroundColor White
Write-Host "  See scripts/DEMO-ACCOUNTS.md for credentials" -ForegroundColor Cyan
Write-Host ""
