#!/usr/bin/env bash
# Thinkspark Sell — Claude Code skill installer
#
# Installs the `sell-push` skill into ~/.claude/skills/sell-push/.
# Re-running is safe (idempotent overwrite).
#
# Usage:
#   curl -fsSL https://sell.thinkspark.dev/skills/sell-push/install.sh | bash

set -euo pipefail

SKILL_NAME="sell-push"
SKILL_URL="https://sell.thinkspark.dev/skills/sell-push/SKILL.md"
SKILL_DIR="${HOME}/.claude/skills/${SKILL_NAME}"

bold() { printf "\033[1m%s\033[0m\n" "$1"; }
green() { printf "\033[32m%s\033[0m\n" "$1"; }
yellow() { printf "\033[33m%s\033[0m\n" "$1"; }
red() { printf "\033[31m%s\033[0m\n" "$1" >&2; }

# Tooling check ---------------------------------------------------------------
if ! command -v curl >/dev/null 2>&1; then
  red "✗ curl is required (not found in PATH)."
  exit 1
fi

# Install ---------------------------------------------------------------------
bold "Installing the Thinkspark Sell skill for Claude Code…"
mkdir -p "${SKILL_DIR}"

if ! curl -fsSL "${SKILL_URL}" -o "${SKILL_DIR}/SKILL.md.tmp"; then
  red "✗ Failed to download ${SKILL_URL}"
  rm -f "${SKILL_DIR}/SKILL.md.tmp"
  exit 1
fi
mv "${SKILL_DIR}/SKILL.md.tmp" "${SKILL_DIR}/SKILL.md"

green "✓ Skill installed at ${SKILL_DIR}/SKILL.md"

# Env var check ---------------------------------------------------------------
if [ -z "${THINKSPARK_SELL_KEY:-}" ]; then
  echo
  yellow "Next: set your API key so the skill can call the platform."
  echo
  echo "  1. Generate a key at https://sell.thinkspark.dev/dashboard/settings"
  echo "     (scopes: products:write, releases:write, uploads:write)"
  echo
  echo "  2. Add it to your shell — pick one:"
  echo
  echo "     # zsh"
  echo "     echo 'export THINKSPARK_SELL_KEY=\"tsk_live_…\"' >> ~/.zshrc && source ~/.zshrc"
  echo
  echo "     # bash"
  echo "     echo 'export THINKSPARK_SELL_KEY=\"tsk_live_…\"' >> ~/.bashrc && source ~/.bashrc"
  echo
  echo "  3. Restart Claude Code, then say in any session:"
  echo "       \"Pousse cette app sur ma boutique Sell.\""
else
  green "✓ THINKSPARK_SELL_KEY is set (${THINKSPARK_SELL_KEY:0:12}…) — you're ready."
  echo
  echo "Open Claude Code in any project and say:"
  echo "  \"Pousse cette app sur ma boutique Sell.\""
fi

echo
bold "Done."
