#!/bin/bash

# === CONFIG ===
REPLICA_SERVER=""  # <-- fill this in
SSH_USER=""                                       # <-- fill this in
PG_USER="postgres"                                    # usually postgres

# === Connect and check SSH first ===
echo "[*] Connecting to replica server $REPLICA_SERVER..."

ssh -o BatchMode=yes -o ConnectTimeout=5 "$SSH_USER@$REPLICA_SERVER" "echo 2>&1" && SSH_OK=true || SSH_OK=false

if [[ "$SSH_OK" == false ]]; then
  echo "❌ SSH connection failed! Could not reach $REPLICA_SERVER."
  echo "👉 Suggested Action: Check server DNS, IP, SSH service, firewall."
  exit 2
fi

# === Run the Replication Check ===
OUTPUT=$(ssh "doc@38.102.127.174" "sudo -u $PG_USER psql -Atc \"SELECT application_name, state, sync_state, write_lag, flush_lag, replay_lag FROM pg_stat_replication;\"")

if [[ -z "$OUTPUT" ]]; then
  echo "❌ No replication data found!"
  echo "👉 Suggested Action: Check PostgreSQL service on both primary and replica. Verify replication setup."
  exit 1
fi

# === Parse and Advise ===
echo ""
echo "=== Replication Status ==="
echo "$OUTPUT"
echo "==========================="

while IFS='|' read -r application_name state sync_state write_lag flush_lag replay_lag; do
  echo ""
  echo "🛠 Replica: $application_name"
  echo "🔹 Connection State: $state"
  echo "🔹 Sync State: $sync_state"
  echo "🔹 Write Lag: $write_lag"
  echo "🔹 Flush Lag: $flush_lag"
  echo "🔹 Replay Lag: $replay_lag"

  if [[ "$state" == "streaming" && "$sync_state" == "sync" ]]; then
    echo "✅ Status: Healthy synchronous replication. No action needed."
  elif [[ "$state" == "streaming" && "$sync_state" == "async" ]]; then
    echo "⚠️  Warning: Replica is asynchronous. Check if this is expected."
  elif [[ "$state" == "catchup" ]]; then
    echo "⚠️  Warning: Replica is catching up. Monitor closely."
  else
    echo "❌ Error: Unknown or broken state. Immediate action required."
  fi

  if [[ "$write_lag" != "" && "$write_lag" != "0" ]]; then
    echo "⚠️  Write Lag Detected: $write_lag"
  fi
  if [[ "$flush_lag" != "" && "$flush_lag" != "0" ]]; then
    echo "⚠️  Flush Lag Detected: $flush_lag"
  fi
  if [[ "$replay_lag" != "" && "$replay_lag" != "0" ]]; then
    echo "⚠️  Replay Lag Detected: $replay_lag"
  fi

done <<< "$OUTPUT"

echo ""
echo "[✓] Replica check complete."
