maturin := "maturin"

# Print available recipes
help:
  @just --unsorted --list

# Prepare data files in lib/edge/data
prepare-data:
  @curl -s http://localhost:6333 > /dev/null || echo "Please start qdrant on localhost:6333"
  tools/prepare_facet_snapshot.sh

# Check everything, run all examples
check:
  just rs-check
  just rs-examples-build
  just rs-examples
  just py-build
  just py-examples

# 🦀 Run Rust checks
[working-directory: 'publish']
rs-check:
  ./amalgamate.py
  cargo +nightly fmt -p examples -- --check
  @# --no-deps because there are lot of warnings in auto-generated `qdrant-edge` package
  ./cargo clippy -p examples --all-targets --no-deps -- -D warnings

# 🦀 Build Rust examples (optional)
[working-directory: 'publish']
rs-examples-build:
  ./amalgamate.py
  ./cargo build -p examples

# 🦀 Run Rust examples; Specify example with `-e demo`
[working-directory: 'publish', script]
[positional-arguments, arg('e', short = 'e')]
rs-examples e="":
  EXAMPLES=${1:-$(just rs-examples-list)}
  FAILURES=""
  for fname in $EXAMPLES; do
    echo "::group::Running example: $fname"
    python ./cargo run -p examples --bin "$fname" \
      || FAILURES="$FAILURES $fname"
    echo "::endgroup::"
  done

  if [ "$FAILURES" ]; then
    echo "::error::Failed examples:$FAILURES"
    exit 1
  fi

# 🦀 List Rust examples
[working-directory: 'publish', script]
rs-examples-list:
  cargo metadata --format-version 1 --no-deps | \
    jq -r '.packages[] | select(.name == "examples").targets[] | select(.kind[] == "bin").name'

# 🐍 Build qdrant-edge-py
[working-directory: 'python', script]
py-build:
  unset VIRTUAL_ENV
  uv venv --allow-existing
  uv pip install requests
  uvx {{maturin}} develop --no-default-features --features abi3

# 🐍 Generate python/qdrant_edge.pyi
[working-directory: 'python', script]
py-stubs:
  STUBS=$(mktemp -d --suffix=.qdrant-edge-stubs)
  trap "rm -rf ${STUBS}" EXIT

  uvx {{maturin}} generate-stubs --no-default-features --features abi3 -o "$STUBS"
  {
    echo '# This file is auto-generated by `just py-stubs`'
    echo "from __future__ import annotations"
    echo "import typing"
    echo "import uuid"
    echo "from typing import TypeAlias"
    cat "${STUBS}/qdrant_edge/__init__.pyi"
    cargo run --quiet --bin gen_pyi
  } > "$STUBS/out.pyi"
  uvx black --quiet "$STUBS/out.pyi"
  mv "$STUBS/out.pyi" qdrant_edge.pyi

# 🐍 Build Python API docs
[working-directory: 'python', script]
py-docs:
  rm -rf docs
  mkdir -p "docs/src" "docs/docs"
  cp mkdocs.yml "docs/mkdocs.yml"
  cp qdrant_edge.pyi "docs/src/qdrant_edge.py"

  # One page per class
  CLASSES=$(sed -n 's/^class \([A-Za-z_][A-Za-z_0-9]*\).*/\1/p' qdrant_edge.pyi)
  for NAME in $CLASSES; do
    echo "::: qdrant_edge.$NAME" > "docs/docs/$NAME.md"
  done

  {
    echo '# Qdrant Edge'
    echo 
    echo '## Type aliases'
    printf "::: qdrant_edge.%s\n" $(sed -n 's/: TypeAlias .*//p' qdrant_edge.pyi)
  } > "docs/docs/index.md"

  uv run --no-project --with mkdocs --with 'mkdocstrings[python]' --with mkdocs-material --with black \
    mkdocs build --strict --quiet --config-file docs/mkdocs.yml
  echo "docs/site/index.html"

# 🐍 Run Python examples; Specify example with `-e examples/demo.py`
[working-directory: 'python', script]
[positional-arguments, arg('e', short = 'e')]
py-examples e="":
  unset VIRTUAL_ENV
  EXAMPLES=${1:-$(just py-examples-list)}
  FAILURES=""
  for fname in $EXAMPLES; do
    echo "::group::Running example: $fname"
    uv run --no-project "$fname" \
      || FAILURES="$FAILURES $fname"
    echo "::endgroup::"
  done
  if [ "$FAILURES" ]; then
    echo "::error::Failed examples:$FAILURES"
    exit 1
  fi

# 🐍 List Python examples
[working-directory: 'python', script]
py-examples-list:
  find examples -name "*.py" -not -name "__init__.py"
