#!/usr/bin/env bash
# One-shot install smoke for the inert Capacity Planner in an ephemeral
# single-node Kind cluster: build the local image, load it, install the Helm
# chart, wait for Deployment Ready, assert the release is deployed, then
# remove everything this run created.
#
# Run from the module root (internal/capacity-planner).
# Env: KIND, HELM, CONTAINER_TOOL, KIND_NODE_IMAGE, VERSION, COMMIT.
set -euo pipefail

KIND=${KIND:-bin/kind}
HELM=${HELM:-bin/helm}
CONTAINER_TOOL=${CONTAINER_TOOL:-docker}
KIND_NODE_IMAGE=${KIND_NODE_IMAGE:-kindest/node:v1.36.1@sha256:3489c7674813ba5d8b1a9977baea8a6e553784dab7b84759d1014dbd78f7ebd5}
VERSION=${VERSION:-v0.0.0}
COMMIT=${COMMIT:-unknown}

# Unique-per-run names; never touch user resources. KUBECONFIG is an
# exclusive per-run temp file (created only after preflight succeeds); the
# ambient kubeconfig is never read/written.
CLUSTER="capacity-planner-install-$$"
RELEASE="capacity-planner-$$"
NAMESPACE="capacity-planner-install-$$"
IMG_TAG="capacity-planner:install-$$"
CLUSTER_CREATED=0

cleanup() {
  # Helm is only invoked if this run actually created the Kind cluster.
  if [ "$CLUSTER_CREATED" = "1" ]; then
    "$HELM" uninstall "$RELEASE" -n "$NAMESPACE" --kubeconfig "$KUBECONFIG" --wait --timeout 60s >/dev/null 2>&1 || true
  fi
  "$KIND" delete cluster --name "$CLUSTER" --kubeconfig "$KUBECONFIG" >/dev/null 2>&1 || true
  "$CONTAINER_TOOL" rmi "$IMG_TAG" >/dev/null 2>&1 || true
  rm -f "$KUBECONFIG"
}

# Preflight: this run's cluster name and image tag must not exist. On a
# collision, fail immediately and do NOT delete the colliding objects.
if "$KIND" get clusters 2>/dev/null | grep -qx "$CLUSTER"; then
  echo "test/install: cluster '$CLUSTER' already exists; aborting (not deleting)" >&2
  exit 1
fi
if "$CONTAINER_TOOL" image inspect "$IMG_TAG" >/dev/null 2>&1; then
  echo "test/install: image '$IMG_TAG' already exists; aborting (not deleting)" >&2
  exit 1
fi

# The exclusive temp kubeconfig is created only after preflight, so a
# collision exit cannot leak a temp file. Trap registered here too:
# cleanup targets exactly the resources this run may have (partially)
# created and never masks the original exit code.
KUBECONFIG=$(mktemp)
trap 'rc=$?; cleanup; exit $rc' EXIT

echo "test/install: creating ephemeral Kind cluster '$CLUSTER'"
"$KIND" create cluster --name "$CLUSTER" --image "$KIND_NODE_IMAGE" --kubeconfig "$KUBECONFIG"
CLUSTER_CREATED=1

echo "test/install: building local image '$IMG_TAG'"
"$CONTAINER_TOOL" build -t "$IMG_TAG" \
  --build-arg GIT_VERSION="$VERSION" \
  --build-arg GIT_COMMIT="$COMMIT" \
  -f Dockerfile .

echo "test/install: loading image into '$CLUSTER'"
"$KIND" load docker-image --name "$CLUSTER" "$IMG_TAG"

echo "test/install: installing chart (wait for Deployment Ready)"
"$HELM" upgrade --install "$RELEASE" charts/capacity-planner \
  -n "$NAMESPACE" --create-namespace \
  --kubeconfig "$KUBECONFIG" \
  --set image.repository=capacity-planner \
  --set image.tag="install-$$" \
  --set image.pullPolicy=Never \
  --wait --timeout 120s

STATUS=$("$HELM" status "$RELEASE" -n "$NAMESPACE" --kubeconfig "$KUBECONFIG" -o json | sed -n 's/.*"status"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')
if [ "$STATUS" != "deployed" ]; then
  echo "test/install: release status is '$STATUS', want 'deployed'" >&2
  exit 1
fi

# Success path: explicit cleanup, then disable the trap, then assert no
# leftovers (assertions must not run through the failure trap).
cleanup
trap - EXIT

if "$KIND" get clusters 2>/dev/null | grep -qx "$CLUSTER"; then
  echo "test/install: leftover cluster '$CLUSTER' after cleanup" >&2
  exit 1
fi
if "$CONTAINER_TOOL" image inspect "$IMG_TAG" >/dev/null 2>&1; then
  echo "test/install: leftover image '$IMG_TAG' after cleanup" >&2
  exit 1
fi

echo "test/install: OK (install, wait, deployed status, full cleanup)"
