#!/usr/bin/env bash
# beagle-validate: validate .bnix files against the NixOS option schema.
#
# Validates on SOURCE (.bnix), not generated .nix.
# Gives source-line precision and catches errors before emission.
#
# Usage:
#   bin/beagle-validate [files...]         # validate specific files
#   bin/beagle-validate                    # auto-discover all .bnix files
#   bin/beagle-validate --auto-fix         # apply unambiguous corrections
#
# Exits 0 if clean, 1 if errors, 2 if no files found.

set -euo pipefail
source "$(dirname "$0")/_beagle-racket"

LIB_DIR="$BEAGLE_ROOT/beagle-lib/private"

args=()
files=()
auto_fix=""
json_flag=""

for arg in "$@"; do
    case "$arg" in
        --auto-fix)
            auto_fix="--auto-fix"
            ;;
        --json)
            json_flag="--json"
            ;;
        --help|-h)
            echo "Usage: beagle-validate [--auto-fix] [--json] [files...]"
            echo ""
            echo "Validate .bnix files against the NixOS option schema."
            echo ""
            echo "  files...     .bnix files to validate (auto-discovers if omitted)"
            echo "  --auto-fix   apply unambiguous Levenshtein corrections"
            echo "  --json       emit errors as jsonl on stdout (one record per error)"
            echo ""
            echo "Exits 0 if clean, 1 if errors, 2 if no files found."
            exit 0
            ;;
        *)
            if [[ -d "$arg" ]]; then
                while IFS= read -r -d '' f; do
                    files+=("$f")
                done < <(find "$arg" -name '*.bnix' -type f -print0 2>/dev/null | sort -z)
            else
                files+=("$arg")
            fi
            ;;
    esac
done

# Auto-discover .bnix files if none specified
if [[ ${#files[@]} -eq 0 ]]; then
    while IFS= read -r -d '' f; do
        files+=("$f")
    done < <(find . -name '*.bnix' -type f -print0 2>/dev/null | sort -z)
fi

if [[ ${#files[@]} -eq 0 ]]; then
    echo "beagle-validate: no .bnix files found" >&2
    exit 2
fi

# Build the racket command
racket_args=()
if [[ -n "$auto_fix" ]]; then
    racket_args+=("$auto_fix")
fi
if [[ -n "$json_flag" ]]; then
    racket_args+=("$json_flag")
fi
for f in "${files[@]}"; do
    racket_args+=("$f")
done

exec "$RACKET" "$LIB_DIR/validate-nix.rkt" "${racket_args[@]}"
