#!/bin/bash
# Pre-commit hook: reject non-ASCII chars and Python 2.7 syntax violations.
# Install: ln -s ../../.githooks/pre-commit .git/hooks/pre-commit

STAGED=$(git diff --cached --name-only --diff-filter=ACM)
PYTHON_FILES=$(echo "$STAGED" | grep '\.py$' || true)

# ---- Check 1: non-ASCII characters ----
if [ -n "$STAGED" ] && echo "$STAGED" | xargs -r -I{} grep -PHn '[^\x00-\x7F]' {} 2>/dev/null; then
    echo
    echo "ERROR: non-ASCII characters found. Replace with ASCII equivalents:"
    echo "  U+2192 ->  (arrow)       ->  ->"
    echo "  U+2014 -- (em dash)      ->  --"
    echo "  U+2013 -  (en dash)      ->  -"
    echo "  U+00D7 x  (multi sign)   ->  x"
    echo "  U+2265 >= (greater-equal) ->  >="
    exit 1
fi

# ---- Check 2: Python 2.7 syntax compatibility ----
if [ -z "$PYTHON_FILES" ]; then
    exit 0
fi

_resolve_python() {
    local name="$1"
    if command -v "$name" >/dev/null 2>&1 && "$name" -c "import sys; sys.exit(0)" 2>/dev/null; then
        echo "$name"
        return 0
    fi
    # Try pyenv shims
    for d in "$HOME/.pyenv/shims" "$HOME/.pyenv/versions"/*/bin; do
        for cand in "$name" "$name".*; do
            if [ -x "$d/$cand" ] && "$d/$cand" -c "import sys; sys.exit(0)" 2>/dev/null; then
                echo "$d/$cand"
                return 0
            fi
        done
    done
    return 1
}

check_python() {
    local python_bin
    python_bin=$(_resolve_python "$1")
    local ver_label="$2"
    local errors=0

    if [ -z "$python_bin" ]; then
        echo "c2ImageD11: $ver_label not found, skipping syntax check"
        return 0
    fi

    for f in $PYTHON_FILES; do
        if ! "$python_bin" -m py_compile "$f" 2>/dev/null; then
            echo
            echo "ERROR: $f fails to compile on $ver_label:"
            "$python_bin" -m py_compile "$f" 2>&1 || true
            errors=1
        fi
    done
    return $errors
}

HAD_ERROR=0
check_python python2 "Python 2.7" || HAD_ERROR=1
check_python python3 "Python 3.x" || HAD_ERROR=1

if [ "$HAD_ERROR" -eq 1 ]; then
    echo
    echo "Python 2.7 syntax subset rules (from AGENTS.md):"
    echo "  Do NOT use: f-strings, type annotations, async/await, nonlocal,"
    echo "  yield from, {**d}, [*l], @ matrix multiply, keyword-only args."
    echo "  Use %% or .format() for string formatting."
    exit 1
fi

exit 0
