#!/bin/bash
# Dashboard Separation Verification Script
# Run this after updating Node.js to >=20.9.0

set -e

echo "==================================="
echo "Dashboard Separation Verification"
echo "==================================="
echo ""

# Check Node.js version
echo "1. Checking Node.js version..."
NODE_VERSION=$(node -v)
echo "   Current Node.js: $NODE_VERSION"
echo "   Required: >=20.9.0"
echo ""

# Check directory structure
echo "2. Verifying directory structure..."
if [ -d "applications/stemedb-dashboard" ]; then
    echo "   ✅ StemeDB Dashboard directory exists"
else
    echo "   ❌ StemeDB Dashboard directory missing"
    exit 1
fi

if [ -d "applications/aphoria-dashboard" ]; then
    echo "   ✅ Aphoria Dashboard directory exists"
else
    echo "   ❌ Aphoria Dashboard directory missing"
    exit 1
fi
echo ""

# Check package.json ports
echo "3. Verifying port configuration..."
STEMEDB_PORT=$(grep '"dev":' applications/stemedb-dashboard/package.json | grep -o '18188' || echo "")
APHORIA_PORT=$(grep '"dev":' applications/aphoria-dashboard/package.json | grep -o '18189' || echo "")

if [ "$STEMEDB_PORT" = "18188" ]; then
    echo "   ✅ StemeDB Dashboard: port 18188"
else
    echo "   ❌ StemeDB Dashboard: port mismatch (expected 18188)"
    exit 1
fi

if [ "$APHORIA_PORT" = "18189" ]; then
    echo "   ✅ Aphoria Dashboard: port 18189"
else
    echo "   ❌ Aphoria Dashboard: port mismatch (expected 18189)"
    exit 1
fi
echo ""

# Check StemeDB routes (should NOT have Aphoria routes)
echo "4. Verifying StemeDB Dashboard routes..."
if [ -d "applications/stemedb-dashboard/src/app/scans" ]; then
    echo "   ❌ ERROR: StemeDB Dashboard still has /scans route"
    exit 1
fi
if [ -d "applications/stemedb-dashboard/src/app/claims" ]; then
    echo "   ❌ ERROR: StemeDB Dashboard still has /claims route"
    exit 1
fi
if [ -d "applications/stemedb-dashboard/src/app/corpus" ]; then
    echo "   ❌ ERROR: StemeDB Dashboard still has /corpus route"
    exit 1
fi
echo "   ✅ Aphoria routes removed from StemeDB Dashboard"
echo ""

# Check Aphoria routes (should have all 3)
echo "5. Verifying Aphoria Dashboard routes..."
if [ ! -d "applications/aphoria-dashboard/src/app/scans" ]; then
    echo "   ❌ ERROR: Aphoria Dashboard missing /scans route"
    exit 1
fi
if [ ! -d "applications/aphoria-dashboard/src/app/claims" ]; then
    echo "   ❌ ERROR: Aphoria Dashboard missing /claims route"
    exit 1
fi
if [ ! -d "applications/aphoria-dashboard/src/app/corpus" ]; then
    echo "   ❌ ERROR: Aphoria Dashboard missing /corpus route"
    exit 1
fi
echo "   ✅ All 3 Aphoria routes present"
echo ""

# Check shared components exist in both
echo "6. Verifying shared components..."
SHARED_COMPONENTS=(
    "src/components/ui/button.tsx"
    "src/components/shared/api-status.tsx"
    "src/lib/api/client.ts"
)

for component in "${SHARED_COMPONENTS[@]}"; do
    if [ ! -f "applications/stemedb-dashboard/$component" ]; then
        echo "   ❌ Missing in StemeDB: $component"
        exit 1
    fi
    if [ ! -f "applications/aphoria-dashboard/$component" ]; then
        echo "   ❌ Missing in Aphoria: $component"
        exit 1
    fi
done
echo "   ✅ Shared components present in both dashboards"
echo ""

# Try to build (optional, skip if Node version is too old)
echo "7. Build verification (optional)..."
if [[ "$NODE_VERSION" < "v20.9.0" ]]; then
    echo "   ⚠️  SKIPPED: Node.js version too old (need >=20.9.0)"
    echo "      Update Node.js and re-run this script to verify builds"
else
    echo "   Building StemeDB Dashboard..."
    (cd applications/stemedb-dashboard && npm run build > /dev/null 2>&1)
    if [ $? -eq 0 ]; then
        echo "   ✅ StemeDB Dashboard builds successfully"
    else
        echo "   ❌ StemeDB Dashboard build failed"
        exit 1
    fi

    echo "   Building Aphoria Dashboard..."
    (cd applications/aphoria-dashboard && npm run build > /dev/null 2>&1)
    if [ $? -eq 0 ]; then
        echo "   ✅ Aphoria Dashboard builds successfully"
    else
        echo "   ❌ Aphoria Dashboard build failed"
        exit 1
    fi
fi
echo ""

# Summary
echo "==================================="
echo "✅ Verification Complete!"
echo "==================================="
echo ""
echo "Next steps:"
echo "1. Update Node.js to >=20.9.0 (if needed)"
echo "2. Run: cargo run --bin stemedb-api"
echo "3. Run: cd applications/stemedb-dashboard && npm run dev"
echo "4. Run: cd applications/aphoria-dashboard && npm run dev"
echo "5. Visit:"
echo "   - StemeDB: http://localhost:18188"
echo "   - Aphoria: http://localhost:18189"
echo ""
