#!/usr/bin/env bash
# AY Framework -- Team Init
# Adds framework awareness to a shared repo so team members can install.
# Two modes: "optional" (recommended) or "required" (enforced via hook).
# Works on Mac, Linux, Windows (Git Bash).

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
FRAMEWORK_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
VERSION="$(node -p "require('$FRAMEWORK_DIR/package.json').version" 2>/dev/null || cat "$FRAMEWORK_DIR/VERSION" 2>/dev/null || echo "unknown")"

print_header() {
  echo ""
  echo "  AY Framework v${VERSION} -- Team Init"
  echo "  ─────────────────────────────────────────────────"
  echo ""
}

print_usage() {
  echo "Usage: ayf-team-init [optional|required]"
  echo ""
  echo "  optional  -- Add recommended install note to CLAUDE.md"
  echo "  required  -- Add required install note + enforcement hook"
  echo ""
}

CLAUDE_MD="CLAUDE.md"
AYF_MARKER="## AY Framework"

# Check if CLAUDE.md already has AY Framework section
has_ayf_section() {
  if [ -f "$CLAUDE_MD" ]; then
    grep -q "$AYF_MARKER" "$CLAUDE_MD" 2>/dev/null && return 0
  fi
  return 1
}

inject_optional() {
  local section
  section=$(cat <<'SECTION'

## AY Framework (recommended)

This project uses the AY Framework for agent-swarm coordination.

Install it to get tracking files, agent templates, and commit hooks:

```bash
npx ay-framework --local
```

Without the framework, Claude Code still works normally.
With the framework, agents coordinate through shared tracking files.

SECTION
)

  if [ ! -f "$CLAUDE_MD" ]; then
    echo "# CLAUDE.md" > "$CLAUDE_MD"
  fi

  echo "$section" >> "$CLAUDE_MD"
  echo "  [+] Added optional AY Framework section to CLAUDE.md"
}

inject_required() {
  local section
  section=$(cat <<'SECTION'

## AY Framework (REQUIRED)

This project requires the AY Framework for agent-swarm coordination.

Install before starting work:

```bash
npx ay-framework --local --yes
```

The framework provides:
- Task tracking in .ay/tracking/
- Agent coordination via HANDOFFS.md
- Commit guards via pre-commit hook
- Standardized agent and task templates

Do not skip installation. A hook will block work if the framework is missing.

SECTION
)

  if [ ! -f "$CLAUDE_MD" ]; then
    echo "# CLAUDE.md" > "$CLAUDE_MD"
  fi

  echo "$section" >> "$CLAUDE_MD"
  echo "  [+] Added required AY Framework section to CLAUDE.md"
}

create_enforcement_hook() {
  local hooks_dir=".claude/hooks"
  mkdir -p "$hooks_dir"

  cat > "$hooks_dir/check-ayf.sh" <<'HOOKSCRIPT'
#!/usr/bin/env bash
# AY Framework enforcement hook
# Blocks work if framework is not installed in this project.

set -euo pipefail

if [ ! -d ".ay" ] || [ ! -f ".ay/state.json" ]; then
  echo '{"decision":"deny","message":"AY Framework is required but not installed. Run: npx ay-framework --local --yes"}'
  exit 0
fi

echo '{"decision":"allow"}'
HOOKSCRIPT

  chmod +x "$hooks_dir/check-ayf.sh" 2>/dev/null || true
  echo "  [+] Created enforcement hook at $hooks_dir/check-ayf.sh"
}

# ── Main ──

print_header

MODE="${1:-}"

if [ -z "$MODE" ]; then
  print_usage
  exit 1
fi

if has_ayf_section; then
  echo "  CLAUDE.md already contains an AY Framework section."
  echo "  Remove it manually first if you want to re-initialize."
  exit 1
fi

case "$MODE" in
  optional)
    inject_optional
    echo ""
    echo "  Done. Team members will see the install recommendation in CLAUDE.md."
    echo ""
    ;;
  required)
    inject_required
    create_enforcement_hook
    echo ""
    echo "  Done. Team members must install the framework before working."
    echo "  The hook at .claude/hooks/check-ayf.sh enforces this."
    echo ""
    ;;
  *)
    print_usage
    exit 1
    ;;
esac
