#!/usr/bin/env bash
# diff-results.sh - compare test results between baseline and feature branches
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/test-common.sh"

CID="${CLUSTER_ID_DEDICATED}"
DDIR="${RESULTS_DIR}/${CID}-Dedicated"

has_jq() {
  command -v jq >/dev/null 2>&1
}

if has_jq; then
  JQ_AVAILABLE=1
else
  JQ_AVAILABLE=0
fi

# Prometheus comparison
echo "================================================================"
echo " Prometheus: ${BASELINE_LABEL} vs feature (Dedicated ${CID})"
echo "================================================================"
echo ""
echo "Results round directory: ${RESULTS_DIR}"
echo ""

PROM_MAIN="${DDIR}/${BASELINE_LABEL}/prometheus/Dedicated-${CID}.txt"
PROM_FEAT="${DDIR}/feature/prometheus/Dedicated-${CID}.txt"

if [[ -f "${PROM_MAIN}" && -f "${PROM_FEAT}" ]]; then
  echo "--- metric name diff (+ added in feature / - removed in feature) ---"
  diff \
    <(grep -v '^#' "${PROM_MAIN}" | awk '{print $1}' | sort -u) \
    <(grep -v '^#' "${PROM_FEAT}" | awk '{print $1}' | sort -u) \
    > "${DDIR}/prometheus-metric-names-diff.txt" || true
  cat "${DDIR}/prometheus-metric-names-diff.txt"

  echo ""
  echo "--- full line diff (saved to ${DDIR}/prometheus-diff.txt) ---"
  diff \
    <(grep -v '^#' "${PROM_MAIN}" | sort) \
    <(grep -v '^#' "${PROM_FEAT}" | sort) \
    > "${DDIR}/prometheus-diff.txt" || true
  wc -l "${DDIR}/prometheus-diff.txt"
else
  echo "⚠️  Prometheus result files are missing. Run pipeline/tools/local-e2e/scripts/run-prometheus-test.sh first"
fi

# OTel / Vector Datadog comparison
echo ""
echo "================================================================"
echo " OTel Datadog: ${BASELINE_LABEL} vs feature (Dedicated ${CID})"
echo "================================================================"
echo ""

DD_MAIN="${DDIR}/${BASELINE_LABEL}/otel-datadog/Dedicated-${CID}.ndjson"
DD_FEAT="${DDIR}/feature/otel-datadog/Dedicated-${CID}.ndjson"

if [[ -f "${DD_MAIN}" && -f "${DD_FEAT}" ]]; then
  if has_jq; then
    echo "--- metric name diff ---"
    diff \
      <(jq -r '.name' "${DD_MAIN}" | sort -u) \
      <(jq -r '.name' "${DD_FEAT}" | sort -u) \
      > "${DDIR}/otel-dd-names-diff.txt" || true
    cat "${DDIR}/otel-dd-names-diff.txt"

    echo ""
    echo "--- metric name+tags structure diff (saved to ${DDIR}/otel-dd-diff.txt) ---"
    diff \
      <(jq -r '[.name, (.tags // {} | to_entries | map("\(.key)=\(.value)") | sort | join(","))] | join(" ")' "${DD_MAIN}" | sort -u) \
      <(jq -r '[.name, (.tags // {} | to_entries | map("\(.key)=\(.value)") | sort | join(","))] | join(" ")' "${DD_FEAT}" | sort -u) \
      > "${DDIR}/otel-dd-diff.txt" || true
    wc -l "${DDIR}/otel-dd-diff.txt"
  else
    echo "⚠️  jq is not installed, skipping OTel Datadog JSON diff"
  fi
else
  echo "⚠️  OTel Datadog result files are missing. Run pipeline/tools/local-e2e/scripts/run-otel-test.sh first"
fi

# OTel / Vector NewRelic comparison
echo ""
echo "================================================================"
echo " OTel NewRelic: ${BASELINE_LABEL} vs feature (Dedicated ${CID})"
echo "================================================================"
echo ""

NR_MAIN="${DDIR}/${BASELINE_LABEL}/otel-newrelic/Dedicated-${CID}.ndjson"
NR_FEAT="${DDIR}/feature/otel-newrelic/Dedicated-${CID}.ndjson"

if [[ -f "${NR_MAIN}" && -f "${NR_FEAT}" ]]; then
  if has_jq; then
    echo "--- metric name diff ---"
    diff \
      <(jq -r '.name' "${NR_MAIN}" | sort -u) \
      <(jq -r '.name' "${NR_FEAT}" | sort -u) \
      > "${DDIR}/otel-nr-names-diff.txt" || true
    cat "${DDIR}/otel-nr-names-diff.txt"

    echo ""
    echo "--- metric name+tags structure diff (saved to ${DDIR}/otel-nr-diff.txt) ---"
    diff \
      <(jq -r '[.name, (.tags // {} | to_entries | map("\(.key)=\(.value)") | sort | join(","))] | join(" ")' "${NR_MAIN}" | sort -u) \
      <(jq -r '[.name, (.tags // {} | to_entries | map("\(.key)=\(.value)") | sort | join(","))] | join(" ")' "${NR_FEAT}" | sort -u) \
      > "${DDIR}/otel-nr-diff.txt" || true
    wc -l "${DDIR}/otel-nr-diff.txt"
  else
    echo "⚠️  jq is not installed, skipping OTel NewRelic JSON diff"
  fi
else
  echo "⚠️  OTel NewRelic result files are missing. Run pipeline/tools/local-e2e/scripts/run-otel-test.sh first"
fi

# Feature-only summary for Premium / Essential / Starter
echo ""
echo "================================================================"
echo " Feature-only cluster metric summary (Premium / Essential / Starter)"
echo "================================================================"
echo ""

for cluster_entry in \
    "${CLUSTER_ID_PREMIUM}:Premium" \
    "${CLUSTER_ID_ESSENTIAL}:Essential" \
    "${CLUSTER_ID_STARTER}:Starter"; do
  IFS=: read -r cid splan <<< "${cluster_entry}"
  echo "--- ${splan} (${cid}) ---"

  prom_file="${RESULTS_DIR}/${cid}-${splan}/feature/prometheus/${splan}-${cid}.txt"
  dd_file="${RESULTS_DIR}/${cid}-${splan}/feature/otel-datadog/${splan}-${cid}.ndjson"

  if [[ -f "${prom_file}" ]]; then
    echo "  Prometheus metric names:"
    grep -v '^#' "${prom_file}" | awk '{print $1}' | sort -u | sed 's/^/    /'
  else
    echo "  ⚠️  Prometheus results are missing"
  fi

  if [[ -f "${dd_file}" && "${JQ_AVAILABLE}" -eq 1 ]]; then
    echo "  OTel Datadog metric names:"
    jq -r '.name' "${dd_file}" | sort -u | sed 's/^/    /'
  elif [[ -f "${dd_file}" ]]; then
    echo "  ⚠️  jq is not installed, skipping OTel Datadog metric summary"
  else
    echo "  ⚠️  OTel Datadog results are missing"
  fi
  echo ""
done
