#!/bin/bash
# Comprehensive verification script
#
# Builds the project and runs all test suites:
# 1. Build release binary
# 2. Rust unit tests (fast + slow)
# 3. CLI integration tests (against built binary)
# 4. Syntax tests (generated from markdown)
# 5. Roundtrip tests on example files
# 6. Roundtrip tests on syntax test cases
#
# Usage:
#   ./scripts/verify.sh          # Run all tests
#   ./scripts/verify.sh --fast   # Skip slow Rust tests

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
BINARY="$PROJECT_ROOT/target/release/wasot"

# Parse arguments
SKIP_SLOW=false
for arg in "$@"; do
    case $arg in
        --fast)
            SKIP_SLOW=true
            ;;
    esac
done

# Track results
declare -a STEP_NAMES
declare -a STEP_RESULTS

run_step() {
    local name="$1"
    shift
    echo ""
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "  $name"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    STEP_NAMES+=("$name")
    if "$@"; then
        echo "  ✓ $name"
        STEP_RESULTS+=("✓")
    else
        echo "  ✗ $name FAILED"
        STEP_RESULTS+=("✗")
    fi
}

echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║                    WASOT VERIFICATION                        ║"
echo "╚══════════════════════════════════════════════════════════════╝"

# Step 1: Build release binary
run_step "Building release binary" cargo build --release

# Step 2: Rust unit tests (fast)
run_step "Rust unit tests (fast)" cargo test

# Step 3: Rust unit tests (slow) - optional
if [ "$SKIP_SLOW" = false ]; then
    run_step "Rust unit tests (slow)" cargo test -- --ignored
else
    STEP_NAMES+=("Rust unit tests (slow)")
    STEP_RESULTS+=("⊘")
    echo ""
    echo "  ⊘ Skipping slow Rust tests (--fast mode)"
fi

# Step 4: CLI integration tests (against built binary)
run_step "CLI integration tests" \
    env WASOT_TEST_BINARY="$BINARY" cargo test --test cli_tests

# Step 5: Syntax tests (generated from markdown)
run_step "Generated syntax tests" "$SCRIPT_DIR/test_syntax.sh" --quiet

# Step 6: Roundtrip tests on example files
run_step "Roundtrip tests (examples)" python3 "$SCRIPT_DIR/roundtrip_test.py" -e "$BINARY"

# Step 7: Roundtrip tests on syntax test cases
run_step "Roundtrip tests (syntax)" python3 "$PROJECT_ROOT/tests/roundtrip_syntax_tests.py" -e "$BINARY"

# Step 8: Performance benchmarks with assertions
run_step "Performance benchmarks" "$SCRIPT_DIR/benchmark_cli.py" --quick --assert

# Step 9: Python unit tests
run_step "Python unit tests" "$PROJECT_ROOT/python/scripts/test.sh"

# Summary
echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║                       SUMMARY                                ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""

FAILED=0
for i in "${!STEP_NAMES[@]}"; do
    result="${STEP_RESULTS[$i]}"
    name="${STEP_NAMES[$i]}"
    if [ "$result" = "✓" ]; then
        echo "  $result $name — passed"
    elif [ "$result" = "⊘" ]; then
        echo "  $result $name — skipped"
    else
        echo "  $result $name — FAILED"
        FAILED=1
    fi
done

echo ""
if [ $FAILED -eq 0 ]; then
    echo "ALL TESTS PASSED"
    exit 0
else
    echo "SOME TESTS FAILED"
    exit 1
fi
