[private]
default:
  @just --list

# static checking of code, linting, formatting checks, spell checking etc.
check *args="src/wakepy tests/":
  uv run ruff format --check {{ args }}
  uv run mypy {{ args }}
  uv run ruff check --no-fix {{ args }}
  uv run codespell

# Format code using ruff.
format *args="src/wakepy tests/":
  uv run ruff check --fix {{ args }}
  uv run ruff format {{ args }}

# Build and serve the documentation with live-reload.
docs:
    #!/usr/bin/env python3
    import platform
    import subprocess
    import sys

    if platform.system() == "Windows":
        print("***********************************************", flush=True)
        print("*** WARNING: Full docs built only on Linux ***", flush=True)
        print("***  (Most docs are fine on Windows)       ***", flush=True)
        print("***********************************************", flush=True)

    sys.exit(subprocess.run(["uv", "run", "sphinx-autobuild", "docs/source/", "docs/build/", "-a"]).returncode)

# Run tests with coverage (pass any pytest arguments, e.g. --pdb, -k test_name)
test *args="":
    #!/usr/bin/env bash
    set -euo pipefail

    echo "Running tests with coverage..."
    if ! env -u DBUS_SESSION_BUS_ADDRESS uv run python -m pytest -W error {{ args }} --cov-branch --cov ./src --cov-fail-under=100; then
        echo "Tests failed. Generating coverage report..."
        uv run coverage html
        echo "Coverage HTML generated at htmlcov/index.html"
        echo "To see the report, run:"
        echo "  just coverage-serve"
        exit 1
    fi

    echo "Tests passed. Running static checks..."
    just check

# Run tests with coverage, show CLI report (for AI agents - no browser)
test-cli *args="":
    #!/usr/bin/env bash
    set -euo pipefail

    echo "Running tests with coverage..."
    if ! env -u DBUS_SESSION_BUS_ADDRESS uv run python -m pytest -W error {{ args }} --cov-branch --cov ./src --cov-fail-under=100; then
        echo "Tests failed. Generating coverage report in CLI..."
        uv run coverage report -m
        exit 1
    fi

    echo "Tests passed. Running static checks..."
    just check

# Serve coverage report (HTML) for browsing
coverage-serve port="8011":
    #!/usr/bin/env bash
    set -euo pipefail

    if [ ! -f "htmlcov/index.html" ]; then
        echo "htmlcov/index.html not found. Run 'uv run coverage html' first."
        exit 1
    fi

    echo "Serving coverage at http://localhost:{{ port }}/index.html"
    uv run python -m http.server -d htmlcov {{ port }}

# Build the package (sdist and wheel)
build:
    uv build
