#!/usr/bin/env bash
# phase-banner.sh  -  print a phase / sub-phase progress banner to the terminal.
#
# Cross-CLI: works identically from Claude Code, Copilot CLI, or a plain shell.
# Claude Code ALSO uses TaskCreate/TaskUpdate for inline UI cards, but those
# are Claude-only  -  Copilot users wouldn't see anything without this script.
#
# Usage:
#   phase-banner.sh start <phase>           [name] [detail]
#   phase-banner.sh end   <phase> <status>  [name] [detail]
#   phase-banner.sh sub   <phase> <subN>    [name] [detail]
#   phase-banner.sh label <phase>                               (print canonical name)
#
# Examples:
#   phase-banner.sh start 4 "Review" "deterministic gates + parallel + triage"
#   phase-banner.sh start 4 auto                   # resolves to "Review" (en) or "İnceleme" (tr)
#   PHASE_LANG=tr phase-banner.sh start 4 auto     # "Faz 4: İnceleme"
#
# Status values for `end`: done | failed | skipped
#
# Localization:
#   PHASE_LANG=en (default) | tr  -  when set to tr, canonical labels resolve to
#   Turkish. The `auto` sentinel in place of [name] triggers a table lookup.
#   Canonical label set mirrors pipeline/multi-agent-refs/phases.md.
#
# Output is colored when stdout is a TTY (TERM != dumb). Plain otherwise so
# logs and CI artifacts stay readable. Always written to stdout  -  log
# capture is the caller's responsibility.

set -uo pipefail

PHASE_LANG="${PHASE_LANG:-en}"

# Canonical phase label table. Keep synced with
# pipeline/multi-agent-refs/phases.md and the table in
# pipeline/skills/shared/multi-agent/SKILL.md.
phase_label() {
  local phase="$1" lang="${2:-en}"
  case "$lang:$phase" in
    en:0) echo "Init" ;;
    en:1) echo "Analysis" ;;
    en:2) echo "Planning" ;;
    en:3) echo "Dev" ;;
    en:4) echo "Review" ;;
    en:5) echo "Test" ;;
    en:6) echo "Commit & PR" ;;
    en:7) echo "Report" ;;
    tr:0) echo "Başlangıç" ;;
    tr:1) echo "Analiz" ;;
    tr:2) echo "Planlama" ;;
    tr:3) echo "Geliştirme" ;;
    tr:4) echo "İnceleme" ;;
    tr:5) echo "Kullanıcı Testi" ;;
    tr:6) echo "Commit & PR" ;;
    tr:7) echo "Rapor" ;;
    *) echo "" ;;
  esac
}

# Localized word for "Phase" in banner header.
phase_word() {
  case "$1" in
    tr) echo "Faz" ;;
    *)  echo "Phase" ;;
  esac
}

if [ "$#" -lt 2 ]; then
  cat >&2 <<USAGE
usage:
  phase-banner.sh start <phase> [name|auto] [detail]
  phase-banner.sh end   <phase> <status: done|failed|skipped> [name|auto] [detail]
  phase-banner.sh sub   <phase> <subN> [name|auto] [detail]
  phase-banner.sh label <phase>                          (env: PHASE_LANG=en|tr)
USAGE
  exit 64
fi

ACTION="$1"; shift
PHASE="$1"; shift

# Short-circuit: `label` action just prints the canonical name and exits.
if [ "$ACTION" = "label" ]; then
  phase_label "$PHASE" "$PHASE_LANG"
  exit 0
fi

PHASE_WORD="$(phase_word "$PHASE_LANG")"

# Color helpers (skip if not a TTY or TERM=dumb).
if [ -t 1 ] && [ "${TERM:-}" != "dumb" ]; then
  C_RESET=$'\033[0m'
  C_DIM=$'\033[2m'
  C_BOLD=$'\033[1m'
  C_BLUE=$'\033[34m'
  C_GREEN=$'\033[32m'
  C_YELLOW=$'\033[33m'
  C_RED=$'\033[31m'
  C_PURPLE=$'\033[35m'
else
  C_RESET=""; C_DIM=""; C_BOLD=""
  C_BLUE=""; C_GREEN=""; C_YELLOW=""; C_RED=""; C_PURPLE=""
fi

ts() { date +"%H:%M:%S"; }

# Resolve `auto` sentinel in NAME to canonical label from phase_label().
resolve_name() {
  if [ "${1:-}" = "auto" ]; then
    phase_label "$PHASE" "$PHASE_LANG"
  else
    printf "%s" "${1:-}"
  fi
}

case "$ACTION" in
  start)
    NAME="$(resolve_name "${1:-}")"
    DETAIL="${2:-}"
    printf "\n${C_BOLD}${C_BLUE}┌─ %s %s%s ─────────────────────────────${C_RESET}\n" \
      "$PHASE_WORD" "$PHASE" "${NAME:+: $NAME}"
    printf "${C_BOLD}${C_BLUE}│${C_RESET} ${C_DIM}%s${C_RESET}  start\n" "$(ts)"
    [ -n "$DETAIL" ] && printf "${C_BOLD}${C_BLUE}│${C_RESET} %s\n" "$DETAIL"
    printf "${C_BOLD}${C_BLUE}└────────────────────────────────────────${C_RESET}\n"
    ;;

  end)
    STATUS="$1"; shift
    NAME="$(resolve_name "${1:-}")"
    DETAIL="${2:-}"
    case "$STATUS" in
      done)    COLOR="$C_GREEN";  GLYPH="✓" ;;
      failed)  COLOR="$C_RED";    GLYPH="✗" ;;
      skipped) COLOR="$C_YELLOW"; GLYPH="↷" ;;
      *)
        echo "phase-banner: unknown status '$STATUS' (use done|failed|skipped)" >&2
        exit 64 ;;
    esac
    printf "${C_BOLD}${COLOR}└─ %s %s%s  -  %s %s${C_RESET}" \
      "$PHASE_WORD" "$PHASE" "${NAME:+: $NAME}" "$GLYPH" "$STATUS"
    [ -n "$DETAIL" ] && printf " ${C_DIM}(%s)${C_RESET}" "$DETAIL"
    printf "  ${C_DIM}%s${C_RESET}\n\n" "$(ts)"
    ;;

  sub)
    SUB="$1"; shift
    NAME="$(resolve_name "${1:-}")"
    DETAIL="${2:-}"
    printf "${C_BOLD}${C_BLUE}│${C_RESET} ${C_PURPLE}└─ Sub %s.%s${C_RESET}" "$PHASE" "$SUB"
    [ -n "$NAME" ] && printf " ${C_BOLD}%s${C_RESET}" "$NAME"
    [ -n "$DETAIL" ] && printf "  ${C_DIM}%s${C_RESET}" "$DETAIL"
    printf "  ${C_DIM}%s${C_RESET}\n" "$(ts)"
    ;;

  *)
    echo "phase-banner: unknown action '$ACTION' (use start|end|sub|label)" >&2
    exit 64 ;;
esac
