#!/usr/bin/env bash
# Run on the server from repo root with the stack up:
#   bash scripts/check-prod-stack.sh
# Verifies Postgres, MinIO, and be0 /health; be0 → postgres:5432 and minio:9000 reachability.
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
COMPOSE=(docker compose --env-file "${ROOT}/.env" -f "${ROOT}/docker-compose.prod.yml")

cd "$ROOT"

if [[ ! -f "${ROOT}/.env" ]]; then
  printf 'Missing %s\n' "${ROOT}/.env" >&2
  exit 1
fi

set -a
# shellcheck disable=SC1090
source "${ROOT}/.env"
set +a

echo "=== Compose ps ==="
"${COMPOSE[@]}" ps

echo ""
echo "=== Postgres (pg_isready inside postgres container) ==="
"${COMPOSE[@]}" exec -T postgres pg_isready -U "${POSTGRES_USER}" -d "${POSTGRES_DB}"

echo ""
echo "=== MinIO health (inside minio container) ==="
"${COMPOSE[@]}" exec -T minio curl -sf "http://127.0.0.1:9000/minio/health/live" >/dev/null
echo "OK"

echo ""
echo "=== be0 /health ==="
"${COMPOSE[@]}" exec -T be0 curl -sf "http://127.0.0.1:4402/health" >/dev/null
echo "OK"

echo ""
echo "=== be0 → postgres:5432 (TCP) ==="
"${COMPOSE[@]}" exec -T be0 python -c "import socket; s=socket.create_connection(('postgres',5432),3); s.close()" 
echo "OK"

echo ""
echo "=== be0 → minio:9000 (HTTP) ==="
"${COMPOSE[@]}" exec -T be0 curl -sf "http://minio:9000/minio/health/live" >/dev/null
echo "OK"

echo ""
echo "=== fe0 → be0 proxy env ==="
"${COMPOSE[@]}" exec -T fe0 /bin/sh -c 'echo "VITE_DEV_PROXY_TARGET=${VITE_DEV_PROXY_TARGET:-<unset>}"'

echo ""
printf 'Done. If be0 /health failed, read logs: docker compose --env-file .env -f docker-compose.prod.yml logs be0\n'
printf 'Typical cloud issue: POSTGRES_PASSWORD in .env does not match the first-init Postgres volume — see docs/deploy-production-docker.md\n'
