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

# runmelast.sh — Pipeline infrastructure teardown
# Removes: Kargo → ArgoCD → Woodpecker (reverse install order)
# Preserves: namespaces, PVCs, TLS certs, NFS PVs, git-server, application namespaces

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

echo "=== Pipeline Infrastructure Teardown ==="
echo ""
echo "This will remove Kargo, ArgoCD, and Woodpecker CI."
echo "Preserved: namespaces, PVCs, TLS certs, NFS PVs, git-server, application namespaces."
echo ""

# -------------------------------------------------------
# 1. Remove Kargo resources + Helm release + CRDs
# -------------------------------------------------------
echo "--- Step 1: Removing Kargo ---"
kubectl delete -f kargo/stages/live.yaml --ignore-not-found || true
kubectl delete -f kargo/stages/paper.yaml --ignore-not-found || true
kubectl delete -f kargo/stages/beta.yaml --ignore-not-found || true
kubectl delete -f kargo/market-hours-check.yaml --ignore-not-found || true
kubectl delete -f kargo/warehouse.yaml --ignore-not-found || true
kubectl delete -f kargo/project-config.yaml --ignore-not-found || true
kubectl delete -f kargo/project.yaml --ignore-not-found || true
helm uninstall kargo --namespace kargo || true
kubectl delete crd freights.kargo.akuity.io projects.kargo.akuity.io stages.kargo.akuity.io \
  warehouses.kargo.akuity.io promotions.kargo.akuity.io promotiontasks.kargo.akuity.io \
  clusterpromotiontasks.kargo.akuity.io projectconfigs.kargo.akuity.io \
  clusterconfigs.kargo.akuity.io --ignore-not-found > /dev/null 2>&1 || true
echo "  ✓ Kargo removed"
echo ""

# -------------------------------------------------------
# 2. Remove ArgoCD resources + Helm release + CRDs
# -------------------------------------------------------
echo "--- Step 2: Removing ArgoCD ---"
kubectl delete -f argocd/apps/stonks-live.yaml --ignore-not-found || true
kubectl delete -f argocd/apps/stonks-paper.yaml --ignore-not-found || true
kubectl delete -f argocd/apps/stonks-beta.yaml --ignore-not-found || true
kubectl delete -f argocd/repo-secret.yaml --ignore-not-found || true
helm uninstall argocd --namespace argocd || true
kubectl delete crd applications.argoproj.io applicationsets.argoproj.io appprojects.argoproj.io \
  --ignore-not-found > /dev/null 2>&1 || true
echo "  ✓ ArgoCD removed"
echo ""

# -------------------------------------------------------
# 3. Remove Woodpecker CI
# -------------------------------------------------------
echo "--- Step 3: Removing Woodpecker CI ---"
kubectl delete -f woodpecker/agent-rbac.yaml --ignore-not-found || true
helm uninstall woodpecker --namespace woodpecker || true
echo "  ✓ Woodpecker CI removed"
echo ""

# NOTE: Namespaces are intentionally NOT deleted.
# PVCs, TLS certs, and proxy-ca-cert ConfigMaps survive for fast redeploy.
# runmefirst.sh uses idempotent creates (--dry-run=client | kubectl apply).

echo "=== Pipeline Infrastructure Teardown Complete ==="
echo ""
echo "Preserved:"
echo "  - Namespaces (woodpecker, argocd, kargo)"
echo "  - PVCs (Woodpecker DB, agent configs)"
echo "  - TLS certificates (Let's Encrypt rate limited)"
echo "  - NFS PVs and data"
echo "  - proxy-ca-cert ConfigMaps"
echo "  - git-server namespace (Gitea + registry)"
echo "  - Application namespaces (stonks-oracle, stonks-beta, stonks-paper)"
