#!/usr/bin/env bash
set -euo pipefail

json_escape() {
  sed 's/\\/\\\\/g; s/"/\\"/g'
}

get_temp_c() {
  if command -v vcgencmd >/dev/null 2>&1; then
    vcgencmd measure_temp 2>/dev/null | sed -E "s/temp=([0-9.]+)'C/\1/" || echo "na"
  elif [ -f /sys/class/thermal/thermal_zone0/temp ]; then
    awk '{printf "%.1f", $1/1000}' /sys/class/thermal/thermal_zone0/temp 2>/dev/null || echo "na"
  else
    echo "na"
  fi
}

get_disk_root_pct() {
  df -P / 2>/dev/null | awk 'NR==2 {gsub("%","",$5); print $5}' || echo "na"
}

get_disk_opt_hub_pct() {
  df -P /opt/hub 2>/dev/null | awk 'NR==2 {gsub("%","",$5); print $5}' || echo "na"
}

get_load_1m() {
  awk '{print $1}' /proc/loadavg 2>/dev/null || echo "na"
}

get_uptime_human() {
  uptime -p 2>/dev/null | sed 's/^up //' || echo "na"
}

get_ram_used_pct() {
  free 2>/dev/null | awk '/Mem:/ {if ($2 > 0) printf "%.0f", ($2-$7)/$2 * 100; else print "na"}' || echo "na"
}

get_cpu_usage_pct() {
  local v
  v="$(top -bn1 2>/dev/null | awk '/%?Cpu\(s\)/ {print int($2 + $4); found=1} END {if (!found) print "na"}')"
  printf '%s' "${v:-na}"
}

get_wg_ip() {
  ip -o -4 addr show dev wg0 2>/dev/null | awk '{print $4}' | cut -d/ -f1 | head -n1 || true
}

get_wg_status() {
  if ! ip link show dev wg0 >/dev/null 2>&1; then
    echo "down"
    return
  fi

  if ! ip link show dev wg0 | head -n1 | grep -q "<.*UP.*>"; then
    echo "down"
    return
  fi

  if wg show wg0 2>/dev/null | grep -q "latest handshake:"; then
    echo "ok"
  else
    echo "up-no-handshake"
  fi
}

get_wg_peer_count() {
  local v
  v="$(wg show wg0 peers 2>/dev/null | wc -l | tr -d '[:space:]')"
  printf '%s' "${v:-0}"
}

get_docker_status() {
  if ! systemctl is-active --quiet docker 2>/dev/null; then
    echo "down"
  else
    echo "ok"
  fi
}

get_container_status() {
  local name="$1"
  local status
  status="$(docker inspect -f '{{if .State.Running}}{{if .State.Health}}{{.State.Health.Status}}{{else}}running{{end}}{{else}}stopped{{end}}' "$name" 2>/dev/null || true)"
  if [ -z "$status" ]; then
    echo "missing"
  else
    echo "$status"
  fi
}

get_backup_status() {
  if [ -f /opt/hub/backups/last_success ]; then
    echo "ok"
  else
    echo "missing"
  fi
}

# --- Récupération des valeurs (Alignées avec le JSON) ---

HOSTNAME_VAL="$(hostname 2>/dev/null || echo "unknown")"
WG_IP_VAL="$(get_wg_ip)"
UPTIME_VAL="$(get_uptime_human)"
LOAD1_VAL="$(get_load_1m)"
CPU_USAGE_PCT_VAL="$(get_cpu_usage_pct)"
RAM_USED_PCT_VAL="$(get_ram_used_pct)"
DISK_ROOT_PCT_VAL="$(get_disk_root_pct)"
DISK_OPT_HUB_PCT_VAL="$(get_disk_opt_hub_pct)"
TEMP_C_VAL="$(get_temp_c)"
WG_STATUS_VAL="$(get_wg_status)"
WG_PEER_COUNT_VAL="$(get_wg_peer_count)"
BACKUP_STATUS_VAL="$(get_backup_status)"
DOCKER_STATUS_VAL="$(get_docker_status)"
NPM_STATUS_VAL="$(get_container_status npm)"
PIHOLE_STATUS_VAL="$(get_container_status pihole)"

# --- Construction du JSON ---

printf '{'
printf '"hostname":"%s",' "$(printf '%s' "$HOSTNAME_VAL" | json_escape)"
printf '"wg_ip":"%s",' "$(printf '%s' "$WG_IP_VAL" | json_escape)"
printf '"uptime":"%s",' "$(printf '%s' "$UPTIME_VAL" | json_escape)"
printf '"load_1m":"%s",' "$(printf '%s' "$LOAD1_VAL" | json_escape)"
printf '"cpu_usage_pct":"%s",' "$(printf '%s' "$CPU_USAGE_PCT_VAL" | json_escape)"
printf '"ram_used_pct":"%s",' "$(printf '%s' "$RAM_USED_PCT_VAL" | json_escape)"
printf '"disk_root_pct":"%s",' "$(printf '%s' "$DISK_ROOT_PCT_VAL" | json_escape)"
printf '"disk_opt_hub_pct":"%s",' "$(printf '%s' "$DISK_OPT_HUB_PCT_VAL" | json_escape)"
printf '"temp_c":"%s",' "$(printf '%s' "$TEMP_C_VAL" | json_escape)"
printf '"wg_status":"%s",' "$(printf '%s' "$WG_STATUS_VAL" | json_escape)"
printf '"wg_peer_count":"%s",' "$(printf '%s' "$WG_PEER_COUNT_VAL" | json_escape)"
printf '"backup_status":"%s",' "$(printf '%s' "$BACKUP_STATUS_VAL" | json_escape)"
printf '"docker_status":"%s",' "$(printf '%s' "$DOCKER_STATUS_VAL" | json_escape)"
printf '"npm_status":"%s",' "$(printf '%s' "$NPM_STATUS_VAL" | json_escape)"
printf '"pihole_status":"%s"' "$(printf '%s' "$PIHOLE_STATUS_VAL" | json_escape)"
printf '}\n'