#!/usr/bin/env bash
# report-stray-test-lxcs.sh — list leftover test/scratch LXC entities.
#
# Provisioning experiments leave active `lxc:test-*` / `lxc:preflight-*`
# entities in the graph long after the containers are gone or repurposed.
# They generate checks and pollute health/graph views. This reports them and
# their DB state + which Proxmox host each is parented on, so the operator can
# confirm the container is really gone and retire the entity via the
# lifecycle-destroy-node runbook (a destructive, approval-gated action).
#
# Read-only. Pair with: lifecycle-destroy-node (mark destroyed) or
# lifecycle-deprecate-node.
set -euo pipefail

PSQL_CMD="${OIKOS_PSQL:-docker exec -i oikos-postgres-1 psql -U oikos -d oikos}"

echo "== stray test/scratch LXC entities =="
$PSQL_CMD -F ' | ' -Ac "
SELECT e.slug, COALESCE(e.state,'active') AS state,
       e.attributes->>'pve_id' AS pve_id,
       COALESCE(h.slug,'(no host)') AS host
FROM entities e
LEFT JOIN relationships r ON r.target_id = e.id AND r.type='hosts' AND r.valid_to IS NULL
LEFT JOIN entities h ON h.id = r.source_id
WHERE e.type='lxc' AND e.slug ~ '^lxc:(test|preflight)'
ORDER BY e.slug;"

echo
echo "Next: for each, confirm the container is gone in Proxmox (pct list on its"
echo "host), then retire via lifecycle-destroy-node (destructive) or mark"
echo "deprecated. If a container still exists, pct destroy it first."
