#!/usr/bin/env bash
# ---------------------------------------------------------------------------
# Koydo blank-slate workspace bootstrap (macOS / Linux)
#
#   curl -fsSL https://koydo.app/setup.sh | bash
#
# PUBLIC + SAFE: installs only general, publicly-available developer + AI
# tooling. It contains NO credentials, NO private repos, NO internal tooling
# (no vaults, no env files). After it runs you still sign in to each tool
# yourself. Re-runnable and best-effort: a step that fails warns and continues,
# and anything already installed is skipped.
#
# Want a leaner run? Set KOYDO_SETUP=core to install just the essentials
# (base + node + python + the AI CLIs + git/gh). Default installs the full kit.
# ---------------------------------------------------------------------------
set -uo pipefail

PROFILE="${KOYDO_SETUP:-full}"          # full | core
OS="$(uname -s)"

say()  { printf "\n\033[1;36m==>\033[0m %s\n" "$*"; }
warn() { printf "\033[1;33m  ! %s\033[0m\n" "$*"; }
have() { command -v "$1" >/dev/null 2>&1; }
try()  { "$@" || warn "skipped/failed: $*"; }

brewi() { for p in "$@"; do have brew && { brew list "$p" >/dev/null 2>&1 || try brew install "$p"; }; done; }
caski() { for p in "$@"; do have brew && { brew list --cask "$p" >/dev/null 2>&1 || try brew install --cask "$p"; }; done; }
apti()  { have apt-get && try sudo apt-get install -y "$@"; }
npmg()  { have npm && try npm install -g "$@"; }
uvtool(){ have uv && try uv tool install "$@"; }

say "Koydo blank-slate setup ($PROFILE) — general public tooling only, no credentials."

# 1) Base package manager + git/curl -----------------------------------------
if [ "$OS" = "Darwin" ]; then
  if ! have brew; then
    say "Installing Homebrew"
    try /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    eval "$(/opt/homebrew/bin/brew shellenv 2>/dev/null || /usr/local/bin/brew shellenv 2>/dev/null || true)"
  fi
  say "Base + git/gh"
  brewi git gh curl wget
elif [ "$OS" = "Linux" ]; then
  if have apt-get; then
    say "Base via apt"
    try sudo apt-get update -y
    apti git curl wget ca-certificates build-essential unzip
    if ! have gh; then
      try bash -c 'curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg'
      try bash -c 'echo "deb [signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list >/dev/null'
      try sudo apt-get update -y; apti gh
    fi
  else
    warn "No apt-get found — install git/gh/curl with your distro package manager."
  fi
fi

# 2) Node (LTS) + package managers -------------------------------------------
say "Node + pnpm/yarn"
if [ "$OS" = "Darwin" ]; then
  brewi node
elif ! have node; then
  say "Node LTS via nvm"
  try bash -c 'curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash'
  export NVM_DIR="$HOME/.nvm"; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
  try nvm install --lts
fi
try corepack enable
corepack prepare pnpm@latest --activate 2>/dev/null || npmg pnpm
corepack prepare yarn@stable --activate 2>/dev/null || true

# 3) Python (uv + pipx) ------------------------------------------------------
say "Python (uv + pipx)"
if [ "$OS" = "Darwin" ]; then
  brewi python uv pipx
else
  have uv   || try bash -c 'curl -LsSf https://astral.sh/uv/install.sh | sh'
  have pipx || apti pipx || true
fi

# 4) AI CLIs (Claude Code, Codex, Gemini, aider, llm, Ollama) ----------------
say "AI CLIs"
npmg @anthropic-ai/claude-code @openai/codex @google/gemini-cli
uvtool aider-chat
uvtool llm
if [ "$OS" = "Darwin" ]; then
  brewi ollama
  have ollama && try brew services start ollama
else
  have ollama || try bash -c 'curl -fsSL https://ollama.com/install.sh | sh'
fi

# 5) Cloud + deploy CLIs -----------------------------------------------------
say "Cloud + deploy CLIs"
npmg vercel netlify-cli @railway/cli
if [ "$OS" = "Darwin" ]; then
  brewi awscli flyctl doctl supabase/tap/supabase
else
  have flyctl || try bash -c 'curl -L https://fly.io/install.sh | sh'
  have aws    || warn "AWS CLI: see https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html"
fi

if [ "$PROFILE" = "full" ]; then
  # 6) Extra runtimes (Bun, Deno, Go, Rust) ----------------------------------
  say "Extra runtimes (Bun, Deno, Go, Rust)"
  if [ "$OS" = "Darwin" ]; then
    brewi bun deno go rustup
    have rustup && try rustup-init -y --no-modify-path 2>/dev/null
  else
    have bun  || try bash -c 'curl -fsSL https://bun.sh/install | bash'
    have deno || try bash -c 'curl -fsSL https://deno.land/install.sh | sh'
    apti golang-go || true
    have rustc || try bash -c "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y"
  fi

  # 7) Containers + infra (Docker via Colima, kubectl, terraform) ------------
  say "Containers + infra"
  if [ "$OS" = "Darwin" ]; then
    brewi colima docker docker-compose kubernetes-cli helm terraform
    have colima && try colima start
  else
    have docker || try bash -c 'curl -fsSL https://get.docker.com | sh'
    apti kubectl || true
  fi

  # 8) Editors (GUI, macOS casks) --------------------------------------------
  if [ "$OS" = "Darwin" ]; then
    say "Editors (VS Code, Cursor, Zed)"
    caski visual-studio-code cursor zed
    caski google-cloud-sdk
  else
    warn "GUI editors (VS Code, Cursor, Zed): install from their sites on Linux."
  fi

  # 9) Terminal quality-of-life ----------------------------------------------
  say "Terminal utilities"
  if [ "$OS" = "Darwin" ]; then
    brewi ripgrep fd fzf jq yq bat eza tree htop tmux direnv shellcheck gnupg coreutils httpie tldr watchman
  else
    apti ripgrep fd-find fzf jq bat tree htop tmux direnv shellcheck gnupg httpie || true
    warn "On Debian/Ubuntu: 'fd' is 'fdfind' and 'bat' is 'batcat'."
  fi
fi

# 10) Done -------------------------------------------------------------------
say "Done. Now sign in to each tool (interactive, your own accounts):"
cat <<'NEXT'
  gh auth login          # GitHub
  claude                 # Anthropic Claude Code (first run = sign-in)
  codex                  # OpenAI Codex (first run = sign-in)
  gemini                 # Google Gemini (first run = sign-in)
  aider --help           # aider (set your model provider key)
  vercel login           # Vercel
  wrangler login         # Cloudflare (install per-project: npm i -D wrangler)
  netlify login          # Netlify
  fly auth login         # Fly.io
  aws configure          # AWS
  gcloud init            # Google Cloud
  npx supabase --help    # Supabase (run per-project, no global install)

Full cheat sheet + per-OS commands: https://koydo.app/cli
NEXT
