#!/usr/bin/env bash
# banner: De Stijl Mondrian ASCII art banner for MindrianOS
# Usage: bash scripts/banner [version] [old_version]
#
# Responsive banner with 3 width tiers:
#   >= 100 cols: Full block-letter "MindrianOS" with 4 Mondrian zones
#   80-99 cols:  Compact "MOS" block letters with 2 Mondrian zones
#   < 80 cols:   Minimal single-line identity
#
# When old_version is provided, bottom bar shows version transition (v1.4.0 -> v1.5.1)

set -euo pipefail

# Width detection -- once per invocation (D-06)
COLS=${COLUMNS:-$(tput cols 2>/dev/null || echo 120)}

VERSION="${1:-}"
OLD_VERSION="${2:-}"

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"

if [ -z "$VERSION" ]; then
  # plan 85-06: read via platform.cjs (no python3 dependency on Windows).
  VERSION=$(node -e "try{process.stdout.write(require('${PLUGIN_ROOT}/lib/core/platform.cjs').readPluginJsonVersion('${PLUGIN_ROOT}'))}catch(e){process.stdout.write('unknown')}" 2>/dev/null || echo "")
fi

# plan 85-06 (WIN-FIX-H-02/H-03): platform-aware banner gating. On terminals
# that do not support UTF-8 (Windows cmd.exe / PowerShell without chcp 65001,
# or LANG=C locales), the block-letter banner renders as mojibake. Detect
# supportsUtf8 via platform.cjs and fall back to an ASCII-safe banner.
# BANNER_FORCE_ASCII=1 forces the fallback path for testing.
SUPPORTS_UTF8=$(node -e "try{var p=require('${PLUGIN_ROOT}/lib/core/platform.cjs');p.__resetForTests();process.stdout.write(p.detectPlatform().supportsUtf8?'1':'0')}catch(e){process.stdout.write('1')}" 2>/dev/null || echo "1")
if [ "${BANNER_FORCE_ASCII:-0}" = "1" ]; then
  SUPPORTS_UTF8="0"
fi

# De Stijl Mondrian palette -- 24-bit ANSI
RB=$'\e[48;2;166;61;47m'     # Red bg    #A63D2F
BB=$'\e[48;2;30;58;110m'     # Blue bg   #1E3A6E
YB=$'\e[48;2;200;164;60m'    # Yellow bg #C8A43C
TB=$'\e[48;2;42;107;94m'     # Teal bg   #2A6B5E
GB=$'\e[48;2;45;107;74m'     # Green bg  #2D6B4A
CF=$'\e[38;2;245;240;232m'   # Cream fg  #F5F0E8
MF=$'\e[38;2;160;154;144m'   # Muted fg  #A09A90
RS=$'\e[0m'                   # Reset

# Version display string
VER="v${VERSION}"
if [ -n "$OLD_VERSION" ]; then
  VER_TRANSITION="v${OLD_VERSION} -> v${VERSION}"
else
  VER_TRANSITION=""
fi

# Phase 121.5-05 Sub-plan F (SEED-007 absorption): stamp the explicit
# "MindrianOS v<version>" string at the top of the banner. The block-letter
# Mondrian art communicates identity visually; the version-of-record line
# answers the parseable question "what version am I running?" so the user can
# read it without typing `claude plugin list`.
# Routed through lib/core/first-touch-version-stamper.cjs so the stamp text is
# canonical across all six first-touch surfaces (Canon Part 7 reuse).
FT_STAMP=$(node -e "try{var s=require('${PLUGIN_ROOT}/lib/core/first-touch-version-stamper.cjs');process.stdout.write(s.stampVersion('banner','${VERSION}'))}catch(e){process.stdout.write('MindrianOS v'+'${VERSION}')}" 2>/dev/null || echo "MindrianOS v${VERSION}")
printf '\n  %s\n' "${FT_STAMP}"

# ============================================================================
# ASCII fallback banner (plan 85-06 / WIN-FIX-H-03)
# Rendered when platform.cjs reports supportsUtf8=false or code page is not
# 65001 (Windows cmd.exe / PowerShell without UTF-8, LANG=C locales).
# Same semantic content as the UTF-8 variant: identity, tagline, version.
# Box drawing replaced with +, -, |, =. No 24-bit ANSI colors because
# consoles that lack UTF-8 often lack truecolor support too.
# ============================================================================
if [ "$SUPPORTS_UTF8" = "0" ]; then
  if [ -n "$VER_TRANSITION" ]; then
    VDISPLAY="$VER_TRANSITION"
  else
    VDISPLAY="$VER"
  fi
  printf '\n'
  printf '  +============================================================================+\n'
  printf '  |                                                                            |\n'
  printf '  |                           M I N D R I A N - O S                            |\n'
  printf '  |                                                                            |\n'
  printf '  |                        Wicked problems. Navigated.                         |\n'
  printf '  |                                                                            |\n'
  printf '  +----------------------------------------------------------------------------+\n'
  VLEN=${#VDISPLAY}
  VPAD_TOTAL=$(( 76 - VLEN ))
  VLP=$(( VPAD_TOTAL / 2 ))
  VRP=$(( VPAD_TOTAL - VLP ))
  printf '  |%*s%s%*s|\n' "$VLP" '' "$VDISPLAY" "$VRP" ''
  printf '  +============================================================================+\n'
  printf '\n'
  printf '  mindrian.ai | Jonathan Sagir | linkedin.com/in/jonathansagir\n'
  printf '\n'
  exit 0
fi

if [ "$COLS" -ge 100 ]; then
  # ========== WIDE: Full MindrianOS block letters, 4 Mondrian zones ==========
  # 78 chars per row, split into: MIN(24) DRI(18) AN(18) OS(18)
  L1='███╗   ███╗██╗███╗   ██╗██████╗ ██████╗ ██╗ █████╗ ███╗   ██╗ ██████╗ ███████╗'
  L2='████╗ ████║██║████╗  ██║██╔══██╗██╔══██╗██║██╔══██╗████╗  ██║██╔═══██╗██╔════╝'
  L3='██╔████╔██║██║██╔██╗ ██║██║  ██║██████╔╝██║███████║██╔██╗ ██║██║   ██║███████╗'
  L4='██║╚██╔╝██║██║██║╚██╗██║██║  ██║██╔══██╗██║██╔══██║██║╚██╗██║██║   ██║╚════██║'
  L5='██║ ╚═╝ ██║██║██║ ╚████║██████╔╝██║  ██║██║██║  ██║██║ ╚████║╚██████╔╝███████║'
  L6='╚═╝     ╚═╝╚═╝╚═╝  ╚═══╝╚═════╝ ╚═╝  ╚═╝╚═╝╚═╝  ╚═╝╚═╝  ╚═══╝ ╚═════╝ ╚══════╝'

  # Bottom Mondrian bar: tagline(green, 45) + version(red, 33) = 78
  if [ -n "$VER_TRANSITION" ]; then
    VDISPLAY="$VER_TRANSITION"
  else
    VDISPLAY="$VER"
  fi
  VLEN=${#VDISPLAY}
  VLP=$(( (33 - VLEN) / 2 ))
  VRP=$(( 33 - VLEN - VLP ))
  VPAD=$(printf '%*s%s%*s' "$VLP" '' "$VDISPLAY" "$VRP" '')
  TPAD=$(printf '%9s%s%9s' '' 'Wicked problems. Navigated.' '')

  # Render the Mondrian composition
  printf '\n'
  for L in "$L1" "$L2" "$L3" "$L4" "$L5" "$L6"; do
    printf "  %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n" \
      "$RB" "$CF" "${L:0:24}"  "$RS" \
      "$BB" "$CF" "${L:24:18}" "$RS" \
      "$YB" "$CF" "${L:42:18}" "$RS" \
      "$TB" "$CF" "${L:60:18}" "$RS"
  done
  printf '\n'
  printf "  %s%s%s%s%s%s%s%s\n" \
    "$GB" "$CF" "$TPAD" "$RS" \
    "$RB" "$CF" "$VPAD" "$RS"
  printf '\n'
  printf "  %smindrian.ai | Jonathan Sagir | linkedin.com/in/jonathansagir%s\n" "$MF" "$RS"
  printf '\n'

elif [ "$COLS" -ge 80 ]; then
  # ========== COMPACT: "Mindrian-OS" block letters, 2 Mondrian zones ==========
  C1='███╗   ███╗██╗███╗   ██╗██████╗ ██████╗ ██╗ █████╗ ███╗   ██╗        ██████╗ ███████╗'
  C2='████╗ ████║██║████╗  ██║██╔══██╗██╔══██╗██║██╔══██╗████╗  ██║        ██╔═══██╗██╔════╝'
  C3='██╔████╔██║██║██╔██╗ ██║██║  ██║██████╔╝██║███████║██╔██╗ ██║█████╗  ██║   ██║███████╗'
  C4='██║╚██╔╝██║██║██║╚██╗██║██║  ██║██╔══██╗██║██╔══██║██║╚██╗██║╚════╝  ██║   ██║╚════██║'
  C5='██║ ╚═╝ ██║██║██║ ╚████║██████╔╝██║  ██║██║██║  ██║██║ ╚████║       ╚██████╔╝███████║'
  C6='╚═╝     ╚═╝╚═╝╚═╝  ╚═══╝╚═════╝ ╚═╝  ╚═╝╚═╝╚═╝  ╚═╝╚═╝  ╚═══╝        ╚═════╝ ╚══════╝'

  # Bottom bar: tagline(green, 52) + version(red, 33) = 85
  if [ -n "$VER_TRANSITION" ]; then
    VDISPLAY="$VER_TRANSITION"
  else
    VDISPLAY="$VER"
  fi
  VLEN=${#VDISPLAY}
  VLP=$(( (33 - VLEN) / 2 ))
  VRP=$(( 33 - VLEN - VLP ))
  VPAD=$(printf '%*s%s%*s' "$VLP" '' "$VDISPLAY" "$VRP" '')
  TPAD=$(printf '%12s%s%13s' '' 'Wicked problems. Navigated.' '')

  printf '\n'
  for L in "$C1" "$C2" "$C3" "$C4" "$C5" "$C6"; do
    printf "  %s%s%s%s%s%s%s%s%s\n" \
      "$RB" "$CF" "${L:0:42}" "$RS" \
      "$BB" "$CF" "${L:42}"   "$RS"
  done
  printf '\n'
  printf "  %s%s%s%s%s%s%s%s\n" \
    "$GB" "$CF" "$TPAD" "$RS" \
    "$RB" "$CF" "$VPAD" "$RS"
  printf '\n'
  printf "  %smindrian.ai | Jonathan Sagir | linkedin.com/in/jonathansagir%s\n" "$MF" "$RS"
  printf '\n'

else
  # ========== MINIMAL: Single line, no block art ==========
  if [ -n "$VER_TRANSITION" ]; then
    printf "  %s%s Mindrian-OS %s %s%s\n" "$RB" "$CF" "$VER_TRANSITION" "$RS" ""
  else
    printf "  %s%s Mindrian-OS %s %s%s\n" "$RB" "$CF" "$VER" "$RS" ""
  fi
  printf "  %sWicked problems. Navigated.%s\n" "$MF" "$RS"
fi
