#!/usr/bin/env bash
# Minimal CI check: run locally (pre-push hook) or in Gitea Actions.
# Usage: scripts/ci-check.sh [--fast]   (--fast skips frontend type check)
set -euo pipefail
cd "$(dirname "$0")/.."

FAST=0
[[ "${1:-}" == "--fast" ]] && FAST=1

if [[ -f .venv/bin/activate ]]; then
    # shellcheck disable=SC1091
    source .venv/bin/activate
fi

echo "==> 1/4 版本号一致性 (pyproject ↔ package.json)"
python scripts/sync_version.py --check

echo "==> 2/4 ruff lint"
if command -v ruff >/dev/null 2>&1; then
    ruff check backend/
else
    echo "    ruff 未安装，跳过（pip install -e '.[dev]'）"
fi

echo "==> 3/4 pytest"
# Tests use mocked HTTP boundaries and should not inherit a developer's local
# SOCKS/HTTP proxy. Otherwise httpx may require an optional socksio package
# merely while importing the reverse-proxy router.
env -u ALL_PROXY -u all_proxy -u HTTP_PROXY -u http_proxy -u HTTPS_PROXY -u https_proxy \
    python -m pytest -q

if [[ $FAST -eq 1 ]]; then
    echo "==> 4/4 前端类型检查（--fast 跳过）"
else
    echo "==> 4/4 前端类型检查 (tsc --noEmit)"
    if [[ -d frontend/web/node_modules ]]; then
        (cd frontend/web && npx tsc --noEmit)
    else
        echo "    node_modules 不存在，跳过（cd frontend/web && npm install）"
    fi
fi

echo "✅ CI 检查全部通过"
