#!/usr/bin/env bash
# Shared colors, helpers, and constants

# THE VERSION IS THE CLI'S, READ FROM IT. This was the literal "1.0.0", hardcoded when the
# operator was a script in a repo and never touched again — so `unoverse help` announced
# v1.0.0 while the published CLI was on 0.1.95, and the one number a developer uses to say
# which version they are running was wrong by two hundred releases.
#
# THE MONOREPO PATH IS TRIED FIRST, and the order matters. Two levels up from scripts/lib
# is the PLATFORM's package.json, which is genuinely 1.0.0 — so looking there first found a
# real version that was not this CLI's, and reported it confidently. packages/cli exists
# only in the monorepo; published, that path misses and the package's own is two levels up.
GRAVITY_VERSION=$(
  node -p "require('$GRAVITY_LIB/../../packages/cli/package.json').version" 2>/dev/null \
  || node -p "require('$GRAVITY_LIB/../../package.json').version" 2>/dev/null \
  || echo "unknown"
)
DOCR_REGISTRY="registry.digitalocean.com"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
DIM='\033[2m'
BOLD='\033[1m'
NC='\033[0m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
WHITE='\033[1;37m'
UNDERLINE='\033[4m'

ok()   { echo -e "  ${GREEN}✓${NC} $1"; }
warn() { echo -e "  ${YELLOW}⚠${NC} $1"; }
fail() { echo -e "  ${RED}✗${NC} $1"; }
info() { echo -e "  ${DIM}$1${NC}"; }
link() { echo -e "  ${CYAN}${UNDERLINE}$1${NC}"; }
banner() {
  echo ""
  echo -e "  ${BOLD}${CYAN}⬡ $1${NC}"
  echo -e "  ${DIM}─────────────────────────────────${NC}"
}

# The branded access box. ONE definition so start.sh, update.sh and dashboard.sh
# never drift apart again.
print_access_urls() {
  echo -e "  ${WHITE}${BOLD}unoverse${NC}${DIM}, the experience layer for AI${NC}"
  echo -e "  ${DIM}Use the Unoverse MCP to build agents. Somewhere, Skynet is taking notes. 🤖${NC}"
  echo ""
  echo -e "  ${CYAN}Canvas${NC}  ${DIM}(build agents)${NC}   ${UNDERLINE}http://localhost:3001${NC}"
  echo -e "  ${CYAN}API${NC}     ${DIM}(REST + MCP)${NC}     ${UNDERLINE}http://localhost:4105${NC}"
  # NOT A URL, BECAUSE NOTHING IS LISTENING THERE. Studio left the platform image and is
  # launched from npm by `unoverse studio`, so printing http://localhost:3002 after `start`
  # offered an address that could only refuse the connection.
  echo -e "  ${CYAN}Studio${NC}  ${DIM}(build assets)${NC}   ${DIM}unoverse studio${NC}"
  echo ""
  echo -e "  ${DIM}▶ Next:${NC} open this repo in ${BOLD}Claude Code${NC} and ask it to build an agent"
}

# Elapsed time helper
timer_start() { GRAVITY_START=$(date +%s); }
timer_elapsed() {
  local end=$(date +%s)
  local elapsed=$((end - GRAVITY_START))
  if [ $elapsed -ge 60 ]; then
    echo "$((elapsed / 60))m $((elapsed % 60))s"
  else
    echo "${elapsed}s"
  fi
}

# The old "studio mode" system (mode file, is_platform_mode, require_platform_mode)
# was removed 2026-07-28: Studio is a separate app now, and this CLI has one job:
# operate a universe. See _legacy/scripts-lib/ for the retired authoring tools.

# ── pick_option: the arrow-key list, for bash ────────────────────────────────
# Same interaction as the create menu (packages/cli/lib/create.mjs): up/down move,
# a digit jumps, Enter takes it. Sets PICKED (0-based). Not a TTY falls back to
# reading a number, so scripted runs keep working.
pick_option() {
  local active=0 n=$# i k k2
  local opts=("$@")
  PICKED=0
  if [ ! -t 0 ]; then
    local line
    IFS= read -r line || true
    case "$line" in [1-9]) [ "$line" -le "$n" ] && PICKED=$((line-1));; esac
    return 0
  fi
  _po_draw() {
    for ((i=0; i<n; i++)); do
      if [ "$i" -eq "$active" ]; then
        printf '  \033[36m❯\033[0m %d  \033[1m%s\033[0m\033[K\n' $((i+1)) "${opts[$i]}"
      else
        printf '    %d  %s\033[K\n' $((i+1)) "${opts[$i]}"
      fi
    done
    printf '  \033[2m↑↓ to move, Enter to choose\033[0m\033[K'
  }
  printf '\033[?25l'
  _po_draw
  while true; do
    IFS= read -rsn1 k || break
    case "$k" in
      $'\x1b')
        IFS= read -rsn2 -t 1 k2 || k2=""
        case "$k2" in
          '[A') active=$(( (active-1+n)%n ));;
          '[B') active=$(( (active+1)%n ));;
        esac ;;
      [1-9]) [ "$k" -le "$n" ] 2>/dev/null && active=$((k-1)) ;;
      ""|$'\n') break ;;
    esac
    printf '\r\033[%dA' "$n"
    _po_draw
  done
  printf '\033[?25h\n'
  PICKED=$active
}
