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

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

compose() {
  if docker compose version >/dev/null 2>&1; then
    docker compose "$@"
  else
    docker-compose "$@"
  fi
}

docker rm -f argus-node-b >/dev/null 2>&1 || true

pushd "$TEST_ROOT" >/dev/null
compose -p argus-sys down --remove-orphans || true
popd >/dev/null

echo "[INFO] Cleaning private directories..."
if [[ -d "$TEST_ROOT/private" ]]; then
  docker run --rm -v "$TEST_ROOT/private:/target" ubuntu:24.04 chown -R "$(id -u):$(id -g)" /target >/dev/null 2>&1 || true
  rm -rf "$TEST_ROOT/private"
fi
if [[ -d "$TEST_ROOT/private-nodea" ]]; then
  docker run --rm -v "$TEST_ROOT/private-nodea:/target" ubuntu:24.04 chown -R "$(id -u):$(id -g)" /target >/dev/null 2>&1 || true
  rm -rf "$TEST_ROOT/private-nodea"
fi
if [[ -d "$TEST_ROOT/private-nodeb" ]]; then
  docker run --rm -v "$TEST_ROOT/private-nodeb:/target" ubuntu:24.04 chown -R "$(id -u):$(id -g)" /target >/dev/null 2>&1 || true
  rm -rf "$TEST_ROOT/private-nodeb"
fi

rm -rf "$TEST_ROOT/tmp" "$TEST_ROOT/.env" || true

echo "[OK] Cleaned up system E2E"
