#!/bin/bash
set -euo pipefail

# Seed the three test sites with Akka and gRPC addresses.
# Run after deploy.sh once the central cluster is healthy.
#
# Prerequisites:
#   - Infrastructure services running (infra/docker-compose up -d)
#   - Application containers running (docker/deploy.sh)
#   - Central cluster healthy (curl http://localhost:9000/health/ready)
#
# Usage:
#   docker/seed-sites.sh

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
CLI="dotnet run --project $PROJECT_ROOT/src/ScadaLink.CLI --"
AUTH="--username multi-role --password password"
URL="--url http://localhost:9000"

echo "=== Seeding ScadaLink Sites ==="

echo ""
echo "Creating Site-A (Test Plant A)..."
$CLI $URL $AUTH site create \
    --name "Test Plant A" \
    --identifier "site-a" \
    --description "Test site A - two-node cluster" \
    --node-a-address "akka.tcp://scadalink@scadalink-site-a-a:8082" \
    --node-b-address "akka.tcp://scadalink@scadalink-site-a-b:8082" \
    --grpc-node-a-address "http://scadalink-site-a-a:8083" \
    --grpc-node-b-address "http://scadalink-site-a-b:8083" \
|| echo "  (Site-A may already exist)"

echo ""
echo "Creating Site-B (Test Plant B)..."
$CLI $URL $AUTH site create \
    --name "Test Plant B" \
    --identifier "site-b" \
    --description "Test site B - two-node cluster" \
    --node-a-address "akka.tcp://scadalink@scadalink-site-b-a:8082" \
    --node-b-address "akka.tcp://scadalink@scadalink-site-b-b:8082" \
    --grpc-node-a-address "http://scadalink-site-b-a:8083" \
    --grpc-node-b-address "http://scadalink-site-b-b:8083" \
|| echo "  (Site-B may already exist)"

echo ""
echo "Creating Site-C (Test Plant C)..."
$CLI $URL $AUTH site create \
    --name "Test Plant C" \
    --identifier "site-c" \
    --description "Test site C - two-node cluster" \
    --node-a-address "akka.tcp://scadalink@scadalink-site-c-a:8082" \
    --node-b-address "akka.tcp://scadalink@scadalink-site-c-b:8082" \
    --grpc-node-a-address "http://scadalink-site-c-a:8083" \
    --grpc-node-b-address "http://scadalink-site-c-b:8083" \
|| echo "  (Site-C may already exist)"

echo ""
echo "=== Site seeding complete ==="
echo ""
echo "Verify with: $CLI $URL $AUTH site list"
