#!/usr/bin/env bash
# ui/group.module.sh — rendering for groups

# ======================================================
# List rendering
# ======================================================

# ui::group::list_row <name> <desc> <total> <blocked> <w_name> <w_desc>
function ui::group::list_row() {
  local name="${1:-}" desc="${2:-}" total="${3:-0}" blocked="${4:-0}" \
        w_name="${5:-16}" w_desc="${6:-30}"

  local name_pad desc_val desc_pad_n
  name_pad=$(printf "%-${w_name}s" "$name")
  desc_val="${desc:--}"
  desc_pad_n=$(( w_desc - ${#desc_val} ))
  [[ $desc_pad_n -lt 0 ]] && desc_pad_n=0

  # Peer count — dim if zero
  local peers_word="peers"
  [[ "$total" -eq 1 ]] && peers_word="peer"
  local peers_display
  if [[ "$total" -eq 0 ]]; then
    peers_display="\033[2m0 ${peers_word}\033[0m"
  else
    peers_display="${total} ${peers_word}"
  fi
  local peers_pad
  peers_pad=$(printf "%-10s" "${total} ${peers_word}")

  # Status
  local status_color status_str
  IFS='|' read -r status_color status_str <<< "$(ui::group::status "$total" "$blocked")"

  local peers_str="${total} ${peers_word}"
  local peers_pad_n=$(( 10 - ${#peers_str} ))
  [[ $peers_pad_n -lt 0 ]] && peers_pad_n=0

  if [[ "$total" -eq 0 ]]; then
    printf "  \033[2m%s  %s%*s  %s%*s  %s\033[0m\n" \
      "$name_pad" "$desc_val" "$desc_pad_n" "" \
      "$peers_str" "$peers_pad_n" "" "inactive"
  else
    printf "  %s  %s%*s  %s%*s  %b%s\033[0m\n" \
      "$name_pad" "$desc_val" "$desc_pad_n" "" \
      "$peers_str" "$peers_pad_n" "" \
      "$status_color" "$status_str"
  fi
}

# Table version (kept for future display config)
function ui::group::list_header_table() {
  printf "\n  %-20s %-35s %-8s %s\n" "NAME" "DESCRIPTION" "PEERS" "STATUS"
  printf "  %s\n" "$(printf '─%.0s' {1..75})"
}

function ui::group::list_row_table() {
  local name="${1:-}" desc="${2:-}" total="${3:-0}" blocked="${4:-0}"
  local status_color="" status_str="active"
  if [[ "$total" -gt 0 ]]; then
    if [[ "$blocked" -eq "$total" ]]; then
      status_color="\033[1;31m"; status_str="blocked"
    elif [[ "$blocked" -gt 0 ]]; then
      status_color="\033[1;33m"; status_str="blocked (${blocked}/${total})"
    else
      status_color="\033[1;32m"; status_str="active"
    fi
  fi
  printf "  %-20s %-35s %-8s %b\n" \
    "$name" "${desc:-—}" "$total" \
    "${status_color}${status_str}\033[0m"
}

# ======================================================
# Show rendering
# ======================================================

# ui::group::show_member_row <name> <ip> <rule> <is_blocked> <w_name> <w_ip>
function ui::group::show_member_row() {
  local name="${1:-}" ip="${2:-}" rule="${3:--}" is_blocked="${4:-false}" \
        w_name="${5:-22}" w_ip="${6:-14}"

  local name_pad ip_pad rule_pad
  name_pad=$(printf "%-${w_name}s" "$name")
  ip_pad=$(printf   "%-${w_ip}s"   "$ip")
  rule_pad=$(printf "%-12s"        "${rule:--}")

  local status_color status_str
  if [[ "$is_blocked" == "true" ]]; then
    status_color="\033[1;31m"; status_str="blocked"
  else
    status_color="\033[1;32m"; status_str="active"
  fi

  printf "  %s  %s  \033[2mrule:\033[0m %s  %b%s\033[0m\n" \
    "$name_pad" "$ip_pad" "$rule_pad" \
    "$status_color" "$status_str"
}

# Table version
function ui::group::show_header_table() {
  printf "\n  %-28s %-15s %-12s %s\n" "NAME" "IP" "RULE" "STATUS"
  printf "  %s\n" "$(printf '─%.0s' {1..65})"
}

function ui::group::show_member_row_table() {
  local name="${1:-}" ip="${2:-}" rule="${3:--}" is_blocked="${4:-false}"
  local status_color status_str
  [[ "$is_blocked" == "true" ]] && { status_color="\033[1;31m"; status_str="blocked"; } \
                                 || { status_color="\033[1;32m"; status_str="active"; }
  printf "  %-28s %-15s %-12s %b\n" \
    "$name" "$ip" "${rule:--}" "${status_color}${status_str}\033[0m"
}

# ui::group::show_peers <peers_list_nameref> <w_name> <w_ip>
function ui::group::show_peers() {
  local -n _peers_list="$1"
  local w_name="${2:-16}" w_ip="${3:-13}"
 
  if [[ ${#_peers_list[@]} -eq 0 || -z "${_peers_list[0]:-}" ]]; then
    printf "  \033[2m—\033[0m\n"
    return 0
  fi
 
  for peer_name in "${_peers_list[@]}"; do
    [[ -z "$peer_name" ]] && continue
 
    if ! peers::require_exists "$peer_name" > /dev/null 2>&1; then
      printf "  \033[2m%-${w_name}s  (no longer exists)\033[0m\n" "$peer_name"
      continue
    fi
 
    local ip rule is_blocked
    ip=$(peers::get_ip "$peer_name")
    rule=$(peers::get_meta "$peer_name" "rule")
    peers::is_blocked "$peer_name" 2>/dev/null && is_blocked="true" || is_blocked="false"
 
    ui::group::show_member_row "$peer_name" "$ip" "${rule:--}" \
      "$is_blocked" "$w_name" "$w_ip"
  done
}


# ======================================================
# Helpers
# ======================================================

# ui::group::status_color <total> <blocked>
# Returns color code and status string for a group
# Usage: IFS='|' read -r color str <<< "$(ui::group::status "$total" "$blocked")"
function ui::group::status() {
  local total="${1:-0}" blocked="${2:-0}"
  if [[ "$total" -eq 0 ]]; then
    echo "\033[2;37m|inactive"
  elif [[ "$blocked" -eq "$total" ]]; then
    echo "\033[1;31m|blocked"
  elif [[ "$blocked" -gt 0 ]]; then
    echo "\033[1;33m|partial (${blocked}/${total})"
  else
    echo "\033[1;32m|active"
  fi
}