#!/bin/bash
# Verify Project 2 is ready to launch

echo "=== Project 2 Readiness Check ==="
echo

PASS=0
FAIL=0

# Check 1: Project 1 corpus exists
echo "1. Checking Project 1 corpus..."
CLAIMS_COUNT=$(curl -s 'http://localhost:18180/v1/aphoria/corpus' 2>/dev/null | jq '[.items[] | select(.subject | contains("dbpool"))] | length' 2>/dev/null)
if [ "$CLAIMS_COUNT" = "27" ]; then
    echo "   ✅ PASS - 27 dbpool claims in corpus"
    ((PASS++))
else
    echo "   ❌ FAIL - Expected 27 claims, got: ${CLAIMS_COUNT:-ERROR}"
    echo "   → Run Project 1 Day 1 first"
    ((FAIL++))
fi

# Check 2: Skills installed
echo "2. Checking skills installation..."
SKILLS_COUNT=$(ls -la ~/.claude/skills/ 2>/dev/null | grep aphoria | wc -l)
if [ "$SKILLS_COUNT" -ge "8" ]; then
    echo "   ✅ PASS - $SKILLS_COUNT Aphoria skills installed"
    ((PASS++))
else
    echo "   ❌ FAIL - Expected 8 skills, found: $SKILLS_COUNT"
    echo "   → Install skills in ~/.claude/skills/"
    ((FAIL++))
fi

# Check 3: API running
echo "3. Checking API health..."
API_STATUS=$(curl -s http://localhost:18180/health 2>/dev/null | jq -r '.status' 2>/dev/null)
if [ "$API_STATUS" = "healthy" ]; then
    echo "   ✅ PASS - StemeDB API running"
    ((PASS++))
else
    echo "   ❌ FAIL - API not responding"
    echo "   → Start stemedb-api with STEMEDB_CORPUS_DB_DIR env var"
    ((FAIL++))
fi

# Check 4: Documentation exists
echo "4. Checking documentation..."
if [ -f "PROJECT2-QUICKSTART.md" ]; then
    echo "   ✅ PASS - PROJECT2-QUICKSTART.md exists"
    ((PASS++))
else
    echo "   ❌ FAIL - Missing PROJECT2-QUICKSTART.md"
    ((FAIL++))
fi

echo
echo "=== Summary ==="
echo "Passed: $PASS/4"
echo "Failed: $FAIL/4"
echo

if [ $FAIL -eq 0 ]; then
    echo "✅ ALL CHECKS PASSED - Ready to launch Project 2!"
    echo
    echo "Next steps:"
    echo "1. Choose your Project 2 domain (httpclient, grpc-client, cache-client)"
    echo "2. Follow: PROJECT2-QUICKSTART.md"
    echo "3. Expected Day 1 time: <2 hours (vs Project 1's 4 hours)"
    exit 0
else
    echo "❌ $FAIL CHECK(S) FAILED - Fix issues above before proceeding"
    exit 1
fi
