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 }}"

# Set this package's version to the server's (SSOT: server/pyproject.toml). uv reads and writes the files.
# llm: read with --dry-run --no-sync (--frozen would report the stale version in uv.lock);
# llm: write with --frozen (edits pyproject.toml only: no re-lock, no sync)
sync-version:
    uv version "$(cd ../server && uv version --short --dry-run --no-sync)" --frozen

# Generate the OpenAPI client code.
codegen: schema sync-version
    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


# 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

# Get JWT access token for the test stack API.
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'}"
    # llm: JWT sign-in: email + password -> {"access": <15 min>, "refresh": <60 days>}. Print the access token.
    response="$(http --check-status --ignore-stdin --body --no-verify POST "${PS_API_URL%/}/api/v2/token/" \
        email="$PS_API_USERNAME" password="$PS_API_PASSWORD")"
    python3 -c "import json, sys; print(json.loads(sys.stdin.read())['access'])" <<< "$response"

# Unit tests: no server, no credentials.
test:
    cd ../testing && just client-test

# Integration tests: the test stack API (PS_API_URL) with the fake credentials of testing/test.env.
# Start it first: `just test-server-infra-up` (repo root) and run the server on :8001.
test-integration *args:
    cd ../testing && just client-integration {{ args }}

# Live tests: a real service and real credentials from the host environment (never testing/test.env).
test-live:
    PHISAVER_RUN_LIVE=1 uv run pytest -m live 
