#!/usr/bin/env bash
set -euo pipefail

CORES=$(getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)
CI_PARITY_XDIST_WORKERS=2
DEFAULT_WORKERS=$CI_PARITY_XDIST_WORKERS
PYTEST_XDIST_WORKERS="${PYTEST_XDIST_WORKERS:-$DEFAULT_WORKERS}"
PYTEST_FAULTHANDLER_TIMEOUT="${PYTEST_FAULTHANDLER_TIMEOUT:-60}"
RUN_INTEGRATION=0
QUICK_MODE=0
TARGET_ARGS=()
USE_VENV=0
VERIFY_PHASE_CMD=()
RUFF_TARGETS=(src/gza/ tests/test_main_integration_verify.py)
VENV_BIN=""

usage() {
    echo "Usage: $0 [--quick [-- <pytest-target> ...]] [-i|--integration]" >&2
    exit 2
}

while [[ "$#" -gt 0 ]]; do
    case "$1" in
        --quick)
            QUICK_MODE=1
            ;;
        -i|--integration)
            RUN_INTEGRATION=1
            ;;
        --)
            shift
            TARGET_ARGS=("$@")
            break
            ;;
        *)
            usage
            ;;
    esac
    shift
done

if [[ -d ".venv/bin" ]]; then
    VENV_BIN="$(pwd)/.venv/bin"
    export PATH="${VENV_BIN}:$PATH"
fi

if [[ -x "${VENV_BIN}/python" ]]; then
    USE_VENV=1
    VERIFY_PHASE_CMD=("${VENV_BIN}/python" -m gza.tools.verify_phase)
else
    VERIFY_PHASE_CMD=(uv run python -m gza.tools.verify_phase)
fi

resolve_tool_cmd() {
    local tool_name="$1"
    shift
    if [[ -n "$VENV_BIN" && -x "${VENV_BIN}/${tool_name}" ]]; then
        printf '%s\0' "${VENV_BIN}/${tool_name}" "$@"
        return
    fi
    printf '%s\0' uv run "$tool_name" "$@"
}

resolve_python_module_cmd() {
    local module_name="$1"
    shift
    if [[ -n "$VENV_BIN" && -x "${VENV_BIN}/python" ]]; then
        printf '%s\0' "${VENV_BIN}/python" -m "$module_name" "$@"
        return
    fi
    printf '%s\0' uv run python -m "$module_name" "$@"
}

run_resolved_phase() {
    local phase_name="$1"
    shift
    local -a command=()
    while IFS= read -r -d '' entry; do
        command+=("$entry")
    done < <("$@")
    run_phase "$phase_name" "${command[@]}"
}

run_phase() {
    local phase_name="$1"
    shift
    "${VERIFY_PHASE_CMD[@]}" "${phase_name}" -- "$@"
}

echo "verify: mode=$([[ "$QUICK_MODE" -eq 1 ]] && echo quick || echo full) cores=$CORES xdist_workers=$PYTEST_XDIST_WORKERS faulthandler_timeout=${PYTEST_FAULTHANDLER_TIMEOUT}s use_venv=$USE_VENV"

if [[ "$QUICK_MODE" -eq 1 ]]; then
    if [[ "$RUN_INTEGRATION" -eq 1 ]]; then
        echo "Usage: --integration is not supported with --quick" >&2
        exit 2
    fi

    run_resolved_phase ruff resolve_tool_cmd ruff check "${RUFF_TARGETS[@]}"
    run_resolved_phase checks resolve_python_module_cmd checks
    if [[ "${#TARGET_ARGS[@]}" -gt 0 ]]; then
        run_resolved_phase targeted_pytest resolve_tool_cmd pytest "${TARGET_ARGS[@]}" -x -o "faulthandler_timeout=${PYTEST_FAULTHANDLER_TIMEOUT}"
    else
        echo "quick verify: no targeted pytest args supplied; skipping pytest phase"
    fi
    exit 0
fi

if [[ "${#TARGET_ARGS[@]}" -gt 0 ]]; then
    usage
fi

run_resolved_phase ruff resolve_tool_cmd ruff check "${RUFF_TARGETS[@]}"
run_resolved_phase ty resolve_tool_cmd ty check src/gza/
run_resolved_phase mypy resolve_tool_cmd mypy src/gza/
run_resolved_phase checks resolve_python_module_cmd checks
run_phase unit ./bin/test-unit --summary -- tests/ -n "$PYTEST_XDIST_WORKERS" --dist loadscope --durations=25 -o "faulthandler_timeout=${PYTEST_FAULTHANDLER_TIMEOUT}"
run_resolved_phase functional resolve_python_module_cmd gza.test_functional_rerun --summary -- tests_functional/ -n "$PYTEST_XDIST_WORKERS" --dist loadscope --durations=25 -o "faulthandler_timeout=${PYTEST_FAULTHANDLER_TIMEOUT}"

if [[ "$RUN_INTEGRATION" -eq 1 ]]; then
    run_resolved_phase integration resolve_tool_cmd pytest tests_integration -xv
fi
