set shell := ["bash", "-eu", "-o", "pipefail", "-c"]


schema_path := "../server/schema.yml"

# Show available recipes.
default:
    @just --list

# Install development dependencies.
buildtools:
    uv pip install -e ".[dev]"

# Generate the server schema.
schema:
    cd ../server/ && direnv exec . just schema
    echo "Schema generated at {{ schema_path }}"

# Generate the OpenAPI client code.
codegen: schema
    echo "Generating OpenAPI client code from schema at {{ schema_path }}"
    uv run openapi-python-client generate --output-path src/ --overwrite --path {{ schema_path }} --config openapi_python_client.yaml

# Show how to run the Django server for live tests.
run-api:
    @echo "Run these commands"
    @echo "pushd ../server/"
    @echo "just runserver &"
    @echo "popd"

# Remove distribution build artifacts.
clean-dist:
    rm -rf dist/ build/ *.egg-info src/*.egg-info

# Build distribution packages.
# llm: uv build replaces `python -m build`; it builds in an isolated env, so no buildtools install is needed.
build: clean-dist
    uv build

# Upload to Test PyPI.
# llm: uv publish replaces twine (twine <6.1 cannot read hatchling's Metadata-Version 2.4).
# llm: uv publish does not read ~/.pypirc; set UV_PUBLISH_TOKEN to a Test PyPI token (or pass --token).
pypi-test: build
    @echo "Uploading to Test PyPI..."
    # llm: --publish-url is required, otherwise uv publish targets production PyPI.
    uv publish --publish-url https://test.pypi.org/legacy/ --check-url https://test.pypi.org/simple/ --token "$PYPI_TEST_TOKEN" dist/*
    @echo "Test with: pip install --index-url https://test.pypi.org/simple/ phisaver-client"

# Upload to production PyPI.
# llm: set UV_PUBLISH_TOKEN to a production PyPI token (or pass --token).
pypi: build
    @echo "WARNING: Publishing to PRODUCTION PyPI!"
    @echo "Version: $(grep '^version' pyproject.toml)"
    @read -p "Continue? [y/N] " confirm && [ "$confirm" = "y" ]
    uv publish --token "$PYPI_TOKEN" dist/*
    @echo "Published! Install with: pip install phisaver-client"

# Show the current package version.
check-version:
    @grep '^version' pyproject.toml

api-get-token:
    #!/usr/bin/env bash
    set -eu -o pipefail
    : "${PS_API_URL:?PS_API_URL is not set}"
    : "${PS_API_USERNAME:?PS_API_USERNAME is not set}"
    : "${PS_API_PASSWORD:?PS_API_PASSWORD is not set; run 'direnv allow && direnv reload'}"
    response="$(http --check-status --ignore-stdin --body --no-verify POST "${PS_API_URL%/}/api/v2/token/" \
        username="$PS_API_USERNAME" password="$PS_API_PASSWORD")"
    python3 -c "import json, sys; print(json.loads(sys.stdin.read())['token'])" <<< "$response"

test:
    cd ../testing && just client-test

test-live:
    PHISAVER_RUN_LIVE=1 uv run pytest -m live 
