#!/usr/bin/env bash
set -euo pipefail

# =============================================================================
# omo-paseo-bridge.sh — Import OMO task() child sessions as Paseo agents
#
# Automates calling `paseo import` on child session IDs so OMO subagents
# appear in `paseo ls` alongside native Paseo agents.
#
# Usage:
#   omo-paseo-bridge.sh import [--type <category>] <session-id>...
#       Import session(s) as Paseo agents with omo=true labels
#
#   omo-paseo-bridge.sh archive <agent-id>...
#       Archive (soft-delete) agent(s) imported by this bridge
#
#   omo-paseo-bridge.sh ls [--all]
#       List agents tagged omo=true via paseo ls
#
#   omo-paseo-bridge.sh --dry-run <command> ...
#       Print what would be done without executing
#
# Examples:
#   omo-paseo-bridge.sh import ses_abc123 ses_def456
#   omo-paseo-bridge.sh import --type research ses_abc123
#   omo-paseo-bridge.sh archive agt_789
#   omo-paseo-bridge.sh ls
#   omo-paseo-bridge.sh --dry-run import ses_abc123
# =============================================================================

SCRIPT_NAME="$(basename "$0")"
PASEO="$(which paseo 2>/dev/null || echo "paseo")"
DRY_RUN=false

# ── helpers ──────────────────────────────────────────────────────────────────

log()  { printf "[%s] %s\n" "$SCRIPT_NAME" "$*"; }
warn() { printf "[%s] WARNING: %s\n" "$SCRIPT_NAME" "$*" >&2; }
err()  { printf "[%s] ERROR: %s\n" "$SCRIPT_NAME" "$*" >&2; exit 1; }

paseo_cmd() {
  if $DRY_RUN; then
    log "[DRY-RUN] would run: $PASEO $*"
    return 0
  fi
  "$PASEO" "$@" 2>&1 || warn "'paseo $*' exited with code $?"
}

paseo_import() {
  local session_id="$1"
  shift
  local type_label="${1:-}"
  local labels=("--label" "omo=true")

  # Add parent session label if OMO_SESSION_ID is set (injected by agent)
  if [[ -n "${OMO_SESSION_ID:-}" ]]; then
    labels+=("--label" "parent=${OMO_SESSION_ID}")
  fi

  if [[ -n "$type_label" ]]; then
    labels+=("--label" "type=${type_label}")
  fi

  log "Importing session ${session_id} as Paseo agent ..."
  paseo_cmd import "$session_id" --provider opencode "${labels[@]}"
}

paseo_archive() {
  local agent_id="$1"
  log "Archiving agent ${agent_id} ..."
  paseo_cmd archive "$agent_id" --force
}

paseo_list() {
  local all_flag="${1:-}"
  if [[ "$all_flag" == "--all" ]]; then
    paseo_cmd ls --label "omo=true" --all
  else
    paseo_cmd ls --label "omo=true"
  fi
}

# ── usage ────────────────────────────────────────────────────────────────────

usage() {
  cat <<EOF
Usage: $SCRIPT_NAME [--dry-run] <command> [options] [args...]

Commands:
  import [--type <category>] <session-id>...
      Import OMO child session(s) as Paseo agents
  archive <agent-id>...
      Archive Paseo agent(s) (soft-delete)
  ls [--all]
      List agents tagged omo=true

Options:
  --dry-run   Print actions without executing them
  -h, --help  Show this help

Examples:
  $SCRIPT_NAME import --type research ses_abc123
  $SCRIPT_NAME archive agt_789
  $SCRIPT_NAME ls --all
EOF
  exit 0
}

# ── main ─────────────────────────────────────────────────────────────────────

# Peel off global flags
while [[ $# -gt 0 ]]; do
  case "$1" in
    --dry-run) DRY_RUN=true; shift ;;
    -h|--help) usage ;;
    *) break ;;
  esac
done

[[ $# -eq 0 ]] && usage

COMMAND="$1"
shift

case "$COMMAND" in
  import)
    TYPE_LABEL=""
    SESSION_IDS=()

    while [[ $# -gt 0 ]]; do
      case "$1" in
        --type) TYPE_LABEL="$2"; shift 2 ;;
        --type=*) TYPE_LABEL="${1#*=}"; shift ;;
        -*) err "Unknown option for import: $1" ;;
        *) SESSION_IDS+=("$1"); shift ;;
      esac
    done

    [[ ${#SESSION_IDS[@]} -eq 0 ]] && err "import requires at least one session-id"

    for sid in "${SESSION_IDS[@]}"; do
      paseo_import "$sid" "$TYPE_LABEL"
    done
    ;;

  archive)
    [[ $# -eq 0 ]] && err "archive requires at least one agent-id"
    for aid in "$@"; do
      paseo_archive "$aid"
    done
    ;;

  ls)
    paseo_list "${1:-}"
    ;;

  *)
    err "Unknown command: $COMMAND\n$(usage)"
    ;;
esac
