# pyrithmic — dev shortcuts
# Usage : `just <recipe>` (run `just` to list all recipes)

# Default recipe : list available commands
default:
    @just --list

# ─── Setup ─────────────────────────────────────────────────────

# Install all dependencies + create .venv
sync:
    uv sync

# Lock dependencies without installing
lock:
    uv lock

# ─── Tests ─────────────────────────────────────────────────────

# Run unit tests only (integration / benchmark / property skipped)
test:
    uv run pytest tests/unit -q

# Run integration tests against real Rithmic (requires RITHMIC_* env vars)
test-integration:
    uv run pytest tests/integration -v -m integration

# Run all tests except benchmark / property (unit + integration)
test-all:
    uv run pytest tests/ -m "not benchmark and not property"

# Run unit tests with coverage report (terminal + HTML)
test-cov:
    uv run pytest tests/unit --cov=src/pyrithmic --cov-report=term-missing --cov-report=html

# Run codec/dispatch microbenchmarks (skipped from the default suite)
bench:
    uv run pytest tests/benchmark -m benchmark

# Run hypothesis-based property tests (Phase 7+)
test-property:
    uv run pytest tests/property -m property

# ─── Code quality ──────────────────────────────────────────────

# Run ruff linter
lint:
    uv run ruff check src/ tests/

# Auto-fix ruff issues
lint-fix:
    uv run ruff check --fix src/ tests/

# Format code with ruff formatter
fmt:
    uv run ruff format src/ tests/

# Check formatting without rewriting (mirror of the GitHub CI step)
fmt-check:
    uv run ruff format --check src/ tests/

# Run pyright strict type check
typecheck:
    uv run pyright src/

# Run all quality checks (lint + format-check + typecheck + tests)
ci: lint fmt-check typecheck test

# Same complete CI set as `ci`, but invoked through the venv directly so it runs
# where `uv` is NOT on PATH (e.g. a sandbox without uv). Keep the four gates
# byte-for-byte equivalent to `ci` — this is the COMPLETE set, never a partial
# subset. OS-specific variants below (just enables only the matching one).

# Windows: PowerShell + .venv\Scripts (no sh dependency)
[windows]
ci-venv:
    #!powershell.exe
    .venv\Scripts\ruff.exe check src/ tests/; if ($LASTEXITCODE) { exit $LASTEXITCODE }
    .venv\Scripts\ruff.exe format --check src/ tests/; if ($LASTEXITCODE) { exit $LASTEXITCODE }
    .venv\Scripts\pyright.exe src/; if ($LASTEXITCODE) { exit $LASTEXITCODE }
    .venv\Scripts\python.exe -m pytest tests/unit -q; if ($LASTEXITCODE) { exit $LASTEXITCODE }

# Unix (Linux/macOS): sh + .venv/bin
[unix]
ci-venv:
    .venv/bin/ruff check src/ tests/
    .venv/bin/ruff format --check src/ tests/
    .venv/bin/pyright src/
    .venv/bin/python -m pytest tests/unit -q

# ─── Git hooks (pre-commit) ────────────────────────────────────

# Install the pre-commit + pre-push git hooks (uses uvx — no project dep added)
hooks:
    uvx pre-commit install --install-hooks
    uvx pre-commit install --hook-type pre-push

# Run every pre-commit hook against the whole repo (manual full pass)
hooks-run:
    uvx pre-commit run --all-files

# ─── Protobuf ──────────────────────────────────────────────────

# Compile .proto files into src/pyrithmic/protocol/_generated/
proto:
    uv run python scripts/compile_protos.py

# Clean generated protobuf files
proto-clean:
    rm -rf src/pyrithmic/protocol/_generated/*.py

# ─── Reference documentation ───────────────────────────────────

# Extract Rithmic Reference_Guide.pdf to markdown (SSoT for wire protocol)
# Output: proto/source/doc/Reference_Guide.md (gitignored)
pdf-spec:
    uv run python scripts/extract_pdf_spec.py

# ─── Build / publish ───────────────────────────────────────────

# Build wheel + sdist
build:
    uv build

# Clean build artifacts
clean:
    rm -rf build/ dist/ *.egg-info/

# Publish to PyPI (requires UV_PUBLISH_TOKEN)
publish: build
    uv publish
