#!/usr/bin/env bash
set -u

status=0

fail() {
  status=1
  printf '[FAIL] %s\n' "$1"
}

pass() {
  printf '[PASS] %s\n' "$1"
}

skip() {
  printf '[SKIP] %s\n' "$1"
}

run_required() {
  local label="$1"
  shift

  if "$@"; then
    pass "$label"
  else
    fail "$label"
  fi
}

repo_root="$(git rev-parse --show-toplevel 2>/dev/null)" || {
  printf '[FAIL] git worktree detection\n' >&2
  exit 1
}

if [ "$PWD" != "$repo_root" ]; then
  printf '[FAIL] bin/lint must run from the repo root: %s\n' "$repo_root" >&2
  exit 1
fi

run_tracked_text_checks() {
  if ! python3 "$repo_root/tools/check-tracked-text" "$repo_root"; then
    status=1
  fi
}

has_tracked_files() {
  git ls-files -- "$1" | grep -q .
}

run_tracked_text_checks
capture_checker='tools/check-source-capture-receipts.py'
capture_manifest='testdata/sources/capture-receipts.json'
if [ -f "$capture_checker" ] && [ -f "$capture_manifest" ]; then
  run_required 'Source capture and fixture-key contract' \
    python3 "$capture_checker" --root testdata/sources
elif [ ! -f "$capture_checker" ] && [ ! -f "$capture_manifest" ]; then
  skip 'Source capture and fixture-key contract (not configured)'
else
  fail 'Source capture and fixture-key contract (incomplete configuration; checker and manifest are both required)'
fi
if [ -f scripts/check-mcp-registry-server.sh ]; then
  run_required 'MCP registry metadata check' bash scripts/check-mcp-registry-server.sh
else
  skip 'MCP registry metadata check (not configured)'
fi

if has_tracked_files '*.py'; then
  if command -v ruff >/dev/null 2>&1 && ruff --version | grep -Fxq 'ruff 0.13.2'; then
    run_required 'Python lint (ruff check .)' ruff check .
  else
    fail 'Python lint (Ruff 0.13.2 required)'
  fi
else
  skip 'Python lint (no tracked Python files)'
fi

if [ -f tools/check-shell-workflows ]; then
  run_required 'Bash and workflow lint' python3 tools/check-shell-workflows
else
  skip 'Bash and workflow lint (checker not configured)'
fi

if [ -f build.zig ] || has_tracked_files '*.zig'; then
  run_required 'Zig lint (zig fmt --check .)' zig fmt --check .
else
  skip 'Zig lint (not detected)'
fi

if [ -f Cargo.toml ]; then
  read -r -a routine_cargo_features <<< "${ROUTINE_CARGO_FEATURES:---no-default-features}"
  run_required 'Rust lint (cargo fmt --check)' cargo fmt --check
  run_required 'Rust lint (cargo clippy -- -D warnings)' tools/with-build-identity cargo clippy "${routine_cargo_features[@]}" -- -D warnings
  # Ask cargo, not PATH. Cargo finds its own subcommands in ~/.cargo/bin
  # whatever PATH says, so `command -v cargo-deny` reports missing on any
  # caller with a trimmed PATH — which is how the factory runs us.
  if cargo deny --version >/dev/null 2>&1; then
    run_required 'Rust license lint (cargo deny check licenses)' cargo deny check licenses
    run_required 'Rust advisory lint (cargo deny check advisories)' cargo deny check advisories
  else
    printf 'Install cargo-deny with: cargo install cargo-deny --locked\n'
    fail 'Rust license lint (cargo-deny missing)'
  fi
else
  skip 'Rust lint (not detected)'
fi

if [ -f package.json ]; then
  run_required 'JS/TS lint (npm run lint --if-present)' npm run lint --if-present
else
  skip 'JS/TS lint (not detected)'
fi

exit "$status"
