#!/usr/bin/env bash
# 提交前做轻量检查：Python 格式 + 前端 TypeScript。
# 全量 pytest 放在 pre-push，避免每次 commit 都跑一遍。
set -euo pipefail

cd "$(git rev-parse --show-toplevel)"

if [ "${SKIP_TESTS:-0}" = "1" ]; then
    echo "[pre-commit] SKIP_TESTS=1，跳过检查"
    exit 0
fi

staged="$(git diff --cached --name-only --diff-filter=ACMR)"

python_files=()
frontend_changed=0
while IFS= read -r path; do
    [ -z "$path" ] && continue
    case "$path" in
        *.py)
            [ -f "$path" ] && python_files+=("$path")
            ;;
        web/frontend/*)
            frontend_changed=1
            ;;
    esac
done <<< "$staged"

if [ "${#python_files[@]}" -gt 0 ]; then
    echo "[pre-commit] 检查 Python 格式..."
    uv run --group dev ruff format --check "${python_files[@]}"
    echo "[pre-commit] Python 格式通过"
fi

if [ "$frontend_changed" -eq 1 ]; then
    .githooks/run-frontend-tsc.sh
fi
