#!/usr/bin/env bash
set -Eeuo pipefail

usage() {
  cat <<'EOF'
Install zhctprompt project skills into the local Codex skill registry.

Usage:
  bash standards-stack/agent-skills/scripts/install_project_skills_to_codex.sh [--dry-run] [--target DIR]

Options:
  --dry-run      Show what would be copied without writing to ~/.codex/skills.
  --target DIR   Override the target skill directory. Default: $HOME/.codex/skills
  --help         Show this help.

This script copies only versioned project skills from:
  standards-stack/agent-skills/skills/

It does not copy personal memories, connectors, auth state, browser profiles,
tokens, or the whole ~/.codex directory.
EOF
}

DRY_RUN=0
TARGET_DIR="${HOME}/.codex/skills"

while [[ $# -gt 0 ]]; do
  case "$1" in
    --dry-run)
      DRY_RUN=1
      shift
      ;;
    --target)
      TARGET_DIR="${2:?--target requires a directory}"
      shift 2
      ;;
    --help|-h)
      usage
      exit 0
      ;;
    *)
      echo "[skill-install] unknown argument: $1" >&2
      usage >&2
      exit 2
      ;;
  esac
done

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_DIR="$(cd "${SCRIPT_DIR}/../skills" && pwd)"
REPORT_PATH="${HOME}/.codex/zhctprompt-project-skill-install-report.md"

if [[ ! -f "${SOURCE_DIR}/codex-team-skill-handoff/SKILL.md" ]]; then
  echo "[skill-install] source directory is not the zhctprompt project skills folder: ${SOURCE_DIR}" >&2
  exit 1
fi

command -v rsync >/dev/null 2>&1 || {
  echo "[skill-install] rsync is required on this Mac." >&2
  exit 1
}

SOURCE_COUNT="$(find "${SOURCE_DIR}" -mindepth 2 -maxdepth 2 -name SKILL.md -print | wc -l | tr -d ' ')"
if [[ "${DRY_RUN}" == "0" ]]; then
  mkdir -p "${TARGET_DIR}"
  mkdir -p "$(dirname "${REPORT_PATH}")"
elif [[ ! -d "${TARGET_DIR}" ]]; then
  echo "[skill-install] dry-run target does not exist; no directory was created: ${TARGET_DIR}" >&2
  exit 1
fi

RSYNC_OPTS=(
  -a
  --itemize-changes
  --exclude=.DS_Store
  --exclude=.git/
  --exclude=tmp/
  --exclude=.cache/
)

if [[ "${DRY_RUN}" == "1" ]]; then
  RSYNC_OPTS+=(--dry-run)
fi

echo "[skill-install] source: ${SOURCE_DIR}/"
echo "[skill-install] target: ${TARGET_DIR}/"
echo "[skill-install] source skill count: ${SOURCE_COUNT}"
echo "[skill-install] dry run: ${DRY_RUN}"

RSYNC_OUTPUT="$(rsync "${RSYNC_OPTS[@]}" "${SOURCE_DIR}/" "${TARGET_DIR}/")"
printf '%s\n' "${RSYNC_OUTPUT}"

TARGET_COUNT="$(find "${TARGET_DIR}" -mindepth 2 -maxdepth 2 -name SKILL.md -print 2>/dev/null | wc -l | tr -d ' ')"
STAMP="$(date '+%Y-%m-%d %H:%M:%S %z')"

if [[ "${DRY_RUN}" == "1" ]]; then
  echo "[skill-install] dry-run complete; no files were copied."
else
  {
    echo "# zhctprompt Project Skill Install Report"
    echo
    echo "- generated_at: ${STAMP}"
    echo "- source_dir: ${SOURCE_DIR}"
    echo "- target_dir: ${TARGET_DIR}"
    echo "- dry_run: ${DRY_RUN}"
    echo "- source_skill_count: ${SOURCE_COUNT}"
    echo "- target_skill_count_after_command: ${TARGET_COUNT}"
    echo
    echo "## rsync output"
    echo
    if [[ -n "${RSYNC_OUTPUT}" ]]; then
      printf '```text\n%s\n```\n' "${RSYNC_OUTPUT}"
    else
      echo "No changed files reported by rsync."
    fi
  } > "${REPORT_PATH}"
  echo "[skill-install] report: ${REPORT_PATH}"
  echo "[skill-install] install complete."
fi
