#!/usr/bin/env bash
# ui/identity.module.sh — rendering for identity data
# All functions pure rendering — no writes, no state changes.

function ui::identity::header() {
  printf "  %-20s  %-7s  %s\n" "IDENTITY" "PEERS" "DEVICE TYPES"
  ui::divider 54
}

function ui::identity::row() {
  local name="${1:-}" peer_count="${2:-}" types="${3:-}"
  local types_display="${types//,/, }"
  [[ -z "$types_display" ]] && types_display="—"
  printf "  %-20s  %-7s  %s\n" "$name" "$peer_count" "$types_display"
}

function ui::identity::detail_name() {
  local name="${1:-}" peer_count="${2:-}"
  echo ""
  ui::row "Identity" "$name"
  ui::row "Peers"    "$peer_count"
  echo ""
}

function ui::identity::migrate_create() {
  local peer_name="${1:-}" identity_name="${2:-}" \
        peer_type="${3:-}" index="${4:-}"
  printf "  %s  %-22s → identity %-14s  %-10s  #%s\n" \
    "$(color::green "+")" "$peer_name" "$identity_name" "$peer_type" "$index"
}

function ui::identity::migrate_skip() {
  local peer_name="${1:-}"
  printf "  %s  %-22s  (no convention match)\n" \
    "$(color::dim "·")" "$peer_name"
}

function ui::identity::migrate_summary() {
  local created="${1:-0}" skipped="${2:-0}" dry_run="${3:-false}"
  echo ""
  if [[ "$dry_run" == "true" ]]; then
    log::info "Would create entries for ${created} peers (${skipped} skipped)"
  else
    log::ok "Created identity entries for ${created} peers (${skipped} skipped)"
  fi
}

function ui::identity::list_row_compact() {
  local name="${1:-}" peer_count="${2:-}" rules_list="${3:-}" policy="${4:-}"
  local peer_word="peers"
  [[ "$peer_count" -eq 1 ]] && peer_word="peer"

  local peers_col="${peer_count} ${peer_word}"
  local rules_val="-"
  [[ -n "$rules_list" ]] && rules_val="$rules_list"

  # Pad rules_val to fixed width before adding any ANSI
  local pad=16
  local rules_padded
  rules_padded=$(printf "%-${pad}s" "$rules_val")

  printf "  \033[1m%-20s\033[0m  %-10s  \033[2mrules:\033[0m %s  \033[2mpolicy:\033[0m %s\n" \
    "$name" "$peers_col" "$rules_padded" "$policy"
}
 
 
function ui::identity::list_row_table() {
  local name="${1:-}" peer_count="${2:-}" types="${3:-}"
  local types_display="${types//,/, }"
  [[ -z "$types_display" ]] && types_display="—"
  printf "  %-20s  %-7s  %s\n" "$name" "$peer_count" "$types_display"
}

function ui::identity::device_row() {
  local peer_name="${1:-}" dev_type="${2:-}" \
        dev_index="${3:-1}" status="${4:-}"
 
  # Extract connection state for dimming
  local is_online=false
  [[ "$status" == *"online"* ]] && is_online=true
 
  # Only show index suffix if peer name doesn't already encode it
  local suffix=""
  [[ "$dev_index" -gt 1 && "$peer_name" != *"-${dev_index}" ]] && \
    suffix=" (#${dev_index})"
 
  if $is_online; then
    printf "    · %-24s %-10s%s%s\n" \
      "$peer_name" "$dev_type" "$suffix" "$status"
  else
    local clean_status
    clean_status=$(echo "$status" | sed 's/\x1b\[[0-9;]*m//g')
    printf "    · %-24s %-10s%s\033[2m%s\033[0m\n" \
      "$peer_name" "$dev_type" "$suffix" "$clean_status"
  fi
}