#!/bin/bash
# Script to generate and run Wasot syntax tests
# This generates Rust test code from Markdown specifications and runs only those tests

set -e  # Exit on error

# Command line options
FORMAT_CODE=false
VERBOSE=false
QUIET=false

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --format)
            FORMAT_CODE=true
            shift
            ;;
        --verbose|-v)
            VERBOSE=true
            shift
            ;;
        --quiet|-q)
            QUIET=true
            shift
            ;;
        --help|-h)
            echo "Usage: $0 [options]"
            echo ""
            echo "Options:"
            echo "  --format          Enable auto-formatting of generated code (disabled by default)"
            echo "  --verbose, -v     Show verbose test output"
            echo "  --quiet, -q       Show minimal test output"
            echo "  --help, -h        Show this help message"
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            echo "Use --help for usage information"
            exit 1
            ;;
    esac
done

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

# Paths
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
TEST_SPEC="$PROJECT_ROOT/tests/wasot_syntax_tests.md"
GENERATED_TEST="$PROJECT_ROOT/tests/generated_syntax_tests.rs"
GENERATOR="$PROJECT_ROOT/scripts/generate_syntax_tests.py"

echo -e "${GREEN}🚀 Wasot Syntax Test Runner${NC}"
echo "================================"

# Check if Python is available
if ! command -v python3 &> /dev/null; then
    echo -e "${RED}❌ Error: python3 is required but not found${NC}"
    echo "Please install Python 3 to run the test generator"
    exit 1
fi

# Check if PyYAML is installed
if ! python3 -c "import yaml" 2>/dev/null; then
    echo -e "${YELLOW}⚠️  Warning: PyYAML not found${NC}"
    echo "Installing PyYAML..."
    uv pip3 install pyyaml || {
        echo -e "${RED}❌ Failed to install PyYAML${NC}"
        echo "Please run: pip3 install pyyaml"
        exit 1
    }
fi

# Check if test specification exists
if [ ! -f "$TEST_SPEC" ]; then
    echo -e "${RED}❌ Error: Test specification not found at $TEST_SPEC${NC}"
    exit 1
fi

# Check if generator exists
if [ ! -f "$GENERATOR" ]; then
    echo -e "${RED}❌ Error: Test generator not found at $GENERATOR${NC}"
    exit 1
fi

# Generate tests
echo -e "${YELLOW}📝 Generating tests from $TEST_SPEC...${NC}"

# Build generator command with format option
GENERATOR_CMD="python3 \"$GENERATOR\" \"$TEST_SPEC\" \"$GENERATED_TEST\""
if [ "$FORMAT_CODE" = true ]; then
    echo -e "${YELLOW}💫 Auto-formatting enabled${NC}"
    GENERATOR_CMD="$GENERATOR_CMD --format"
else
    echo -e "${YELLOW}⚡ Auto-formatting disabled (use --format to enable)${NC}"
    GENERATOR_CMD="$GENERATOR_CMD --no-format"
fi

# Execute the generator
eval $GENERATOR_CMD || {
    echo -e "${RED}❌ Test generation failed${NC}"
    exit 1
}

# Run the generated tests
echo -e "${YELLOW}🧪 Running generated syntax tests...${NC}"
echo ""

# Run tests with appropriate verbosity
if [ "$VERBOSE" = true ]; then
    cargo test --test generated_syntax_tests -- --nocapture
elif [ "$QUIET" = true ]; then
    cargo test --test generated_syntax_tests --quiet
else
    # Default: show test names but not output
    cargo test --test generated_syntax_tests
fi

# Check exit code
if [ $? -eq 0 ]; then
    echo ""
    echo -e "${GREEN}✅ All syntax tests passed!${NC}"
else
    echo ""
    echo -e "${RED}❌ Some tests failed${NC}"
    echo ""
    echo "Tips for debugging:"
    echo "  - Run with --verbose to see all output"
    echo "  - Check test expectations in $TEST_SPEC"
    echo "  - Use 'wasot listen-demo file.wasot' to see actual events"
    echo "  - Remember: plain text doesn't create implicit block events"
    exit 1
fi