#!/bin/bash
# RTExit Verify — Shared Library
# Source this in every phase script

RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'
BLUE='\033[0;34m'; CYAN='\033[0;36m'; GRAY='\033[0;37m'; NC='\033[0m'
BOLD='\033[1m'

# Counters (shared across sourced scripts)
TOTAL=0; PASS=0; FAIL=0; WARN=0

# Check binary in PATH
chk() {
  local name="$1" cmd="${2:-$1}"
  TOTAL=$((TOTAL+1))
  if command -v "$cmd" >/dev/null 2>&1; then
    local ver
    ver=$(timeout 2 ${cmd} --version 2>/dev/null | head -1 | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?' | head -1)
    [ -n "$ver" ] && ver=" ${GRAY}(${ver})${NC}" || ver=""
    printf "  ${GREEN}✅${NC} %-35s%b\n" "$name" "$ver"
    PASS=$((PASS+1))
  else
    printf "  ${RED}❌${NC} %-35s ${RED}MISSING${NC}\n" "$name"
    FAIL=$((FAIL+1))
  fi
}

# Check Python module
chk_py() {
  local name="$1" module="${2:-$1}"
  TOTAL=$((TOTAL+1))
  if python3 -c "import ${module}" 2>/dev/null; then
    local ver
    ver=$(python3 -c "import ${module}; print(getattr(${module},'__version__',''))" 2>/dev/null | head -1)
    [ -n "$ver" ] && ver=" ${GRAY}(py ${ver})${NC}" || ver=""
    printf "  ${GREEN}✅${NC} %-35s%b\n" "$name" "$ver"
    PASS=$((PASS+1))
  else
    printf "  ${RED}❌${NC} %-35s ${RED}no module${NC}\n" "$name"
    FAIL=$((FAIL+1))
  fi
}

# Check directory (git repo / tool folder)
chk_dir() {
  local name="$1" path="$2"
  TOTAL=$((TOTAL+1))
  if [ -d "$path" ] && [ "$(ls -A "$path" 2>/dev/null)" ]; then
    local count
    count=$(find "$path" -maxdepth 1 -type f | wc -l)
    printf "  ${GREEN}✅${NC} %-35s ${GRAY}(%s files in %s)${NC}\n" "$name" "$count" "$path"
    PASS=$((PASS+1))
  else
    printf "  ${RED}❌${NC} %-35s ${RED}not found: %s${NC}\n" "$name" "$path"
    FAIL=$((FAIL+1))
  fi
}

# Check file exists
chk_file() {
  local name="$1" path="$2"
  TOTAL=$((TOTAL+1))
  if [ -f "$path" ]; then
    printf "  ${GREEN}✅${NC} %-35s ${GRAY}(%s)${NC}\n" "$name" "$path"
    PASS=$((PASS+1))
  else
    printf "  ${RED}❌${NC} %-35s ${RED}not found: %s${NC}\n" "$name" "$path"
    FAIL=$((FAIL+1))
  fi
}

# Optional tool (warning, not fail)
chk_opt() {
  local name="$1" cmd="${2:-$1}"
  TOTAL=$((TOTAL+1))
  if command -v "$cmd" >/dev/null 2>&1 || python3 -c "import ${cmd}" 2>/dev/null; then
    printf "  ${GREEN}✅${NC} %-35s ${GRAY}(optional)${NC}\n" "$name"
    PASS=$((PASS+1))
  else
    printf "  ${YELLOW}⚠️ ${NC} %-35s ${YELLOW}optional — not installed${NC}\n" "$name"
    WARN=$((WARN+1))
  fi
}

# Print phase header
phase_header() {
  echo ""
  printf "${CYAN}${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}\n"
  printf "${CYAN}${BOLD}  %s${NC}\n" "$1"
  printf "${CYAN}${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}\n"
}

# Print section header
section() {
  echo ""
  printf "  ${BLUE}── %s ──${NC}\n" "$1"
}

# Print phase summary
phase_summary() {
  local pct=0
  [ "$TOTAL" -gt 0 ] && pct=$((PASS*100/TOTAL))
  echo ""
  printf "  ${BOLD}Result: ${GREEN}%d✅${NC} ${RED}%d❌${NC} ${YELLOW}%d⚠️${NC}  /  %d total  →  " "$PASS" "$FAIL" "$WARN" "$TOTAL"
  if   [ "$pct" -ge 90 ]; then printf "${GREEN}${BOLD}%d%%${NC}\n" "$pct"
  elif [ "$pct" -ge 70 ]; then printf "${YELLOW}${BOLD}%d%%${NC}\n" "$pct"
  else printf "${RED}${BOLD}%d%%${NC}\n" "$pct"
  fi
}
