#!/usr/bin/env bash
set -euo pipefail

# Unset git env vars that leak into test subprocesses during commit.
# GIT_INDEX_FILE points to a temp file during commit, breaking workspace
# tests that spawn their own git repos in temp directories.
unset GIT_INDEX_FILE GIT_DIR GIT_WORK_TREE 2>/dev/null || true

echo "=== pre-commit: fmt ==="
cargo fmt --all -- --check

echo "=== pre-commit: clippy ==="
cargo clippy --workspace --all-targets -- -D warnings

echo "=== pre-commit: test ==="
# Live Postgres tests require a configured Harness database URL.
# Run them when available; otherwise skip only the known live-Postgres lib tests
# and let CI or a configured local database provide full DB coverage.
if [ -n "${HARNESS_DATABASE_URL:-}" ]; then
    cargo test --workspace --lib
else
    echo "HARNESS_DATABASE_URL not set — skipping live Postgres lib tests"
    cargo test --workspace --lib -- --skip db::tests
fi

echo "=== pre-commit: all checks passed ==="
