DEFAULT_ENV := 'base'
DEFAULT_TOOL := 'pi'
GIT_REMOTE := 'origin'
# release tags look like ai-container/<version>
TAG_PREFIX := 'ai-container/'
# keychain service for the PyPI token, stored under the fixed user '__token__'
PYPI_URL := 'https://upload.pypi.org/legacy/'

alias bi := build-image
alias rb := build-image
alias il := image-list
alias el := env-list
alias sh := shell
alias ag := agent
alias dry := agent-dryrun

# list all available recipes
default:
    just --list

[private]
require-podman:
    #!/usr/bin/env bash
    set -euo pipefail
    if ! command -v podman >/dev/null; then
        echo "error: podman is not installed -- see https://podman.io/docs/installation" >&2
        exit 1
    fi
    if ! uv run python -c "import sys; from podman import PodmanClient; sys.exit(not PodmanClient().ping())" 2>/dev/null; then
        echo "error: the podman service socket is not reachable" >&2
        echo "       start it with: systemctl --user enable --now podman.socket" >&2
        exit 1
    fi

# (re)build one or more images, parents first (e.g. just build-image pi pi-python)
[group: 'image']
build-image *NAMES=DEFAULT_ENV: require-podman
    uv run ai image rebuild {{ NAMES }}

# list the packaged images and whether they are built
[group: 'image']
image-list: require-podman
    uv run ai image list

# list custom environments and whether their images are built
[group: 'image']
env-list:
    uv run ai env list

# usage: just shell                 -> base env, current directory
#        just shell . rust          -> rust env
# open an interactive shell in the container with PATH mounted
[group: 'container']
shell PATH='.' ENV=DEFAULT_ENV: require-podman
    uv run ai shell -e {{ ENV }} {{ PATH }}

# usage: just agent                         -> pi on the current directory
#        just agent claude . base --verbose  -> extra args go to the tool
# run an agent/tool against PATH (tools: pi, opc, claude, aic, llm)
[group: 'container']
agent TOOL=DEFAULT_TOOL PATH='.' ENV=DEFAULT_ENV *ARGS: require-podman
    uv run ai agent -e {{ ENV }} {{ TOOL }} {{ PATH }} -- {{ ARGS }}

# print the podman commands an agent run would execute, without running them
[group: 'container']
agent-dryrun TOOL=DEFAULT_TOOL PATH='.' ENV=DEFAULT_ENV *ARGS: require-podman
    uv run ai --dryrun agent -e {{ ENV }} {{ TOOL }} {{ PATH }} -- {{ ARGS }}

# lint and check formatting and types (what CI should run)
[group: 'check']
check: lint format-check typecheck

# lint with ruff
[group: 'check']
lint *ARGS:
    uv run ruff check . {{ ARGS }}

# check formatting with ruff, without changing files
[group: 'check']
format-check:
    uv run ruff format --check .

# usage: just fix                   -> apply safe fixes
#        just fix --unsafe-fixes    -> also apply unsafe fixes
# apply ruff lint fixes
[group: 'check']
fix *ARGS:
    uv run ruff check --fix . {{ ARGS }}

# format with ruff and apply safe lint fixes
[group: 'check']
fmt: fix
    uv run ruff format .

# type check with ty
[group: 'check']
typecheck *ARGS:
    uv run ty check {{ ARGS }}

# build the sdist and wheel into dist/
[group: 'build']
build:
    uv build

# install the CLI as a uv tool from this checkout
[group: 'build']
install:
    uv tool install --force --reinstall .

# remove build artifacts and caches
[group: 'build']
clean:
    rm -rf dist .ruff_cache
    find . -type d -name __pycache__ -not -path './.venv/*' -exec rm -rf {} +

[private]
require-clean:
    #!/usr/bin/env bash
    set -euo pipefail
    if ! git diff --quiet || ! git diff --cached --quiet; then
        echo "error: the working tree has uncommitted changes -- commit or stash them first" >&2
        exit 1
    fi

[private]
require-new-tag VERSION:
    #!/usr/bin/env bash
    set -euo pipefail
    if git rev-parse --quiet --verify "refs/tags/{{ TAG_PREFIX }}{{ VERSION }}" >/dev/null; then
        echo "error: tag '{{ TAG_PREFIX }}{{ VERSION }}' already exists" >&2
        exit 1
    fi

[private]
require-token:
    #!/usr/bin/env bash
    set -euo pipefail
    if [ -n "${UV_PUBLISH_TOKEN:-}" ]; then
        exit 0
    fi
    if ! uv run keyring get {{ PYPI_URL }} __token__ >/dev/null 2>&1; then
        echo "error: no PyPI token in the keychain and UV_PUBLISH_TOKEN is not set" >&2
        echo "       store it with: just token-store" >&2
        exit 1
    fi

# usage: just release 0.19.0
# bump the version, then commit, tag, push and publish it to PyPI
[group: 'release']
release VERSION: require-token (bump VERSION) (push VERSION) publish

# set the version with uv, then commit and tag it
[group: 'release']
bump VERSION: require-clean (require-new-tag VERSION)
    uv version {{ VERSION }}
    git add pyproject.toml uv.lock
    git commit -m "Bump ai-container version to {{ VERSION }}"
    git tag -a {{ TAG_PREFIX }}{{ VERSION }} -m "Release ai-container {{ VERSION }}"

# push the current branch and the release tag to the central repo
[group: 'release']
push VERSION:
    git push --atomic {{ GIT_REMOTE }} HEAD refs/tags/{{ TAG_PREFIX }}{{ VERSION }}

# clean dist/, build the package and publish it (token: UV_PUBLISH_TOKEN, else keychain)
[group: 'release']
publish: require-token
    #!/usr/bin/env bash
    set -euo pipefail
    export UV_PUBLISH_TOKEN="${UV_PUBLISH_TOKEN:-$(uv run keyring get {{ PYPI_URL }} __token__)}"
    rm -rf dist
    uv build
    uv publish

# store the PyPI token in the local keychain (prompts, input is hidden)
[group: 'release']
token-store:
    uv run keyring set {{ PYPI_URL }} __token__

# remove the PyPI token from the local keychain
[group: 'release']
token-remove:
    uv run keyring del {{ PYPI_URL }} __token__
