#!/usr/bin/env bash
# Deploy Ligbox Ops Console — VM123 apenas
# Uso: cd /opt/ligbox-ops-console && ./scripts/deploy-console.sh
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

if [[ ! -f .env ]]; then
  echo "Criar .env a partir de .env.example"
  exit 1
fi

# shellcheck disable=SC1091
source .env

echo "=== Preflight ==="
"$ROOT/scripts/preflight-vm123.sh" "${CONSOLE_HOST_PORT:-8100}"

echo "=== Build + up (stack isolado) ==="
docker compose -f "$ROOT/docker-compose.yml" build --pull
docker compose -f "$ROOT/docker-compose.yml" up -d

echo "=== Health ==="
sleep 3
curl -sf "http://${CONSOLE_BIND:-127.0.0.1}:${CONSOLE_HOST_PORT:-8100}/health" && echo

echo "=== Containers deste stack ==="
docker compose -f "$ROOT/docker-compose.yml" ps

echo "Deploy concluído. Configurar Traefik: console.ligbox.com.br -> ${CONSOLE_BIND:-127.0.0.1}:${CONSOLE_HOST_PORT:-8100}"
