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

SVC_DIR="$(cd "$(dirname "$0")/services" && pwd)"
export KUBECONFIG="${KUBECONFIG:-/etc/rancher/k3s/k3s.yaml}"

apply_wait() {
	for f in "$@"; do
		echo "${f%.yaml}"
		kubectl apply -f "${SVC_DIR}/${f}"
	done
	kubectl -n juwan wait --for=condition=Available deploy --all --timeout=600s || true
}

cd "$SVC_DIR"

apply_wait snowflake.yaml authz-adapter.yaml

domain_files=()
for f in *.yaml; do
	case "$f" in
		snowflake.yaml|authz-adapter.yaml|chat.yaml|email.yaml|frontend.yaml) ;;
		*) domain_files+=("$f") ;;
	esac
done
apply_wait "${domain_files[@]}"

apply_wait chat.yaml email.yaml frontend.yaml

kubectl get pods -n juwan
