#!/usr/bin/env just --justfile
#
# Language-specific recipes for the Python packages under py/. Invoked
# from the repo root as `just py <recipe>` via the `mod py` import.
#
# Two workspace members:
#   moq-ffi/  maturin project: rs/moq-ffi cdylib + uniffi bindings (dist `moq-ffi`)
#   moq-rs/   pure-python ergonomic wrapper depending on moq-ffi (dist `moq-rs`,
#             import `moq`)

set working-directory := '.'

default:
    just check

# Build moq-ffi (maturin cdylib + bindings) and install the pure-python moq-rs
# wrapper, both editable into the workspace venv. `--no-deps` on the wrapper

# keeps uv from fetching moq-ffi off PyPI; maturin just installed it locally.
_develop:
    cd moq-ffi && uv run --no-sync maturin develop --uv
    uv pip install --no-deps -e moq-rs

# Lint + format + editable build + pyright. `--no-install-workspace` installs
# the root dev group (ruff, maturin, pyright, pytest) without trying to

# pip-build the workspace members; `_develop` then installs them editable.
check:
    uv sync --no-install-workspace
    uv run --no-sync ruff check .
    uv run --no-sync ruff format --check .
    just _develop
    uv run --no-sync pyright

fix:
    uv sync --no-install-workspace
    uv run --no-sync ruff check --fix .
    uv run --no-sync ruff format .

test:
    uv sync --no-install-workspace
    just _develop
    uv run --no-sync pytest moq-rs/tests/ moq-ffi/tests/

# Local dev build: editable install of moq-ffi (with the cdylib + uniffi

# bindings) and the moq-rs wrapper into the workspace venv.
build:
    uv sync --no-install-workspace
    just _develop

# Remove the virtualenv, release dist, bytecode caches, and the uniffi
# bindings maturin drops in during editable installs. The uv workspace venv

# lives at the repo root, so reach up for it.
clean:
    #!/usr/bin/env bash
    set -euo pipefail
    rm -rf dist moq-ffi/moq_ffi/_uniffi
    rm -rf ../.venv .venv
    find . -name .claude -prune -o -type d -name __pycache__ -prune -exec rm -rf {} +
    find . -name .claude -prune -o -type f -name '*.pyc' -exec rm -f {} +

# Build the pure-python moq-rs wrapper sdist + wheel into py/dist (for release).
# moq-ffi is built separately by maturin (see release-py.yml); the wrapper is

# pure python so it needs no compilation, just a metadata-correct wheel.
package:
    rm -rf dist
    uv build --package moq-rs --out-dir dist

# Build the Sphinx API docs for the `moq` wrapper into py/dist/docs. Read the
# Docs builds the same config (see .readthedocs.yaml) and hosts the result. The
# native moq_ffi is mocked (autodoc_mock_imports in docs/conf.py), so this needs
# no Rust build: install the pure-python wrapper metadata and render.
docs:
    uv sync --no-install-workspace
    uv pip install --no-deps -e moq-rs
    rm -rf dist/docs
    uv run --no-sync --with sphinx --with myst-parser --with furo \
        sphinx-build -b html moq-rs/docs dist/docs

# Full Python CI: lint + tests + build. Takes a newline-separated list
# of changed files; skips if FILES is non-empty and none match the
# Python scope (which includes rs/moq-ffi because moq-ffi bundles it via

# maturin). Run `just py ci` (no FILES) to force-run everything.
ci $FILES="":
    #!/usr/bin/env bash
    set -euo pipefail
    if [[ -n "$FILES" ]] && ! grep -qE '^(py/|pyproject\.toml$|uv\.lock$|rs/moq-ffi/)' <<< "$FILES"; then
    	echo "py: no Python changes; skipping."
    	exit 0
    fi
    just check
    just test
    just build
    just docs
