#!/usr/bin/env bash
# Legacy T036 validation script.
# The canonical local VM validation path is now nix/test-cluster/run-cluster.sh.
# Keep this script only for the older manual PXE flow.

set -euo pipefail

echo "=== T036 Cluster Validation ==="
echo ""
echo "This path is archived. Prefer: nix run ./nix/test-cluster#cluster -- smoke"
echo ""

CURL_CONNECT_TIMEOUT="${CURL_CONNECT_TIMEOUT:-5}"
CURL_MAX_TIME="${CURL_MAX_TIME:-10}"
CURL_INSECURE="${CURL_INSECURE:-1}"
CURL_FLAGS=(--connect-timeout "$CURL_CONNECT_TIMEOUT" --max-time "$CURL_MAX_TIME")
if [[ "$CURL_INSECURE" == "1" ]]; then
    CURL_FLAGS+=(-k)
fi

# Wait for services to be ready
echo "Waiting for cluster services to start (60 seconds)..."
sleep 60

echo ""
echo "=== S6.1: PXE Server Validation ==="
echo ""
echo "Checking DHCP service..."
ssh root@192.168.100.1 'systemctl status dnsmasq || true'

echo ""
echo "Checking DHCP leases..."
ssh root@192.168.100.1 'cat /var/lib/dnsmasq/dnsmasq.leases || echo "No leases yet"'

echo ""
echo "=== S6.2: Chainfire Cluster Validation ==="
echo ""
echo "Checking Chainfire cluster members on node01..."
curl "${CURL_FLAGS[@]}" https://192.168.100.11:2379/admin/cluster/members | jq . || echo "Chainfire API not ready"

echo ""
echo "Expected: 3 members (node01, node02, node03), one leader elected"
echo ""

echo "=== S6.3: FlareDB Cluster Validation ==="
echo ""
echo "Checking FlareDB cluster members on node01..."
curl "${CURL_FLAGS[@]}" https://192.168.100.11:2479/admin/cluster/members | jq . || echo "FlareDB API not ready"

echo ""
echo "=== S6.4: CRUD Operations Test ==="
echo ""
echo "Writing test key to FlareDB..."
curl "${CURL_FLAGS[@]}" -X PUT https://192.168.100.11:2479/api/v1/kv/test-key \
  -H "Content-Type: application/json" \
  -d '{"value": "hello-t036-cluster"}' || echo "Write failed"

echo ""
echo "Reading test key from node01..."
curl "${CURL_FLAGS[@]}" https://192.168.100.11:2479/api/v1/kv/test-key || echo "Read failed"

echo ""
echo "Reading test key from node02 (verify replication)..."
curl "${CURL_FLAGS[@]}" https://192.168.100.12:2479/api/v1/kv/test-key || echo "Read failed"

echo ""
echo "Reading test key from node03 (verify replication)..."
curl "${CURL_FLAGS[@]}" https://192.168.100.13:2479/api/v1/kv/test-key || echo "Read failed"

echo ""
echo "=== S6.5: IAM Service Validation ==="
echo ""
for node in 192.168.100.11 192.168.100.12 192.168.100.13; do
    echo "Checking IAM health on $node..."
    curl "${CURL_FLAGS[@]}" https://$node:8080/health || echo "IAM not ready on $node"
    echo ""
done

echo ""
echo "=== S6.6: Health Checks ==="
echo ""
for node in 192.168.100.11 192.168.100.12 192.168.100.13; do
    echo "Node: $node"
    echo "  Chainfire: $(curl -s "${CURL_FLAGS[@]}" https://$node:2379/health || echo 'N/A')"
    echo "  FlareDB:   $(curl -s "${CURL_FLAGS[@]}" https://$node:2479/health || echo 'N/A')"
    echo "  IAM:       $(curl -s "${CURL_FLAGS[@]}" https://$node:8080/health || echo 'N/A')"
    echo ""
done

echo ""
echo "=== Validation Complete ==="
echo ""
echo "Review the output above and verify:"
echo "  ✓ Chainfire cluster: 3 members, leader elected"
echo "  ✓ FlareDB cluster: 3 members, quorum formed"
echo "  ✓ CRUD operations: write/read working, data replicated to all nodes"
echo "  ✓ IAM service: operational on all 3 nodes"
echo "  ✓ Health checks: all services responding"
echo ""
echo "If all checks pass, T036 acceptance criteria are met."
