#!/usr/bin/env bash
# swarm — CLI entry point for the Swarm agent orchestrator

set -euo pipefail

SWARM_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

usage() {
    cat <<EOF
Usage: swarm <command> [options]

Commands:
  init          Initialize swarm in the current project
  setup-ci      Configure event-driven CI triggers
  worktrees     Manage agent worktrees (dashboard, list, clean)
  watch         Poll for issues (deprecated — use setup-ci)
  process <N>   Process issue #N through the agent pipeline
  review <N>    Review PR #N through the review agent
  help          Show this help message

Run 'swarm <command> --help' for command-specific help.
EOF
}

main() {
    local command="${1:-help}"
    shift || true

    export SWARM_COMMAND="$command"

    case "$command" in
        init)
            source "$SWARM_ROOT/lib/commands/init.sh"
            cmd_init "$@"
            ;;
        setup-ci)
            source "$SWARM_ROOT/lib/commands/setup-ci.sh"
            _setup_ci_flow "$@"
            ;;
        worktrees)
            source "$SWARM_ROOT/lib/commands/worktrees.sh"
            cmd_worktrees "$@"
            ;;
        watch)
            source "$SWARM_ROOT/lib/commands/watch.sh"
            cmd_watch "$@"
            ;;
        process)
            source "$SWARM_ROOT/lib/commands/process.sh"
            cmd_process "$@"
            ;;
        review)
            source "$SWARM_ROOT/lib/commands/review.sh"
            cmd_review "$@"
            ;;
        help|--help|-h)
            usage
            ;;
        *)
            echo "Unknown command: $command"
            echo ""
            usage
            exit 1
            ;;
    esac
}

main "$@"
