#!/usr/bin/env bash
# CoderLap Docs Toolkit — one-line installer
# Usage:  curl -fsSL https://coderv.dev/install.sh | bash
#
# Default: clones the toolkit and runs its installer, which copies the 5
# slash-command skills into ~/.claude/skills/ and wires the coderv-router
# UserPromptSubmit hook into ~/.claude/settings.json (Claude Code only).
#
# Flags pass through to the toolkit installer:
#   --codex / --gemini / --all   install for those CLIs (skills only — no hook)
#   --force                       overwrite existing files
#   --uninstall                   remove the toolkit
#
# --project is handled by THIS script (drops skills into ./.claude/skills/
# of the current directory, no hook).

set -euo pipefail

REPO="https://github.com/AbudiHadi/coderv.git"
TMPDIR="$(mktemp -d -t coderv-XXXXXX)"
trap 'rm -rf "$TMPDIR"' EXIT

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
NC='\033[0m'

echo
echo -e "${BOLD}${BLUE}CoderLap Docs Toolkit${NC} — installer"
echo -e "${BLUE}https://coderv.dev${NC}"
echo

if ! command -v git &>/dev/null; then
  echo -e "${RED}Error:${NC} git is required but not installed."
  echo "Install git first, then re-run this command."
  exit 1
fi

# Parse: --project is local-mode, everything else is forwarded.
MODE="user"
declare -a FORWARD=()
for arg in "$@"; do
  case "$arg" in
    --project) MODE="project" ;;
    --help|-h)
      cat <<EOF
Usage: curl -fsSL https://coderv.dev/install.sh | bash
       curl -fsSL https://coderv.dev/install.sh | bash -s -- [options]

Hosts (default is Claude Code):
  --codex       Install to ~/.codex/skills/   (skills only — no hook)
  --gemini      Install to ~/.gemini/skills/  (skills only — no hook)
  --all         Install to Claude + Codex + Gemini

Other options:
  --project     Install into ./.claude/skills/ of current dir instead of ~
  --force       Overwrite existing skills without asking
  --uninstall   Remove toolkit skills (and the router hook on Claude Code)
  --help        Show this message

Installs the 5 CoderLap slash commands:
  /docify, /before, /decision, /ship, /session

On Claude Code, also installs the coderv-router hook so the right command
is suggested automatically when your phrasing matches.

More info: https://coderv.dev
EOF
      exit 0
      ;;
    *) FORWARD+=("$arg") ;;
  esac
done

echo -e "${BLUE}1.${NC} Fetching latest toolkit from GitHub…"
git clone --depth 1 "$REPO" "$TMPDIR/coderv" --quiet 2>&1 | grep -v "^Cloning" || true

echo -e "${BLUE}2.${NC} Running installer…"
echo

if [[ "$MODE" == "project" ]]; then
  # Per-project: copy skills only into ./.claude/skills/ of current dir.
  # No hook — hooks are user-scope by design.
  FORCE_LOCAL=0
  for f in "${FORWARD[@]}"; do [[ "$f" == "--force" ]] && FORCE_LOCAL=1; done

  mkdir -p .claude/skills
  for skill_dir in "$TMPDIR/coderv/skills"/*/; do
    skill_name="$(basename "$skill_dir")"
    dst=".claude/skills/$skill_name"
    if [[ -d "$dst" && "$FORCE_LOCAL" -eq 0 ]]; then
      echo -e "  ${YELLOW}exists${NC}  /$skill_name (use --force to overwrite)"
    else
      rm -rf "$dst"
      cp -r "$skill_dir" "$dst"
      grep -q "claude-docs-toolkit" "$dst/SKILL.md" 2>/dev/null \
        || printf '\n<!-- claude-docs-toolkit -->\n' >> "$dst/SKILL.md"
      echo -e "  ${GREEN}installed${NC} /$skill_name → ./.claude/skills/$skill_name"
    fi
  done
  echo
  echo -e "  ${YELLOW}Note:${NC} project-scope install does not install the coderv-router"
  echo -e "        hook. Hooks are user-scope and live in ~/.claude/settings.json."
else
  # Hand off to the toolkit's own installer with all forwarded flags.
  cd "$TMPDIR/coderv"
  bash ./install.sh "${FORWARD[@]}"
fi

echo
echo -e "${GREEN}${BOLD}✓ Done.${NC}"
echo
echo "Five commands:"
echo -e "  ${BOLD}/docify${NC}            — Once per project: scan code, generate CLAUDE.md + docs"
echo -e "  ${BOLD}/before <task>${NC}     — Claude reads docs, plans, waits for your OK"
echo -e "  ${BOLD}/decision <title>${NC}  — Log why you chose X over Y (30 seconds)"
echo -e "  ${BOLD}/ship${NC}              — Auto-updates docs, validates citations, drafts commit"
echo -e "  ${BOLD}/session${NC}           — End-of-session handoff for next time"
echo
echo "Docs: https://coderv.dev"
echo
