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

SCRIPT_DIR="$(pwd)"
export KUBECONFIG="$SCRIPT_DIR/../env/frigo-dev.yaml"

POD=$(kubectl get pod -n ai-env -l app=dc-backend \
  --field-selector=status.phase=Running \
  -o jsonpath='{.items[0].metadata.name}' 2>/dev/null || true)

if [ -z "$POD" ]; then
  echo "Kein laufender dc-backend Pod gefunden. Alle Pods:"
  kubectl get pods -n ai-env -l app=dc-backend
  exit 1
fi

echo "=== Logs: $POD ==="
kubectl logs "$POD" -n ai-env --tail $1
while true; do kubectl logs -f "$POD" -n ai-env  --tail 0 && break || sleep 2; done
kubectl logs -f "$POD" -n ai-env --tail $1
