#!/usr/bin/env bash
# harness-copilot — bash setup wrapper around bin/cli.js
#
# Usage:
#   ./setup                  # Interactive install into the current project
#   ./setup --yes            # Non-interactive defaults
#   ./setup --global         # Install prompts to VS Code User prompts folder
#   ./setup --skills audit,verify
#   ./setup --force          # Overwrite existing copilot-instructions.md/AGENTS.md/mcp.json
#   ./setup --help

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CLI="$SCRIPT_DIR/bin/cli.js"
VERSION="0.1.0"

if [ -t 1 ]; then
  RED='\033[0;31m'
  GREEN='\033[0;32m'
  BLUE='\033[0;34m'
  YELLOW='\033[0;33m'
  BOLD='\033[1m'
  NC='\033[0m'
else
  RED='' GREEN='' BLUE='' YELLOW='' BOLD='' NC=''
fi

info() { echo -e "${BLUE}ℹ${NC} $*"; }
ok()   { echo -e "${GREEN}✓${NC} $*"; }
warn() { echo -e "${YELLOW}⚠${NC} $*"; }
err()  { echo -e "${RED}✗${NC} $*" >&2; }

usage() {
  cat <<EOF
harness-copilot setup v${VERSION}

Usage: ./setup [OPTIONS]

Options:
  --yes, -y         Accept all defaults (non-interactive)
  --global          Install prompts to VS Code User prompts folder
  --skills <list>   Comma-separated skill dirs (default: all)
  --force           Overwrite existing copilot-instructions.md / AGENTS.md / mcp.json
  --help            Show this help message
EOF
  exit 0
}

if [ ! -f "$CLI" ]; then
  err "bin/cli.js not found at $CLI"
  err "Are you running this from the harness-copilot repo root?"
  exit 1
fi

if ! command -v node >/dev/null 2>&1; then
  err "node is not installed or not on PATH."
  err "Install Node 18+ from https://nodejs.org and retry."
  exit 1
fi

NODE_VERSION="$(node -v | sed 's/v//' | cut -d. -f1)"
if [ "${NODE_VERSION:-0}" -lt 18 ]; then
  err "Node 18+ is required (found $(node -v))."
  exit 1
fi

# Forward --help to the CLI's help message (which is the canonical source of truth).
case "${1:-}" in
  --help|-h) usage ;;
esac

info "harness-copilot v${VERSION}"
info "Running ${CLI} init $*"
echo ""

# Pass through arguments. The CLI handles interactive vs non-interactive itself.
exec node "$CLI" init "$@"
