#!/bin/bash

# Pre-push hook: Re-run the bounded contract smoke suite without coverage.
# The complete unit/integration suite belongs to the release gate.

set -e

echo "🧪 Running bounded pre-push contract smoke suite..."
echo ""

PROJECT_PYTHON=".venv/bin/python"

ensure_test_env() {
    if [ ! -x "$PROJECT_PYTHON" ] || ! "$PROJECT_PYTHON" -c "import pytest" >/dev/null 2>&1; then
        echo "🔧 Syncing project test environment..."
        uv sync --extra dev --frozen
    fi
}

export PYTHONPATH="src:$PYTHONPATH"

PYTHON_TMPDIR=$(python3 -c "import tempfile, os; print(os.path.dirname(tempfile.gettempdir()))")
export GIT_CEILING_DIRECTORIES="$PYTHON_TMPDIR"

unset GIT_DIR
unset GIT_WORK_TREE
unset GIT_INDEX_FILE
unset GIT_OBJECT_DIRECTORY
unset GIT_ALTERNATE_OBJECT_DIRECTORIES

echo "🔒 Git isolation enabled: GIT_CEILING_DIRECTORIES=$GIT_CEILING_DIRECTORIES"
TEST_FILES=(
    "tests/unit/test_abstract_cli.py"
    "tests/unit/test_delta_packet.py"
    "tests/unit/test_initial_input_providers.py"
    "tests/unit/test_issue_yaml_config.py"
    "tests/unit/test_playbook_loader.py"
    "tests/unit/test_prepare_fields_loader.py"
    "tests/unit/test_session_continuation.py"
    "tests/unit/test_skill_loader.py"
    "tests/unit/test_status_codes.py"
    "tests/unit/test_workflow_models.py"
    "tests/unit/utils/test_checklist_validator.py"
)

echo "Running ${#TEST_FILES[@]} bounded contract smoke files..."
ensure_test_env
"$PROJECT_PYTHON" -m pytest "${TEST_FILES[@]}" -q --tb=short --no-cov -m "not slow"

echo ""
echo "✅ Pre-push contract smoke suite passed! Proceeding with push..."
