#!/usr/bin/env bash
# =============================================================================
# Finixo Gate — UI Preview (NO root, NO install, NO system changes)
# Shows how the main menu / colors / banners look on your terminal.
# Usage:  bash finixo-ui-preview.sh
# =============================================================================
export LC_ALL=C.UTF-8 2>/dev/null || true
set -uo pipefail

VERSION="3.2.5-preview"
NO_COLOR=${NO_COLOR:-}

# ---- colors (same spirit as Finixo Titan / gold theme) ----
init_colors() {
  R=""; G=""; Y=""; C=""; W=""; D=""; N=""; M=""; B=""; GOLD=""; BG=""
  [ -t 1 ] || return 0
  [ -z "${NO_COLOR:-}" ] || return 0
  [ "${TERM:-dumb}" = dumb ] && return 0
  N=$'\033[0m'
  B=$'\033[1m'
  D=$'\033[2m'
  R=$'\033[38;5;196m'
  G=$'\033[38;5;46m'
  Y=$'\033[38;5;220m'
  C=$'\033[38;5;51m'
  W=$'\033[38;5;255m'
  M=$'\033[38;5;213m'
  GOLD=$'\033[38;5;214m'
  BG=$'\033[48;5;234m'
}

init_colors

banner() {
  clear 2>/dev/null || true
  printf '%s\n' "${GOLD}${B}"
  cat <<'BAN'
  ███████╗██╗███╗   ██╗██╗██╗  ██╗ ██████╗
  ██╔════╝██║████╗  ██║██║╚██╗██╔╝██╔═══██╗
  █████╗  ██║██╔██╗ ██║██║ ╚███╔╝ ██║   ██║
  ██╔══╝  ██║██║╚██╗██║██║ ██╔██╗ ██║   ██║
  ██║     ██║██║ ╚████║██║██╔╝ ██╗╚██████╔╝
  ╚═╝     ╚═╝╚═╝  ╚═══╝╚═╝╚═╝  ╚═╝ ╚═════╝
BAN
  printf '%s\n' "${N}${GOLD}  ────────────────────────────────────────${N}"
  printf '  %sPremium Secure Tunnel%s  ·  %sFast · Stable · Private%s\n' "$W$B" "$N" "$D" "$N"
  printf '  %sUI Preview%s  v%s  %s(no root · no changes)%s\n' "$C" "$N" "$VERSION" "$D" "$N"
  printf '%s\n' "${GOLD}  ────────────────────────────────────────${N}"
  echo
}

ok()   { printf '  %s✓%s %s\n' "$G" "$N" "$*"; }
err()  { printf '  %s✗%s %s\n' "$R" "$N" "$*"; }
warn() { printf '  %s!%s %s\n' "$Y" "$N" "$*"; }
info() { printf '  %s·%s %s\n' "$C" "$N" "$*"; }

fake_status() {
  printf '  %s▸ LIVE STATUS%s (demo data)\n' "$GOLD$B" "$N"
  echo
  ok "nginx active"
  ok "xray active"
  ok "finixo-ssh-ws active"
  ok "haproxy active (edge)"
  info "Online (unique IPs · last 5 min): ${W}${B}2${N}"
  info "Accounts: VLESS=3  VMess=1  Trojan=2  SS=0  SSH=1"
  info "Edge: ${G}haproxy${N}  ·  TLS: ${G}OK${N}  ·  Paths: fixed CDN-style"
  echo
}

show_menu() {
  local items=(
    "INSTALL / REINSTALL"
    "CONNECTION LINKS"
    "TLS CERTIFICATE"
    "LIVE STATUS"
    "VLESS · WebSocket"
    "VLESS NON-TLS"
    "VMESS · WebSocket"
    "TROJAN · WebSocket"
    "SHADOWSOCKS 2022"
    "SSH · WebSocket"
    "CLIENT ENDPOINT"
    "RESTART SERVICES"
    "QUICK SELF-TEST"
    "DEEP AUDIT"
    "IRON TEST (STRICT)"
    "UPDATE XRAY-CORE"
    "REPAIR"
    "HARDEN"
    "UNINSTALL"
    "LIVE COUNTDOWN"
    "CONNECTION CONTROL"
    "Exit preview"
  )
  local i=0
  printf '  %s▸ MAIN MENU%s\n' "$GOLD$B" "$N"
  echo
  for label in "${items[@]}"; do
    if [ "$i" -eq $((${#items[@]} - 1)) ]; then
      printf '  %s[%2d]%s  %s%s%s\n' "$D" "$i" "$N" "$D" "$label" "$N"
    else
      printf '  %s[%2d]%s  %s\n' "$C" "$i" "$N" "$label"
    fi
    i=$((i + 1))
  done
  echo
  printf '  %s────────────────────────────────────────%s\n' "$GOLD" "$N"
  printf '  %sDemo only — nothing is installed or changed.%s\n' "$D" "$N"
  echo
}

show_conn_control() {
  printf '  %s▸ CONNECTION CONTROL%s (demo)\n' "$GOLD$B" "$N"
  echo
  printf '  %s━━ LIVE (can connect) ━━%s\n' "$G" "$N"
  printf '   1) %-8s %-16s %s● ONLINE (2 IPs)%s\n' "vless" "default" "$G" "$N"
  printf '   2) %-8s %-16s %s● ONLINE (1 IP)%s\n' "vmess" "shop-a" "$G" "$N"
  printf '   3) %-8s %-16s %s○ idle%s\n' "trojan" "trial1" "$D" "$N"
  echo
  printf '  %s━━ DISABLED (cannot connect) ━━%s\n' "$Y" "$N"
  printf '   (none)\n'
  echo
  info "Online (unique IPs · last 5 min): ${W}${B}3${N}"
  echo
  printf '  [1] Disable user (stop + kill sessions)\n'
  printf '  [2] Enable user  (restore access)\n'
  printf '  [3] Delete user permanently\n'
  printf '  [4] Kick sessions only (user stays active)\n'
  printf '  [5] Refresh list\n'
  printf '  [0] Back\n'
  echo
}

show_welcome_card() {
  printf '  %s▸ Client welcome card%s (what users see after connect)\n' "$GOLD$B" "$N"
  echo
  printf '  %s┌──────────────────────────────────────┐%s\n' "$C" "$N"
  printf '  %s│%s  🚀 Welcome to 🚀                    %s│%s\n' "$C" "$W$B" "$C" "$N"
  printf '  %s│%s         %sfinixo%s                        %s│%s\n' "$C" "$N" "$GOLD$B" "$N" "$C" "$N"
  printf '  %s│%s  Premium Secure Tunnel               %s│%s\n' "$C" "$D" "$C" "$N"
  printf '  %s│%s  Fast · Stable · Private             %s│%s\n' "$C" "$D" "$C" "$N"
  printf '  %s│%s                                      %s│%s\n' "$C" "$N" "$C" "$N"
  printf '  %s│%s  %s✅ Connected successfully%s           %s│%s\n' "$C" "$N" "$G" "$N" "$C" "$N"
  printf '  %s│%s  Please respect the fair-use rules 🤝 %s│%s\n' "$C" "$W" "$C" "$N"
  printf '  %s│%s  Made with 💙 by finixo              %s│%s\n' "$C" "$D" "$C" "$N"
  printf '  %s└──────────────────────────────────────┘%s\n' "$C" "$N"
  echo
}

show_links_demo() {
  printf '  %s▸ Sample links%s (fake — for layout only)\n' "$GOLD$B" "$N"
  echo
  printf '  %sVLESS-WS%s\n' "$C$B" "$N"
  printf '  %svless://xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx@example.com:443?encryption=none&security=tls&sni=example.com&type=ws&host=example.com&path=%%2Fcdn%%2Fassets%%2Fjs%%2Fvless.chunk.js#demo-VLESS%s\n' "$D" "$N"
  echo
  printf '  %sTrojan-WS%s\n' "$C$B" "$N"
  printf '  %strojan://password@example.com:443?security=tls&sni=example.com&type=ws&host=example.com&path=%%2Fcdn%%2Fassets%%2Fjs%%2Ftrojan.chunk.js#demo-Trojan%s\n' "$D" "$N"
  echo
}

pause() {
  echo
  printf '  %sPress Enter…%s' "$D" "$N"
  read -r _ || true
  echo
}

main() {
  while true; do
    banner
    fake_status
    show_menu
    printf '  %s>%s ' "$GOLD" "$N"
    read -r choice || exit 0
    case "${choice:-}" in
      3|status) banner; fake_status; pause ;;
      1|links)  banner; show_links_demo; pause ;;
      20|cc|control) banner; show_conn_control; pause ;;
      welcome|card) banner; show_welcome_card; pause ;;
      0|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19)
        banner
        warn "This is a UI preview only — option [$choice] is not executed."
        info "On a real root VPS, finixo would run the real action here."
        pause
        ;;
      21|q|quit|exit|"")
        echo
        info "Preview ended. No changes were made."
        exit 0
        ;;
      *)
        banner
        err "Unknown choice: $choice"
        info "Try: 1 links · 3 status · 20 connection control · welcome · 21 exit"
        pause
        ;;
    esac
  done
}

echo
info "Finixo UI Preview — colors depend on your terminal (truecolor recommended)."
info "No root required. No packages, configs, or services are touched."
pause
main

