#!/usr/bin/env bash
# ============================================================================
# ARQERA Pre-Push Hook
# ============================================================================
# This hook runs static validation before pushing to remote.
# Full validation (including DB-dependent checks) happens in CI.
#
# Skip with: ARQERA_SKIP_PUSH_GATES=1 git push
# ============================================================================

set -euo pipefail

REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

# Sync private notes (if configured)
"$REPO_DIR/scripts/sync-private-notes.sh" 2>/dev/null || true

if [ "${ARQERA_SKIP_PUSH_GATES:-0}" = "1" ]; then
    echo "ARQERA pre-push gates skipped."
    exit 0
fi

echo "=============================================="
echo "ARQERA Pre-Push Validation"
echo "=============================================="

# Static validation only (no DB required)
echo "Running PostgreSQL compatibility check..."
if [ -f "$REPO_DIR/scripts/postgres-compat-gate.py" ]; then
    python3 "$REPO_DIR/scripts/postgres-compat-gate.py" || {
        echo "PostgreSQL compatibility check failed."
        exit 1
    }
else
    echo "postgres-compat-gate.py not found, skipping..."
fi

# Note: inventory-gate.sh and system-graph-gate.sh require DB access
# These are now run in CI against cloud infrastructure instead
if [ "${ARQERA_RUN_DB_GATES:-0}" = "1" ]; then
    echo "Running DB-dependent gates (ARQERA_RUN_DB_GATES=1)..."
    bash "$REPO_DIR/scripts/inventory-gate.sh"
    bash "$REPO_DIR/scripts/system-graph-gate.sh"
else
    echo "Skipping DB-dependent gates (run in CI instead)"
    echo "  - inventory-gate.sh"
    echo "  - system-graph-gate.sh"
fi

echo "=============================================="
echo "Pre-push checks passed. CI will run full validation."
echo "=============================================="
