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

CONFIG="${1:?site config is required}"
command -v kubectl >/dev/null 2>&1 || { echo "kubectl is required" >&2; exit 1; }
command -v yq >/dev/null 2>&1 || { echo "yq v4 is required" >&2; exit 1; }

NAMESPACE="$(yq -r '.deployment.namespace' "$CONFIG")"
RELEASE="$(yq -r '.deployment.releaseName' "$CONFIG")"
SELECTOR="app.kubernetes.io/instance=$RELEASE"

kubectl get nodes -o wide
kubectl --namespace "$NAMESPACE" get pods -o wide
kubectl --namespace "$NAMESPACE" wait \
  --for=condition=Available \
  deployment \
  --selector "$SELECTOR" \
  --timeout=15m

for deployment in frontend backend uav-access-service vision-inference pointcloud-analysis artifact-installer; do
  kubectl --namespace "$NAMESPACE" rollout status "deployment/$deployment" --timeout=10m
done

if [[ "$(yq -r '.helmValues.bundledDataServices.enabled' "$CONFIG")" == "true" ]]; then
  for statefulset in postgres redis kafka minio; do
    kubectl --namespace "$NAMESPACE" rollout status "statefulset/$statefulset" --timeout=15m
  done
fi

kubectl --namespace "$NAMESPACE" get ingress,service,pvc
echo "Cluster verification passed"
