# justfile for rampa
# https://just.systems/

set shell := ["bash", "-uc"]

# File patterns
py_files := "find . -type f -not -path '*/\\.*' | grep -i '.*[.]py$' 2> /dev/null"
doc_files := "find . -type f -not -path '*/\\.*' | grep -i '.*[.]rst$\\|.*[.]md$\\|.*[.]css$\\|.*[.]py$\\|mkdocs\\.yml\\|CHANGES\\|README\\|TODO\\|.*conf\\.py' 2> /dev/null"
all_files := "find . -type f -not -path '*/\\.*' | grep -i '.*[.]py$\\|.*[.]rst$\\|.*[.]md$\\|.*[.]css$\\|.*[.]py$\\|mkdocs\\.yml\\|CHANGES\\|TODO\\|.*conf\\.py' 2> /dev/null"

# List all available commands
default:
    @just --list

# Run tests with pytest
[group: 'test']
test *args:
    uv run py.test {{ args }}

# Run tests then start continuous testing with pytest-watcher
[group: 'test']
start:
    just test
    uv run ptw .

# Watch files and run tests on change (requires entr)
[group: 'test']
watch-test:
    #!/usr/bin/env bash
    set -euo pipefail
    if command -v entr > /dev/null; then
        {{ all_files }} | entr -c just test
    else
        just test
        just _entr-warn
    fi

# Build documentation
[group: 'docs']
build-docs:
    just -f docs/justfile html

# Watch files and rebuild docs on change
[group: 'docs']
watch-docs:
    #!/usr/bin/env bash
    set -euo pipefail
    if command -v entr > /dev/null; then
        {{ doc_files }} | entr -c just build-docs
    else
        just build-docs
        just _entr-warn
    fi

# Serve documentation
[group: 'docs']
serve-docs:
    just -f docs/justfile serve

# Watch and serve docs simultaneously
[group: 'docs']
dev-docs:
    #!/usr/bin/env bash
    set -euo pipefail
    just watch-docs &
    just serve-docs

# Start documentation server with auto-reload
[group: 'docs']
start-docs:
    just -f docs/justfile start

# Start documentation design mode (watches static files)
[group: 'docs']
design-docs:
    just -f docs/justfile design

# Format code with ruff
[group: 'lint']
ruff-format:
    uv run ruff format .

# Run ruff linter
[group: 'lint']
ruff:
    uv run ruff check .

# Watch files and run ruff on change
[group: 'lint']
watch-ruff:
    #!/usr/bin/env bash
    set -euo pipefail
    if command -v entr > /dev/null; then
        {{ py_files }} | entr -c just ruff
    else
        just ruff
        just _entr-warn
    fi

# Run ty type checker
[group: 'lint']
ty:
    uv run ty check

# Watch files and run ty on change
[group: 'lint']
watch-ty:
    uv run ty check --watch

# Detect installed agent CLIs (Claude, Codex, Cursor, Gemini)
[group: 'mcp']
mcp-detect:
    uv run scripts/mcp_swap.py detect

# Show current MCP server entry per CLI
[group: 'mcp']
mcp-status *args:
    uv run scripts/mcp_swap.py status {{ args }}

# Rewrite CLI configs to run this checkout's MCP server
[group: 'mcp']
mcp-use-local *args:
    uv run scripts/mcp_swap.py use-local --entry rampa-mcp {{ args }}

# Restore each CLI's config from the backup written by mcp-use-local
[group: 'mcp']
mcp-revert *args:
    uv run scripts/mcp_swap.py revert {{ args }}

# Run benchmark scripts
[group: 'bench']
bench-scheduler *args:
    uv run python scripts/bench_scheduler.py {{ args }}

# Run throughput benchmark
[group: 'bench']
bench-throughput *args:
    uv run python scripts/bench_throughput.py {{ args }}

# Run metric engine benchmark
[group: 'bench']
bench-metrics *args:
    uv run python scripts/bench_metrics.py {{ args }}

# Run HTTP overhead benchmark
[group: 'bench']
bench-http *args:
    uv run python scripts/bench_http_local.py {{ args }}

[private]
_entr-warn:
    @echo "----------------------------------------------------------"
    @echo "     ! File watching functionality non-operational !      "
    @echo "                                                          "
    @echo "Install entr(1) to automatically run tasks on file change."
    @echo "See https://eradman.com/entrproject/                      "
    @echo "----------------------------------------------------------"
