#!/usr/bin/env bash
# List every run-stack command in a table (no workspace / Docker required).
# Usage: run-stack commands [--raw]
set -euo pipefail

PACKAGE_DIR="${PACKAGE_DIR:-${RUN_PACKAGE_DIR:-}}"
if [ -z "${PACKAGE_DIR}" ]; then
  PACKAGE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
fi
export PACKAGE_DIR RUN_PACKAGE_DIR="${RUN_PACKAGE_DIR:-${PACKAGE_DIR}}"
# shellcheck source=common.sh
source "${PACKAGE_DIR}/scripts/common.sh"

usage() {
  cat <<EOF
Usage: ${RUN_CMD:-run-stack} commands [--raw]

List every CLI command in a table (command, aliases, group, description).

  --raw        One canonical command name per line
  -h, --help   Show this help
EOF
}

# COMMAND \t ALIASES \t GROUP \t DESCRIPTION
# Aliases: space-separated; use — when none.
commands_catalog() {
  cat <<'EOF'
create	new	Global	Write .run/run.config.json and run init
migrate	—	Global	Convert legacy run/ to .run/ (config + env)
upgrade	update	Global	Refresh legacy run/ or self-update
self-update	—	Global	Install the latest run-stack from npm/volta/pnpm
completion	—	Global	Shell / Kiro autocompletion (bash|zsh|fig|install|services)
init	—	Setup	Write .run/run.config.json and .run/.env
init --update	—	Setup	Find and add apps added to the workspace since setup
up	run	Lifecycle	Build if needed and start the stack
down	—	Lifecycle	Stop everything, or named services (keeps data)
restart	—	Lifecycle	Stop then start (down + up); --build rebuilds images
rebuild	—	Lifecycle	Rebuild images from scratch and recreate containers
clean	—	Lifecycle	Stop and DELETE all volumes
commands	—	Inspection	List every CLI command in a table
apps	list	Inspection	List configured apps (backend, web, mobile, …)
services	—	Inspection	List compose services (and status when Docker is up)
ps	status	Inspection	Service status
ports	—	Inspection	Host ports + env URLs table
logs	—	Inspection	Follow logs
shell	sh	Inspection	Shell into a container (default: backend)
backend	—	Backend	Run any command inside the backend container
migrate	—	Backend	Run pending migrations
seed	—	Backend	Run database seeders
fresh	—	Backend	Rebuild the schema and seed it (destroys data)
artisan	—	Backend	php artisan … (BACKEND_STACK=laravel)
composer	—	Backend	composer … (BACKEND_STACK=laravel)
pnpm	—	Frontend	pnpm … inside the frontend workspace
ios	—	Mobile	Open mobile-client in the iOS Simulator
android	—	Mobile	Open mobile-client on an Android emulator
device	—	Mobile	Open on a USB phone, or print the Expo Go URL
mobile	—	Mobile	Restart mobile-deps / mobile-packages / Metro
reload	—	Mobile	Metro /status + /reload; --clear wipes caches (no Docker restart)
prebuild	—	Mobile	expo prebuild (Expo apps only)
sync-mobile-port	—	Mobile	Sync MOBILE_CLIENT_PORT into the mobile app configs
bundler	configure-bundler	Mobile	Point the simulator at Metro
desktop	—	Desktop	Launch the Electron / Tauri shell on the host
deploy	—	Deploy	Deploy web, mobile or desktop
dash	dashboard	Dashboard	Open the status dashboard in a browser
EOF
}

print_commands_table() {
  local rows line cmd aliases group desc
  rows="$(printf '%s\t%s\t%s\t%s' "COMMAND" "ALIASES" "GROUP" "DESCRIPTION")"
  while IFS=$'\t' read -r cmd aliases group desc; do
    [ -n "${cmd}" ] || continue
    rows="${rows}$(printf '\n%s\t%s\t%s\t%s' "${cmd}" "${aliases}" "${group}" "${desc}")"
  done < <(commands_catalog)
  _print_tsv_table "Commands" "${rows}"
}

print_commands_raw() {
  local cmd
  while IFS=$'\t' read -r cmd _rest; do
    [ -n "${cmd}" ] || continue
    printf '%s\n' "${cmd}"
  done < <(commands_catalog) | awk '!seen[$0]++'
}

case "${1:-}" in
  "" )
    print_commands_table
    ;;
  --raw)
    print_commands_raw
    ;;
  -h|--help|help)
    usage
    ;;
  *)
    echo "unknown option: $1" >&2
    usage >&2
    exit 1
    ;;
esac
