#!/usr/bin/env bash
# 用途：查看所有服務的存取方式（網址、IP、Port）

set -euo pipefail

# 顏色定義
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

echo "=========================================="
echo "        K3s 服務存取清單"
echo "=========================================="
echo ""

# 節點 IP
echo "🖥️  節點 IP"
echo "------------------------------------------"
NODE_IPS=$(kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.addresses[?(@.type=="InternalIP")].address}{"\n"}{end}')
echo "$NODE_IPS"
echo ""

# Ingress (HTTP/HTTPS 服務)
echo "🌐 Web 服務 (Ingress)"
echo "------------------------------------------"
INGRESS_COUNT=$(kubectl get ingress --no-headers 2>/dev/null | wc -l)

if [ $INGRESS_COUNT -eq 0 ]; then
  echo -e "${YELLOW}無 Ingress 服務${NC}"
else
  kubectl get ingress --no-headers 2>/dev/null | while read -r name class hosts address ports age; do
    # 取得第一個 host
    FIRST_HOST=$(kubectl get ingress "$name" -o jsonpath='{.spec.rules[0].host}' 2>/dev/null)

    # 檢查是否有 TLS
    TLS=$(kubectl get ingress "$name" -o jsonpath='{.spec.tls}' 2>/dev/null)
    if [[ -n "$TLS" && "$TLS" != "[]" ]]; then
      PROTO="https"
    else
      PROTO="http"
    fi

    echo -e "${GREEN}→ $name${NC}"
    echo "   $PROTO://$FIRST_HOST"

    # 顯示所有 hosts
    ALL_HOSTS=$(kubectl get ingress "$name" -o jsonpath='{range .spec.rules[*]}{.host}{"\n"}{end}' 2>/dev/null)
    HOST_COUNT=$(echo "$ALL_HOSTS" | wc -l)
    if [ $HOST_COUNT -gt 1 ]; then
      echo "   其他域名: $(echo "$ALL_HOSTS" | tail -n +2 | tr '\n' ',' | sed 's/,$//')"
    fi
    echo ""
  done
fi

# Services
echo "🔧 內部服務 (ClusterIP)"
echo "------------------------------------------"
kubectl get svc --no-headers 2>/dev/null | grep "ClusterIP" | while read -r name type clusterip externalip ports age; do
  if [[ ! "$name" =~ "kube-" ]] && [[ ! "$name" =~ "svc-" ]]; then
    echo -e "$name ${CYAN}→${NC} $clusterip $ports"
  fi
done
echo ""

# NodePort (如果有)
echo "🔌 NodePort 服務"
echo "------------------------------------------"
NODEPORT_COUNT=$(kubectl get svc --no-headers 2>/dev/null | grep -c "NodePort" || true)

if [ $NODEPORT_COUNT -eq 0 ]; then
  echo -e "${YELLOW}無 NodePort 服務${NC}"
else
  kubectl get svc --no-headers 2>/dev/null | grep "NodePort" | while read -r name type clusterip externalip ports age; do
    echo -e "${GREEN}→ $name${NC}"
    echo "   Ports: $ports"
    echo "$NODE_IPS" | while read -r node ip; do
      echo "   $ip:$ports"
    done
    echo ""
  done
fi

# LoadBalancer (如果有)
echo "⚖️  LoadBalancer 服務"
echo "------------------------------------------"
LB_COUNT=$(kubectl get svc --no-headers 2>/dev/null | grep -c "LoadBalancer" || true)

if [ $LB_COUNT -eq 0 ]; then
  echo -e "${YELLOW}無 LoadBalancer 服務${NC}"
else
  kubectl get svc --no-headers 2>/dev/null | grep "LoadBalancer" | while read -r name type clusterip externalip ports age; do
    echo -e "${GREEN}→ $name${NC}"
    echo "   External: $externalip"
    echo "   Ports: $ports"
    echo ""
  done
fi

# Pod 概況
echo "📦 Pod 概況"
echo "------------------------------------------"
POD_TOTAL=$(kubectl get pods -A --no-headers 2>/dev/null | wc -l)
POD_RUNNING=$(kubectl get pods -A --no-headers 2>/dev/null | grep -c "Running" || true)
POD_ISSUE=$((POD_TOTAL - POD_RUNNING))

echo "總計: $POD_TOTAL 個 Pods"
echo -e "  ${GREEN}Running: $POD_RUNNING${NC}"
if [ $POD_ISSUE -gt 0 ]; then
  echo -e "  ${YELLOW}非 Running: $POD_ISSUE${NC}"
fi
echo ""

echo "=========================================="
