#!/usr/bin/env bash
# Install ai-flow skills + agent-agnostic user-level knowledge artifacts.
#
# Usage:
#   bash scripts/install.sh [--agent <name>]   # default: claude-code
#   bash scripts/install.sh --agent cursor
#   bash scripts/install.sh --agent 'claude-code,cursor'   # multiple
#   bash scripts/install.sh --agent '*'                    # all supported agents
#   bash scripts/install.sh cursor           # positional also works
#
# Two concerns, cleanly separated:
#   1. SKILLS → installed via `npx skills add . --agent <name> --global --copy`.
#      The skills CLI maintains the agent→dir mapping (claude-code→~/.claude/skills/,
#      cursor→its dir, …), so this script doesn't hardcode agent dirs.
#   2. USER DATA (knowledge + flow-solutions) → agent-agnostic ~/.ai-flow/.
#      Same data works no matter which agent loads the skills. Read from source
#      skills/, so install does NOT depend on `npm run build`. knowledge/ overwrites
#      on upgrade; flow-solutions/ seed never overwrites user-accumulated data.
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT_DIR"

AGENT="claude-code"   # default; backward compatible
while [ $# -gt 0 ]; do
  case "$1" in
    --agent)
      AGENT="$2"; shift 2 ;;
    --agent=*)
      AGENT="${1#--agent=}"; shift ;;
    -h|--help)
      sed -n '2,12p' "$0"; exit 0 ;;
    *)
      AGENT="$1"; shift ;;   # first positional → agent name
  esac
done

AI_FLOW_DIR="$HOME/.ai-flow"        # agent-agnostic; shared across all agents
SRC_KNOWLEDGE="$ROOT_DIR/skills/knowledge"
SRC_FLOW_SOL="$ROOT_DIR/skills/flow-solutions"

if [ ! -d "$SRC_KNOWLEDGE" ] || [ ! -d "$SRC_FLOW_SOL" ]; then
  echo "ERROR: skills/knowledge or skills/flow-solutions not found under $ROOT_DIR" >&2
  echo "       Run this script from the repo (source install path)." >&2
  exit 1
fi

echo "=== Installing ai-flow skills + user-data ==="
echo "  agent     → $AGENT"
echo "  skills    → via 'npx skills add' (CLI decides target dir per agent)"
echo "  user-data → $AI_FLOW_DIR/ (agent-agnostic)"
echo ""

# Step 1: Skills → agent-specific global dir, via skills CLI (no hardcoded dir map)
echo "Installing skills via 'npx skills add --agent $AGENT --global --copy'..."
npx skills add . --agent "$AGENT" --global --copy -y

# Step 2: Ensure agent-agnostic user-data dirs exist
mkdir -p "$AI_FLOW_DIR/knowledge" "$AI_FLOW_DIR/flow-solutions"

# Step 3: knowledge/ → overwrite unconditionally (read-only, version-locked to skill)
echo "Installing knowledge → $AI_FLOW_DIR/knowledge/ (overwrite, from source)"
cp -r "$SRC_KNOWLEDGE/"* "$AI_FLOW_DIR/knowledge/"

# Step 4: flow-solutions/ SEED → only-if-not-exists (protect user-accumulated data)
echo "Installing flow-solutions seed → $AI_FLOW_DIR/flow-solutions/ (skip existing, from source)"
new_count=0
skip_count=0
for item in "$SRC_FLOW_SOL"/*; do
  [ -e "$item" ] || continue  # robust against empty dir
  name=$(basename "$item")
  if [ -e "$AI_FLOW_DIR/flow-solutions/$name" ]; then
    skip_count=$((skip_count + 1))
  else
    cp -r "$item" "$AI_FLOW_DIR/flow-solutions/$name"
    new_count=$((new_count + 1))
  fi
done
echo "  → $new_count new, $skip_count kept (user data preserved)"

# Strip macOS metadata
find "$AI_FLOW_DIR" -name '.DS_Store' -delete 2>/dev/null || true

echo ""
echo "=== Install complete ==="
echo "Agent:           $AGENT (verify with: npx skills list --global)"
echo "Knowledge:       $(find "$AI_FLOW_DIR/knowledge" -type f | wc -l | tr -d ' ') files"
echo "Flow-solutions:  $(find "$AI_FLOW_DIR/flow-solutions" -type f | wc -l | tr -d ' ') files"
echo ""
echo "Next: skills read ~/.ai-flow/{knowledge,flow-solutions}/ (works for any agent)."
echo "      Project-local ./docs/ai-flow/flow-solutions/ (if present) overrides user-level per slug."
