# Kali MCP Server — Just Commands
set dotenv-load

# Show available commands
default:
    @just --list

# Install dependencies (editable install of the kali_mcp package + deps)
install:
    pip install -e .

# Start the server (foreground)
start:
    python -m kali_mcp

# Start in debug mode
debug:
    MCP_DEBUG=true python -m kali_mcp

# Run the full pytest suite (unit + integration)
test:
    .venv/bin/python -m pytest

# Run only fast unit tests
test-unit:
    .venv/bin/python -m pytest tests/unit

# Run integration tests (uses local subprocesses; no external binaries needed except coreutils)
test-integration:
    .venv/bin/python -m pytest tests/integration

# Run end-to-end protocol tests (spawns a real server)
test-e2e:
    .venv/bin/python -m pytest tests/e2e -m e2e

# Show server health (requires running server)
health:
    @curl -sf http://127.0.0.1:8399/sse -o /dev/null && echo "Server running" || echo "Server not running"

# View logs
logs:
    tail -f logs/server.log

# View execution logs
exec-logs:
    tail -f logs/executions.jsonl

# Clean build artifacts
clean:
    rm -rf logs/*.log logs/*.jsonl
    rm -rf artifacts/*
    find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true

# Purge stored data: just purge db | just purge vault | just purge all
purge scope="all":
    #!/usr/bin/env bash
    set -euo pipefail
    if [ -z "${CONFIRM:-}" ]; then
        read -r -p "Purge '{{scope}}' data — this cannot be undone. Continue? [y/N] " c
        [[ "$c" =~ ^[Yy]$ ]] || { echo "aborted"; exit 1; }
    fi
    case "{{scope}}" in
      db)
        rm -fv data/kali-mcp.sqlite data/kali-mcp.sqlite-wal data/kali-mcp.sqlite-shm
        ;;
      vault)
        rm -rf artifacts/vault
        ;;
      all)
        rm -fv data/kali-mcp.sqlite data/kali-mcp.sqlite-wal data/kali-mcp.sqlite-shm
        rm -rf artifacts/vault
        ;;
      *)
        echo "unknown scope '{{scope}}' — use: db | vault | all" >&2
        exit 1
        ;;
    esac
    echo "purged: {{scope}}"
    echo "note: if the server is running, restart it (or just deploy) so it reconnects cleanly."

# Show all registered tools
tools:
    python -c "from kali_mcp.tools import ALL_TOOLS; [print(f'  {t.name:20s} {t.description[:60]}') for t in ALL_TOOLS]"

# Deploy to the Kali VM and restart the server (requires ssh key auth)
deploy host="kali-linux.shared" dest="~/kali-mcp":
    rsync -av --exclude '.venv' --exclude '__pycache__' --exclude '.git' \
        --exclude 'logs' --exclude 'artifacts' --exclude 'data' \
        ./ {{host}}:{{dest}}/
    ssh {{host}} "pkill -f '[k]ali_mcp' || true; cd {{dest}} && \
        nohup python -m kali_mcp > logs/stdout.log 2>&1 & sleep 2; \
        curl -sf --max-time 5 http://127.0.0.1:8399/sse -o /dev/null \
        && echo 'server restarted OK' || echo 'server did not come up — check logs/stdout.log'"

# Commit all changes, tag with version, and push — triggers PyPI release
release version="0.0.0":
    #!/usr/bin/env bash
    set -euo pipefail
    if [ "{{version}}" = "0.0.0" ]; then
        echo "Usage: just release version=0.1.3" >&2
        exit 1
    fi
    git add -A
    git commit -m "chore: release v{{version}}" || echo "nothing to commit"
    git tag -f "v{{version}}"
    git push origin master
    git push origin "v{{version}}" --force
    echo "Released v{{version}} — GitHub Actions will build and publish to PyPI"
