#!/usr/bin/env bash
# .agents/bin/dev — box-box agentic dev CLI. Works from any harness's shell.
#
#   dev implement <issue#> --harness <codex|cursor|claude|opencode|pi|agy> [--dry-run] [--base <branch>]
#
# Grooming is driven interactively via the Claude Code /groom skill; this CLI covers
# the implement lane (dispatch a Ready issue to a harness → worktree → gate → PR).

set -o pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")/../.." && pwd)"

usage() {
  cat >&2 <<EOF
box-box dev CLI

  dev implement <issue#> --harness <name> [--dry-run] [--base <branch>]

harnesses: codex, cursor (recommended) · claude, opencode (available) · agy (dry-run only, disabled for dispatch) · pi (verify flags)
  --dry-run   render the prompt + plan, touch nothing (no worktree/PR/state change)
  --base      base branch for the worktree/PR (default: main)
EOF
  exit 2
}

cmd="${1:-}"; shift 2>/dev/null || true
case "$cmd" in
  implement)
    issue="${1:-}"; shift 2>/dev/null || true
    harness=""; passthru=()
    while [ $# -gt 0 ]; do
      case "$1" in
        --harness) harness="${2:-}"; shift 2 ;;
        --dry-run) passthru+=(--dry-run); shift ;;
        --base)    passthru+=(--base "${2:-}"); shift 2 ;;
        *) echo "unknown arg: $1" >&2; usage ;;
      esac
    done
    [ -n "$issue" ] && [ -n "$harness" ] || usage
    # shellcheck source=/dev/null
    source "$ROOT/.agents/lib/dispatch.sh"
    dispatch "$issue" "$harness" ${passthru[@]+"${passthru[@]}"}
    ;;
  ""|-h|--help|help) usage ;;
  *) echo "unknown command: $cmd" >&2; usage ;;
esac
