#!/bin/sh
#
# Combined pre-commit hook
#
# Checks:
# 1. Poetry lock file sync with pyproject.toml
# 2. Code quality checks (Black, Ruff, mypy, pytest)
# 3. bd (beads) pre-commit flush
# 4. Any existing pre-commit.old hook
#

# Telemetry helper function
log_telemetry() {
    local event_type=$1
    local details=$2
    local events_file=".devloop/events.jsonl"

    if [ ! -d ".devloop" ]; then
        return
    fi

    # Create JSONL entry (basic implementation without Python)
    local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
    echo "{\"event_type\": \"$event_type\", \"timestamp\": \"$timestamp\", $details}" >> "$events_file"
}

# Check if pyproject.toml changed but poetry.lock didn't (only in Poetry projects)
if [ -f "poetry.lock" ]; then
    if git diff --cached --name-only | grep -q "pyproject.toml"; then
        if ! git diff --cached --name-only | grep -q "poetry.lock"; then
            echo "ERROR: pyproject.toml changed but poetry.lock not updated"
            echo "This will cause CI failure: 'poetry.lock was last generated' error"
            echo ""
            echo "Fix: Run 'poetry lock' and stage both files"
            echo "  poetry lock"
            echo "  git add poetry.lock"
            exit 1
        fi
    fi
fi

# Version is now read dynamically from pyproject.toml via importlib.metadata
# No need to check version consistency - pyproject.toml is the single source of truth

# Run type checks and tests (only if there are Python changes)
if git diff --cached --name-only | grep -qE '\.py$'; then
    if [ -x ".git/hooks/pre-commit-checks" ]; then
        ".git/hooks/pre-commit-checks"
        EXIT_CODE=$?
        
        # Log pre-commit check to telemetry
        if [ $EXIT_CODE -eq 0 ]; then
            log_telemetry "pre_commit_check" "\"success\": true, \"checks_run\": 1"
        else
            log_telemetry "pre_commit_check" "\"success\": false, \"checks_run\": 1"
            exit $EXIT_CODE
        fi
    fi
fi

# Run existing hook if present
if [ -x ".git/hooks/pre-commit.old" ]; then
    ".git/hooks/pre-commit.old" "$@"
    EXIT_CODE=$?
    if [ $EXIT_CODE -ne 0 ]; then
        exit $EXIT_CODE
    fi
fi

# Check if bd is available
if ! command -v bd >/dev/null 2>&1; then
    echo "Warning: bd command not found, skipping pre-commit flush" >&2
    exit 0
fi

# Check if we're in a bd workspace
if [ ! -d .beads ]; then
    exit 0
fi

# Flush pending changes to JSONL
if ! bd sync --flush-only >/dev/null 2>&1; then
    echo "Error: Failed to flush bd changes to JSONL" >&2
    echo "Run 'bd sync --flush-only' manually to diagnose" >&2
    exit 1
fi

# If the JSONL file was modified, stage it
if [ -f .beads/issues.jsonl ]; then
    git add .beads/issues.jsonl 2>/dev/null || true
fi

exit 0
