#!/bin/bash
# scripts/setup-consumer.sh — 在当前消费产品 cwd 配置项目级 TVU skills
# Usage:
#   bash <path-to-tvu-clone>/scripts/setup-consumer.sh
#   或 alias: tvu-setup（见 docs/CONSUMER_ONBOARDING.md）
#   或在 Claude Code 里说 "在当前项目中使用 TVU 设计系统"（触发 setup-tvu-consumer skill 调用本脚本）

set -e

# Self-locate TVU clone root（脚本在 $TVU/scripts/ 下）
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
TVU="$(cd "$SCRIPT_DIR/.." && pwd)"

CWD="$(pwd)"

cat <<EOF

════════════════════════════════════════════════════════
  TVU 项目级 setup
  Source: $TVU
  Target: $CWD
════════════════════════════════════════════════════════

EOF

# Sanity checks
if [ ! -d "$TVU/skills" ] || [ ! -f "$TVU/.claude-plugin/plugin.json" ]; then
  echo "❌ 脚本必须在 TVU clone 的 scripts/ 目录下，且 TVU 路径含 skills/ + .claude-plugin/plugin.json" >&2
  exit 1
fi

if [ "$CWD" = "$TVU" ]; then
  echo "❌ 不能在 TVU clone 自身目录里跑（会自指 symlink 污染源仓库）" >&2
  exit 1
fi

# Create .claude/skills/ if missing
mkdir -p .claude/skills

# Symlink 11 skills
echo "▶ Symlinking skills (11)..."
for s in tvu-design-mockup tvu-design-code consumer-product-conventions \
         design-discovery design-walkthrough design-qa-loop \
         persona-simulation setup-tvu-consumer \
         role-ux role-translator shared-vocab-rules; do
  ln -sfn "$TVU/skills/$s" ".claude/skills/$s"
  echo "  ✓ $s"
done

# Symlink vocabulary
ln -sfn "$TVU/skills/shared-vocab-rules/vocabulary.md" ".claude/vocabulary.md"
echo "  ✓ vocabulary.md"

# Verify all symlinks resolve
BROKEN=$(find .claude/skills -maxdepth 1 -type l ! -exec test -e {} \; -print 2>/dev/null)
if [ -n "$BROKEN" ]; then
  echo ""
  echo "⚠️  以下 symlink broken（可能 TVU clone 路径 / skill 名变了）："
  echo "$BROKEN"
  exit 1
fi

cat <<EOF

✅ 完成 @ $CWD

在该 cwd 内可用唤醒词：
  @TVU mockup  / @TVU code
  @UX          / @翻译
  + 其它 TVU 团队 skill（discovery / walkthrough / qa-loop / persona / consumer-conventions）

离开该 cwd 这些唤醒词失效（项目级 scope）。
EOF
