#!/usr/bin/env bash
# Pre-run cleanup for galaxy/integration. Idempotent and safe to call
# repeatedly; runs before each integration test session to wipe state
# left over from earlier runs.
#
# What we touch:
#   1. Containers labelled `org.testcontainers=true` — every container
#      brought up by testcontainers-go (our backend/gateway/game plus
#      postgres/redis/mailpit/ryuk service containers).
#   2. Containers labelled `galaxy.backend=1` — engine instances spawned
#      by backend's runtime adapter on the host Docker daemon (see
#      `backend/internal/dockerclient/types.go`). These do not carry
#      the testcontainers label because backend, not testcontainers,
#      creates them.
#   3. Networks labelled `org.testcontainers=true` — networks created
#      by testcontainers-go for cross-container wiring.
#   4. Images labelled `galaxy.test.kind=integration-image` — local
#      builds of galaxy/{backend,gateway,game}:integration. Pulled
#      service images (postgres, redis, ryuk, mailpit) are NOT touched
#      so the cache stays warm between runs.
#
# What we never touch:
#   - Containers / images without one of the labels above.
#   - User-managed images and volumes.

set -euo pipefail

remove_containers_with_label() {
    local label="$1"
    local description="$2"
    local ids
    ids=$(docker ps -aq --filter "label=$label" 2>/dev/null || true)
    if [ -z "$ids" ]; then
        return
    fi
    local count
    count=$(printf '%s\n' "$ids" | wc -l | tr -d ' ')
    echo "preclean: removing $count $description"
    # shellcheck disable=SC2086
    docker rm -f $ids >/dev/null 2>&1 || true
}

remove_networks_with_label() {
    local label="$1"
    local description="$2"
    local ids
    ids=$(docker network ls -q --filter "label=$label" 2>/dev/null || true)
    if [ -z "$ids" ]; then
        return
    fi
    local count
    count=$(printf '%s\n' "$ids" | wc -l | tr -d ' ')
    echo "preclean: removing $count $description"
    # shellcheck disable=SC2086
    docker network rm $ids >/dev/null 2>&1 || true
}

remove_images_with_label() {
    local label="$1"
    local description="$2"
    local ids
    ids=$(docker images -q --filter "label=$label" 2>/dev/null || true)
    if [ -z "$ids" ]; then
        return
    fi
    local count
    count=$(printf '%s\n' "$ids" | sort -u | wc -l | tr -d ' ')
    echo "preclean: removing $count $description"
    # shellcheck disable=SC2086
    docker rmi -f $ids >/dev/null 2>&1 || true
}

if ! command -v docker >/dev/null 2>&1; then
    echo "preclean: docker CLI not found, nothing to do" >&2
    exit 0
fi

if ! docker info >/dev/null 2>&1; then
    echo "preclean: docker daemon unreachable, nothing to do" >&2
    exit 0
fi

remove_containers_with_label "org.testcontainers=true" "testcontainers-managed containers"
remove_containers_with_label "galaxy.backend=1"       "backend-managed engine containers"
remove_networks_with_label   "org.testcontainers=true" "testcontainers-managed networks"
remove_images_with_label     "galaxy.test.kind=integration-image" "integration-built images"

echo "preclean: done"
