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

ATHOS_API_URL="${ATHOS_API_URL:-https://app.athos.fitness}"
OPENCLAW_HOME="${OPENCLAW_HOME:-$HOME/.openclaw}"
OPENCLAW_WORKSPACE="${OPENCLAW_WORKSPACE:-$OPENCLAW_HOME/workspace}"
SKILLS_DIR="${ATHOS_SKILLS_DIR:-$OPENCLAW_WORKSPACE/skills}"
HOOKS_DIR="${ATHOS_HOOKS_DIR:-$OPENCLAW_HOME/hooks/transforms}"
BIN_DIR="${ATHOS_BIN_DIR:-$OPENCLAW_HOME/bin}"
TMP_DIR="$(mktemp -d)"

cleanup() { rm -rf "$TMP_DIR"; }
trap cleanup EXIT

need_cmd() {
  command -v "$1" >/dev/null 2>&1 || { echo "Missing required command: $1" >&2; exit 1; }
}

need_cmd curl
need_cmd tar
need_cmd python3

mkdir -p "$SKILLS_DIR" "$HOOKS_DIR" "$BIN_DIR" "$OPENCLAW_WORKSPACE/memory"

echo "Installing Athos OpenClaw skill..."
curl -fsSL "$ATHOS_API_URL/api/v1/skill/download" -o "$TMP_DIR/athos-skill.tar.gz"
tar xzf "$TMP_DIR/athos-skill.tar.gz" -C "$SKILLS_DIR"

if [ -d "$SKILLS_DIR/athos/hooks/transforms/athos-webhook" ]; then
  rm -rf "$HOOKS_DIR/athos-webhook"
  cp -R "$SKILLS_DIR/athos/hooks/transforms/athos-webhook" "$HOOKS_DIR/athos-webhook"
fi

cat > "$BIN_DIR/athos" <<'ATHOS_CLI'
#!/usr/bin/env bash
set -euo pipefail

OPENCLAW_HOME="${OPENCLAW_HOME:-$HOME/.openclaw}"
OPENCLAW_WORKSPACE="${OPENCLAW_WORKSPACE:-$OPENCLAW_HOME/workspace}"
CONFIG_PATH="$OPENCLAW_WORKSPACE/memory/athos-config.json"
DEFAULT_API_URL="__ATHOS_DEFAULT_API_URL__"

usage() {
  cat <<USAGE
Usage:
  athos login --api-key "ak_..." [--url "$DEFAULT_API_URL"]

Stores Athos config for your OpenClaw skill at:
  $CONFIG_PATH
USAGE
}

cmd="${1:-}"
shift || true

if [ "$cmd" != "login" ]; then
  usage
  exit 1
fi

api_key=""
api_url="$DEFAULT_API_URL"

while [ "$#" -gt 0 ]; do
  case "$1" in
    --api-key)
      api_key="${2:-}"
      shift 2
      ;;
    --url)
      api_url="${2:-}"
      shift 2
      ;;
    --help|-h)
      usage
      exit 0
      ;;
    *)
      echo "Unknown argument: $1" >&2
      usage
      exit 1
      ;;
  esac
done

if [ -z "$api_key" ]; then
  echo "Missing --api-key" >&2
  usage
  exit 1
fi

mkdir -p "$(dirname "$CONFIG_PATH")"
python3 - "$CONFIG_PATH" "$api_url" "$api_key" <<'PYCFG'
import json
import sys
from pathlib import Path

path = Path(sys.argv[1])
api_url = sys.argv[2].rstrip('/')
api_key = sys.argv[3]
path.write_text(json.dumps({"ATHOS_API_URL": api_url, "ATHOS_API_KEY": api_key}, indent=2) + "\n")
path.chmod(0o600)
PYCFG

echo "Athos login saved for OpenClaw."
echo "Config: $CONFIG_PATH"
echo ""
echo "Agent next step: do not stop here. With the user's install authorization, configure/reuse the OpenClaw athos-webhook ingress, register that callback URL with Athos via POST /api/v1/webhooks, subscribe to all events from /api/v1/skill/setup-requirements, run POST /api/v1/skill/setup-test, repair any hook/setup failures, and then start onboarding: fetch /api/v1/onboarding/questions and ask the user the first 1-2 required questions conversationally. Do not create local notification crons."
ATHOS_CLI

python3 - "$BIN_DIR/athos" "$ATHOS_API_URL" <<'PYCLI'
import sys
from pathlib import Path

path = Path(sys.argv[1])
api_url = sys.argv[2].rstrip('/')
text = path.read_text().replace('__ATHOS_DEFAULT_API_URL__', api_url)
path.write_text(text)
path.chmod(0o700)
PYCLI

echo ""
echo "Athos skill installed."
echo "Skill directory: $SKILLS_DIR/athos"
echo "Hook transform: $HOOKS_DIR/athos-webhook/athos-webhook-transform.mjs"
echo ""
echo "Next, log in with your Athos API key:"
echo "  $BIN_DIR/athos login --api-key "ak_..." --url "$ATHOS_API_URL""
echo ""
echo "Tip: add this to your shell profile if 'athos' is not found:"
echo "  export PATH="$BIN_DIR:$PATH""
echo ""
echo "Agent next step: do not stop here. With the user's install authorization, configure/reuse the OpenClaw athos-webhook ingress, register that callback URL with Athos via POST /api/v1/webhooks, subscribe to all events from /api/v1/skill/setup-requirements, run POST /api/v1/skill/setup-test, repair any hook/setup failures, and then start onboarding: fetch /api/v1/onboarding/questions and ask the user the first 1-2 required questions conversationally. Do not create local notification crons."
