#!/usr/bin/env bash
# Remove local dev skill symlinks. Restores global gstack as the active install.
set -e

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

removed=()

# ─── Clean up .claude/skills/ ─────────────────────────────────
CLAUDE_SKILLS="$REPO_ROOT/.claude/skills"
if [ -d "$CLAUDE_SKILLS" ]; then
  for link in "$CLAUDE_SKILLS"/*/; do
    name="$(basename "$link")"
    [ "$name" = "gstack" ] && continue
    if [ -L "${link%/}" ]; then
      rm "${link%/}"
      removed+=("claude/$name")
    fi
  done

  if [ -L "$CLAUDE_SKILLS/gstack" ]; then
    rm "$CLAUDE_SKILLS/gstack"
    removed+=("claude/gstack")
  fi

  rmdir "$CLAUDE_SKILLS" 2>/dev/null || true
fi

# ─── Clean up the untracked brain-aware render (bin/dev-setup step 7) ──
RENDER_DIR="$REPO_ROOT/.claude/gstack-rendered"
if [ -d "$RENDER_DIR" ]; then
  rm -rf "$RENDER_DIR"
  removed+=("claude/gstack-rendered")
fi
rmdir "$REPO_ROOT/.claude" 2>/dev/null || true

# ─── Clean up .agents/skills/ ────────────────────────────────
AGENTS_SKILLS="$REPO_ROOT/.agents/skills"
if [ -d "$AGENTS_SKILLS" ]; then
  for link in "$AGENTS_SKILLS"/*/; do
    name="$(basename "$link")"
    [ "$name" = "gstack" ] && continue
    if [ -L "${link%/}" ]; then
      rm "${link%/}"
      removed+=("agents/$name")
    fi
  done

  if [ -L "$AGENTS_SKILLS/gstack" ]; then
    rm "$AGENTS_SKILLS/gstack"
    removed+=("agents/gstack")
  fi

  rmdir "$AGENTS_SKILLS" 2>/dev/null || true
  rmdir "$REPO_ROOT/.agents" 2>/dev/null || true
fi

if [ ${#removed[@]} -gt 0 ]; then
  echo "Removed: ${removed[*]}"
else
  echo "No symlinks found."
fi
echo "Dev mode deactivated. Global gstack (~/.claude/skills/gstack) is now active."
