#!/usr/bin/env bash
#
# deeptide installer — fetches the latest macOS arm64 build from GitHub
# releases, drops `deeptide` and a `tide` symlink into ~/.local/bin (or
# $DEEPTIDE_BIN_DIR), and verifies the binary launches.
#
#   curl -fsSL https://deeptide.sh | sh
#
# Override the install dir with DEEPTIDE_BIN_DIR=/usr/local/bin etc.
# To pin a version: DEEPTIDE_VERSION=v0.6.40 curl … | sh

set -euo pipefail

REPO="paean-ai/deeptide"
BIN_DIR="${DEEPTIDE_BIN_DIR:-$HOME/.local/bin}"
PIN_VERSION="${DEEPTIDE_VERSION:-}"

# ANSI colors when stdout is a TTY.
if [ -t 1 ]; then
  BOLD=$'\033[1m'; DIM=$'\033[2m'; CYAN=$'\033[36m'; GREEN=$'\033[32m'
  YELLOW=$'\033[33m'; RED=$'\033[31m'; RESET=$'\033[0m'
else
  BOLD=""; DIM=""; CYAN=""; GREEN=""; YELLOW=""; RED=""; RESET=""
fi

say()  { printf "%s\n" "$*"; }
note() { printf "%s\n" "${DIM}$*${RESET}"; }
err()  { printf "%s\n" "${RED}error:${RESET} $*" >&2; }

# --- Platform check: macOS arm64 only -----------------------------------
case "$(uname -s)" in
  Darwin) ;;
  *)
    err "deeptide is currently macOS-only."
    note "For Linux / Windows, use the cross-platform sibling:"
    note "  https://github.com/paean-ai/zero-cli"
    exit 1
    ;;
esac

case "$(uname -m)" in
  arm64) ;;
  x86_64)
    err "deeptide currently ships arm64 (Apple Silicon) builds only."
    note "Intel-Mac fat binaries are tracked but not yet published."
    exit 1
    ;;
  *)
    err "Unrecognised arch: $(uname -m). Expected arm64."
    exit 1
    ;;
esac

# --- Resolve release tag -------------------------------------------------
if [ -n "$PIN_VERSION" ]; then
  TAG="$PIN_VERSION"
  note "Pinned to $TAG"
else
  TAG=$(curl -fsSL \
    "https://api.github.com/repos/$REPO/releases/latest" \
    | grep '"tag_name":' | head -1 | cut -d'"' -f4 || true)
  if [ -z "$TAG" ]; then
    err "Could not resolve the latest deeptide release."
    note "Network down? Check https://github.com/$REPO/releases"
    exit 1
  fi
fi

TARBALL="deeptide-${TAG}-darwin-arm64.tar.gz"
URL="https://github.com/$REPO/releases/download/${TAG}/${TARBALL}"

# --- Download + extract --------------------------------------------------
TMP=$(mktemp -d)
trap "rm -rf '$TMP'" EXIT

say "${CYAN}deeptide${RESET} ${BOLD}${TAG}${RESET}  ${DIM}(macOS arm64)${RESET}"
say "Downloading ${DIM}${URL}${RESET}"
if ! curl -fsSL --progress-bar "$URL" -o "$TMP/$TARBALL"; then
  err "Download failed. URL above; retry, or grab the asset manually from"
  note "  https://github.com/$REPO/releases/tag/$TAG"
  exit 1
fi

tar -xzf "$TMP/$TARBALL" -C "$TMP"

if [ ! -x "$TMP/deeptide" ]; then
  err "Tarball did not contain a 'deeptide' executable."
  exit 1
fi

# --- Install -------------------------------------------------------------
mkdir -p "$BIN_DIR"
install -m 0755 "$TMP/deeptide" "$BIN_DIR/deeptide"
ln -sf "$BIN_DIR/deeptide" "$BIN_DIR/tide"

# --- Verify --------------------------------------------------------------
INSTALLED_VERSION=$("$BIN_DIR/deeptide" --version 2>/dev/null | head -1 || echo "?")

say ""
say "${GREEN}✓${RESET} ${BOLD}deeptide ${INSTALLED_VERSION}${RESET} installed"
say "  ${DIM}→ $BIN_DIR/deeptide${RESET}"
say "  ${DIM}→ $BIN_DIR/tide   (symlink)${RESET}"
say ""

# PATH check.
case ":$PATH:" in
  *":$BIN_DIR:"*) ;;
  *)
    say "${YELLOW}!${RESET} ${BOLD}$BIN_DIR${RESET} is not in your PATH yet."
    say "  Add this line to your shell rc (e.g. ~/.zshrc):"
    say "    ${CYAN}export PATH=\"$BIN_DIR:\$PATH\"${RESET}"
    say ""
    ;;
esac

# Get-started hint.
say "${BOLD}Next:${RESET}"
say "  ${CYAN}tide auth login${RESET}   ${DIM}# Paean OAuth (multimodal-aware)${RESET}"
say "  ${CYAN}tide login${RESET}        ${DIM}# DeepSeek API key in macOS Keychain${RESET}"
say "  ${CYAN}tide${RESET}              ${DIM}# launch the REPL${RESET}"
say "  ${CYAN}tide doctor${RESET}       ${DIM}# diagnose install + network${RESET}"
