#!/usr/bin/env bash
# Install the hand-authored `yad` BMAD module into the IDE skill dirs.
#
# Source of truth: {project-root}/skills/  (survives `bmad-method` updates).
# This script copies the sdlc-* skills into the four IDE locations the BMAD installer uses,
# and registers the module under _bmad/sdlc/. Re-run after any `bmad-method install/update`.
#
# Usage:  bash skills/sdlc/install.sh
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "$ROOT"

SKILLS=(yad-discovery yad-analysis yad-epic yad-architecture yad-ui yad-stories yad-test-cases yad-connect-repos yad-sync-repos yad-connect-design yad-connect-testing yad-connect-learning yad-connect-docs yad-docs yad-docs-overview yad-docs-sync yad-learn yad-spec yad-implement yad-checks yad-pr-template yad-hub-bridge yad-commit yad-open-pr yad-ship yad-engineer-review yad-backfill yad-run yad-review-gate yad-review-companion yad-pair-review yad-status yad-report yad-change yad-timeline yad-defects yad-reconcile yad-stub)

# Skills removed in a later release: this installer only refreshes names still in SKILLS, so a
# rerun would otherwise leave a dropped skill sitting in the IDE dirs. Purge any lingering copy
# (current name + any pre-rename alias) so the breaking removal takes effect for existing installs.
# Mirrors the CLI's REMOVED_SKILLS purge in `yad update`.
REMOVED_SKILLS=(yad-review-comments sdlc-review-comments)

echo "Installing sdlc module from $ROOT/skills ..."

for ide in .claude .agents .zencoder; do
  for s in "${REMOVED_SKILLS[@]}"; do
    rm -rf "$ide/skills/$s"
  done
done
if [ -d ".opencode/commands" ]; then
  for s in "${REMOVED_SKILLS[@]}"; do
    rm -f ".opencode/commands/$s.md"
  done
fi

# 1. Folder-per-skill IDEs: .claude, .agents, .zencoder
for ide in .claude .agents .zencoder; do
  for s in "${SKILLS[@]}"; do
    if [ -d "skills/$s" ]; then
      # Remove any stale install first, then copy the whole skill folder
      # (SKILL.md + references/). rm -rf guarantees no double-nesting on cp -R.
      rm -rf "$ide/skills/$s"
      cp -R "skills/$s" "$ide/skills/$s"
      echo "  $ide/skills/$s"
    fi
  done
done

# 2. opencode: single flat command file per skill (SKILL.md -> commands/<name>.md).
# Note: opencode commands are flat files, so the skills' references/ folders are
# NOT carried here. Each SKILL.md is self-contained for its operative rules
# (schema, gate predicate); references/ only adds supplementary detail.
if [ -d ".opencode/commands" ]; then
  for s in "${SKILLS[@]}"; do
    if [ -f "skills/$s/SKILL.md" ]; then
      cp "skills/$s/SKILL.md" ".opencode/commands/$s.md"
      echo "  .opencode/commands/$s.md (references/ not included — flat command)"
    fi
  done
fi

# 3. Register the module under _bmad/sdlc/ (regenerable; _bmad/ is rebuilt on update).
mkdir -p "_bmad/sdlc"
cp "skills/sdlc/config.yaml" "_bmad/sdlc/config.yaml"
cp "skills/sdlc/module-help.csv" "_bmad/sdlc/module-help.csv"
echo "  _bmad/sdlc/{config.yaml,module-help.csv}"

echo "Done. Skills available: ${SKILLS[*]}"
echo "Note: _bmad/ is regenerated by BMAD updates — re-run this script afterwards."
