#!/usr/bin/env bash
# vyre quality gate — run before any merge.
# Exit 0 = pass. Exit 1 = fail with actionable message.
set -euo pipefail

VYRE_DIR="$(cd "$(dirname "$0")" && pwd)"
CONFORM_DIR="$VYRE_DIR/../vyre-conform"

echo "=== vyre quality gate ==="

# 1. Compile check (catches syntax errors, missing imports)
echo "[1/6] cargo check -p vyre"
cargo check -p vyre --manifest-path "$VYRE_DIR/Cargo.toml" 2>&1 || {
    echo "FAIL: vyre does not compile. Fix: read the compiler errors above."
    exit 1
}

# 2. Clippy (catches common mistakes)
echo "[2/6] cargo clippy -p vyre"
cargo clippy -p vyre --manifest-path "$VYRE_DIR/Cargo.toml" -- -D warnings 2>&1 || {
    echo "FAIL: clippy warnings. Fix: address each warning above."
    exit 1
}

# 3. Internal tests (our implementation)
echo "[3/6] cargo test -p vyre"
cargo test -p vyre --manifest-path "$VYRE_DIR/Cargo.toml" 2>&1 || {
    echo "FAIL: internal tests failed. Fix: read the test output above."
    exit 1
}

# 4. Conformance suite compile check
if [ -f "$CONFORM_DIR/Cargo.toml" ]; then
    echo "[4/6] cargo check -p vyre-conform"
    cargo check -p vyre-conform --manifest-path "$CONFORM_DIR/Cargo.toml" 2>&1 || {
        echo "FAIL: vyre-conform does not compile. Fix: read the compiler errors above."
        exit 1
    }

    # 5. Conformance tests
    echo "[5/6] cargo test -p vyre-conform"
    cargo test -p vyre-conform --manifest-path "$CONFORM_DIR/Cargo.toml" 2>&1 || {
        echo "FAIL: conformance tests failed. Fix: read the test output above."
        exit 1
    }
else
    echo "[4/6] SKIP: vyre-conform not found"
    echo "[5/6] SKIP: conformance tests not found"
fi

# 6. No stubs check
echo "[6/6] checking for stubs"
STUBS=$(grep -rn 'todo!\|unimplemented!\|panic!("not impl' "$VYRE_DIR/src/" 2>/dev/null || true)
if [ -n "$STUBS" ]; then
    echo "FAIL: stubs found in source:"
    echo "$STUBS"
    echo "Fix: implement fully or delete. LAW 1: no stubs."
    exit 1
fi

echo "=== PASS ==="
