#!/bin/bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

echo "=== ScadaBridge Env2 Docker Deploy ==="

# Reuse the primary build (same scadabridge:latest image, same network creation)
"$SCRIPT_DIR/../docker/build.sh"

# Ensure env2 databases exist on the shared scadabridge-mssql
"$SCRIPT_DIR/init-db.sh"

echo ""
echo "Deploying env2 containers..."
docker compose -f "$SCRIPT_DIR/docker-compose.yml" up -d --force-recreate

echo ""
echo "Container status:"
docker compose -f "$SCRIPT_DIR/docker-compose.yml" ps

echo ""
echo "Access points:"
echo "  Central (Traefik LB): http://localhost:9100"
echo "  Central UI (node A):  http://localhost:9101"
echo "  Central UI (node B):  http://localhost:9102"
echo "  Health check:         http://localhost:9101/health/ready"
echo "  Active node check:    http://localhost:9101/health/active"
echo "  Traefik dashboard:    http://localhost:8181"
echo ""
echo "To seed test site (first-time setup):"
echo "  docker-env2/seed-sites.sh"
echo ""
echo "Logs: docker compose -f $SCRIPT_DIR/docker-compose.yml logs -f"
