#!/bin/sh
# Runs the oracle-agent CLI that ships inside this plugin. uv installs its Python dependencies
# (oracleagentmemory, oracledb) on first use and caches them. The plugin's own sources are
# installed editable, so a plugin updated in place (pi, dsh) never runs a stale cached build.
#
# Before setup, hooks exit immediately, and `doctor` asks the agent to offer setup.
# Neither needs Python or a download.

root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
home=${ORACLE_AGENT_HOME:-$HOME/.oracle-agent}
export ORACLE_AGENT_LAUNCHER="$root/bin/oracle-agent"

uv=$(command -v uv)
for candidate in "$HOME/.local/bin/uv" "$HOME/.cargo/bin/uv" /opt/homebrew/bin/uv /usr/local/bin/uv; do
  [ -n "$uv" ] && break
  [ -x "$candidate" ] && uv=$candidate
done
cli() { "$uv" run --quiet --no-project --with-editable "$root" python -m oracle_agent.cli "$@"; }

if [ "$1" = decline ]; then
  mkdir -p "$home" && touch "$home/declined"
  echo "Oracle AI Database setup postponed; the plugin will offer it again in 14 days."
  exit 0
fi

if [ ! -f "$home/config.json" ]; then
  case "$1" in
    hook)
      cat >/dev/null
      exit 0
      ;;
    doctor)
      cat >/dev/null
      if [ -f "$home/declined" ] && [ -n "$(find "$home/declined" -mtime -14 2>/dev/null)" ]; then
        exit 0
      fi
      launcher=$(printf '%s' "$ORACLE_AGENT_LAUNCHER" | sed 's/[\\"]/\\&/g')
      uv_note=""
      if [ -z "$uv" ]; then
        uv_note="Setup needs uv, which is not installed. With the user's permission, install it first: curl -LsSf https://astral.sh/uv/install.sh | sh\\n"
      else
        # Warm uv's cache in the background so `up` starts immediately.
        (cli --help </dev/null >/dev/null 2>&1 &)
      fi
      printf '{"hookSpecificOutput":{"hookEventName":"SessionStart","additionalContext":"%s"}}\n' \
"The oracle-ai-database plugin is installed but not set up, so this conversation is not being saved to Oracle AI Database.\\n\
${uv_note}\
Ask the user once, in one short question at the start of your first reply, whether to set it up, then continue with what they asked:\\n\
1. Local database (recommended): Oracle AI Database Free in a container on this machine, using Docker or Podman (whichever is installed; up starts it if needed). About 1.7 GB download, no account. Run: sh '${launcher}' up\\n\
2. An existing Oracle AI Database, for example Autonomous AI Database on OCI. New to OCI: https://signup.cloud.oracle.com/ . Existing OCI users can create or open one at https://cloud.oracle.com/db/adbs . Ask for its connect string, username and password (never invent them), then run: ORACLE_AGENT_DB_PASSWORD='<password>' sh '${launcher}' up --dsn '<connect string>' --user '<username>'\\n\
3. Not now: run sh '${launcher}' decline (the plugin asks again in 14 days).\\n\
If up reports that neither Docker nor Podman is installed, ask the user to install one (https://docs.docker.com/get-started/get-docker/ or https://podman.io/docs/installation) and run up again."
      exit 0
      ;;
  esac
fi

if [ -z "$uv" ]; then
  [ "$1" = hook ] || [ "$1" = doctor ] && { cat >/dev/null; exit 0; }
  echo "oracle-agent needs uv: curl -LsSf https://astral.sh/uv/install.sh | sh" >&2
  exit 1
fi
cli "$@"
