#!/bin/bash

# BDD Test Runner and Validator
# Runs all BDD tests and validates there are no undefined, pending, or skipped steps

set -e

echo "🧪 Running BDD tests for DanceLessonsCoach..."
echo "============================================"

# Run tests with verbose output
TEST_OUTPUT=$(go test ./features/... -v 2>&1)
TEST_EXIT_CODE=$?

echo "$TEST_OUTPUT"
echo ""

# Check for failures
echo "🔍 Validating test results..."
echo "============================================"

FAILED=false

# Check for undefined steps
if echo "$TEST_OUTPUT" | grep -q "undefined"; then
    echo "❌ ERROR: Found undefined steps"
    echo "$TEST_OUTPUT" | grep -E "undefined" | sed 's/^/  /'
    FAILED=true
fi

# Check for pending steps
if echo "$TEST_OUTPUT" | grep -q "pending"; then
    echo "❌ ERROR: Found pending steps"
    echo "$TEST_OUTPUT" | grep -E "pending" | sed 's/^/  /'
    FAILED=true
fi

# Check for skipped steps
if echo "$TEST_OUTPUT" | grep -q "skipped"; then
    echo "❌ ERROR: Found skipped steps"
    echo "$TEST_OUTPUT" | grep -E "skipped" | sed 's/^/  /'
    FAILED=true
fi

# Check for test failures
if [ $TEST_EXIT_CODE -ne 0 ]; then
    echo "❌ ERROR: Some tests failed"
    FAILED=true
fi

# Check for no test files
if echo "$TEST_OUTPUT" | grep -q "no test files"; then
    echo "❌ ERROR: No test files found"
    FAILED=true
fi

# Success case
if [ "$FAILED" = false ]; then
    echo "✅ All BDD tests passed successfully"
    echo "✅ No undefined steps found"
    echo "✅ No pending steps found"
    echo "✅ No skipped steps found"
    echo "✅ All scenarios executed successfully"
    echo ""
    echo "🎉 BDD tests are healthy!"
    exit 0
else
    echo ""
    echo "💥 BDD tests have issues that need to be fixed"
    echo ""
    echo "Debugging tips:"
    echo "  1. Run: godog --format=progress --show-step-definitions"
    echo "  2. Check: .vibe/skills/bdd_testing/references/DEBUGGING.md"
    echo "  3. Verify: Step patterns match Godog's exact suggestions"
    echo "  4. Test manually: curl http://localhost:9191/api/ready"
    exit 1
fi