#!/bin/bash
# Verify dbpool directory is reset and ready for next team run

set -e

echo "=== Dogfood Directory Reset Verification ==="
echo

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

PASS_COUNT=0
FAIL_COUNT=0

check_pass() {
    echo -e "${GREEN}✓${NC} $1"
    PASS_COUNT=$((PASS_COUNT + 1))
}

check_fail() {
    echo -e "${RED}✗${NC} $1"
    FAIL_COUNT=$((FAIL_COUNT + 1))
}

check_info() {
    echo -e "${YELLOW}ℹ${NC} $1"
}

echo "=== Documentation Files ==="

# Check CHECKLIST.md has updated Day 1
if grep -q "## Day 1: Create 25-30 Corpus Claims" CHECKLIST.md; then
    check_pass "CHECKLIST.md Day 1 heading updated"
else
    check_fail "CHECKLIST.md Day 1 heading not updated"
fi

# Check for 27 claim checkboxes
CHECKBOX_COUNT=$(grep -c "\- \[ \].*dbpool/" CHECKLIST.md || echo "0")
if [ "$CHECKBOX_COUNT" -ge 27 ]; then
    check_pass "CHECKLIST.md has $CHECKBOX_COUNT claim checkboxes (≥27 expected)"
else
    check_fail "CHECKLIST.md has only $CHECKBOX_COUNT claim checkboxes (27 expected)"
fi

# Check practice bridge exists
if grep -q "Practice Claim 1" CHECKLIST.md; then
    check_pass "Practice bridge added to CHECKLIST.md"
else
    check_fail "Practice bridge missing from CHECKLIST.md"
fi

# Check flywheel setup doc exists
if [ -f "docs/flywheel-setup.md" ]; then
    check_pass "docs/flywheel-setup.md exists"
else
    check_fail "docs/flywheel-setup.md missing"
fi

# Check README exists
if [ -f "README.md" ]; then
    check_pass "README.md exists"
else
    check_fail "README.md missing"
fi

# Check reset documentation
if [ -f "RESET-2026-02-09.md" ]; then
    check_pass "RESET-2026-02-09.md exists"
else
    check_fail "RESET-2026-02-09.md missing"
fi

echo
echo "=== Source Documents ==="

# Check all 3 source documents exist
if [ -f "docs/sources/hikaricp-config.md" ]; then
    check_pass "HikariCP source document preserved"
else
    check_fail "HikariCP source document missing"
fi

if [ -f "docs/sources/owasp-credentials.md" ]; then
    check_pass "OWASP source document preserved"
else
    check_fail "OWASP source document missing"
fi

if [ -f "docs/sources/postgresql-pooling.md" ]; then
    check_pass "PostgreSQL source document preserved"
else
    check_fail "PostgreSQL source document missing"
fi

echo
echo "=== Configuration ==="

# Check .aphoria/config.toml exists
if [ -f ".aphoria/config.toml" ]; then
    check_pass ".aphoria/config.toml exists"

    # Check for persistent mode
    if grep -q 'mode = "persistent"' .aphoria/config.toml; then
        check_pass "Episteme mode set to persistent"
    else
        check_fail "Episteme mode not set to persistent"
    fi

    # Check for aggregation enabled
    if grep -q "aggregation_enabled = true" .aphoria/config.toml; then
        check_pass "Corpus aggregation enabled"
    else
        check_fail "Corpus aggregation not enabled"
    fi
else
    check_fail ".aphoria/config.toml missing"
fi

echo
echo "=== Clean State ==="

# Verify src/ does not exist
if [ ! -d "src" ]; then
    check_pass "src/ directory removed (clean state)"
else
    check_fail "src/ directory still exists (should be removed)"
fi

# Verify tests/ does not exist
if [ ! -d "tests" ]; then
    check_pass "tests/ directory removed (clean state)"
else
    check_fail "tests/ directory still exists (should be removed)"
fi

# Verify Cargo.toml does not exist
if [ ! -f "Cargo.toml" ]; then
    check_pass "Cargo.toml removed (clean state)"
else
    check_fail "Cargo.toml still exists (should be removed)"
fi

# Verify no scan results
SCAN_FILES=$(ls scan-results-*.json 2>/dev/null | wc -l)
if [ "$SCAN_FILES" -eq 0 ]; then
    check_pass "No scan result files (clean state)"
else
    check_fail "Found $SCAN_FILES scan result files (should be removed)"
fi

echo
echo "=== Evaluation Records ==="

# Check eval directory exists
if [ -d "eval" ]; then
    check_pass "eval/ directory preserved"

    # Check key evaluation files
    if [ -f "eval/EVALUATION-REPORT-2026-02-09.md" ]; then
        check_pass "Evaluation report preserved"
    fi

    if [ -f "eval/IMPLEMENTATION-SUMMARY.md" ]; then
        check_pass "Implementation summary moved to eval/"
    fi
else
    check_fail "eval/ directory missing"
fi

echo
echo "=== Scripts ==="

# Check validate-setup.sh exists
if [ -f "scripts/validate-setup.sh" ]; then
    check_pass "Pre-flight validator exists"

    # Check if executable
    if [ -x "scripts/validate-setup.sh" ]; then
        check_pass "Pre-flight validator is executable"
    else
        check_info "Pre-flight validator not executable (run: chmod +x scripts/validate-setup.sh)"
    fi
else
    check_fail "Pre-flight validator missing"
fi

echo
echo "=== Summary ==="
echo "Passed: $PASS_COUNT"
echo "Failed: $FAIL_COUNT"
echo

if [ "$FAIL_COUNT" -eq 0 ]; then
    echo -e "${GREEN}✓ All checks passed. Directory is ready for next team run!${NC}"
    echo
    echo "Next steps:"
    echo "  1. Run: ./scripts/validate-setup.sh (pre-flight check)"
    echo "  2. Read: cat README.md"
    echo "  3. Start: cat CHECKLIST.md | head -300"
    exit 0
else
    echo -e "${RED}✗ $FAIL_COUNT check(s) failed. Please review above.${NC}"
    exit 1
fi
