#!/usr/bin/env bash
# beagle-check: type-check a beagle file without emitting Clojure.
#
# Usage:
#   bin/beagle-check [--agent] [--json-v2] [--profile 0|1|2|3]
#                    [--module-root LOGICAL=PHYSICAL]...
#                    <source.bgl|.bclj|.bjs|.bnix>
#
# Exits 0 if the file compiles cleanly, non-zero otherwise. Stderr carries
# warnings and errors. Stdout is suppressed (no generated Clojure).
#
# --agent:     suppress lint warnings, show only type errors in a clean,
#              minimal format designed for LLM agent consumption.
# --profile N: control which type-checking features are enabled (0-3).
#              0 = parse only, 1 = basic types, 2 = structural, 3 = full (default).
#
# Useful for fast feedback in iteration — no need to write a .clj file.
# Pair with BEAGLE_ERROR_FORMAT=json for the existing V1 record, or
# --json-v2 for the authoritative typed diagnostic sibling:
#   BEAGLE_ERROR_FORMAT=json bin/beagle-check examples/demo.bclj

set -euo pipefail
BIN="$(cd "$(dirname "$0")" && pwd)"
source "$BIN/_beagle-racket"

# A semantic check is a persistent query over the exact content closure it
# reads. The shared proof cache traces that closure on a successful miss and
# revalidates every recorded input before replaying the same green result.
# Keep its bookkeeping line out of this command's intentionally empty stdout.
if [[ -z "${BEAGLE_GATE_CACHE_INNER:-}" ]]; then
    export BEAGLE_GATE_CACHE_QUIET_STATUS=1
    exec "$BIN/_gate-cache-run" \
        --domain semantic-check --id single-source -- "$0" "$@"
fi

# Parse flags
AGENT=false
JSON_V2=false
PROFILE=""
FILE=""
MODULE_ROOT_ARGS=()
while [[ $# -gt 0 ]]; do
    case "$1" in
        --agent) AGENT=true; shift ;;
        --json-v2) JSON_V2=true; shift ;;
        --profile)
            if [[ $# -lt 2 ]]; then
                echo "beagle-check: --profile requires an argument (0, 1, 2, or 3)" >&2
                exit 2
            fi
            PROFILE="$2"; shift 2 ;;
        --module-root)
            if [[ $# -lt 2 ]]; then
                echo "beagle-check: --module-root requires LOGICAL_PREFIX=PHYSICAL_DIRECTORY" >&2
                exit 2
            fi
            MODULE_ROOT_ARGS+=(--module-root "$2"); shift 2 ;;
        --module-root=*) MODULE_ROOT_ARGS+=("$1"); shift ;;
        -*) echo "beagle-check: unknown option: $1" >&2; exit 2 ;;
        *)
            if [[ -n "$FILE" ]]; then
                echo "beagle-check: expected exactly one source file" >&2
                exit 2
            fi
            FILE="$1"; shift ;;
    esac
done

if [[ -z "$FILE" ]]; then
    echo "usage: beagle-check [--agent] [--json-v2] [--profile 0|1|2|3] [--module-root LOGICAL=PHYSICAL]... <source.bgl|.bclj|.bjs|.bnix>" >&2
    exit 2
fi

EXTRA_ARGS=()
if [[ "$AGENT" == true ]]; then
    export BEAGLE_AGENT_MODE=1
fi
if [[ "$JSON_V2" == true ]]; then
    EXTRA_ARGS+=(--json-v2)
fi
if [[ -n "$PROFILE" ]]; then
    EXTRA_ARGS+=(--profile "$PROFILE")
fi
"$RACKET" -e "(require beagle/private/check-all) (run-check-all (vector->list (current-command-line-arguments)))" -- "${EXTRA_ARGS[@]}" "${MODULE_ROOT_ARGS[@]}" "$FILE"
