#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "$ROOT_DIR"

if [[ $# -lt 1 || $# -gt 2 ]]; then
  echo "usage: $0 <rir> [describe|snapshot|delta|both]" >&2
  exit 2
fi

RIR="$1"
MODE="${2:-both}"
BUNDLE_ROOT="${BUNDLE_ROOT:-$ROOT_DIR/../../rpki/target/live/20260316-112341-multi-final3}"
CASE_INFO_SCRIPT="$ROOT_DIR/scripts/payload_replay/multi_rir_case_info.py"
CASE_REPORT_SCRIPT="$ROOT_DIR/scripts/payload_replay/write_multi_rir_case_report.py"
MULTI_RIR_OUT_DIR="${MULTI_RIR_OUT_DIR:-$ROOT_DIR/target/live/multi_rir_replay_runs/$RIR}"
mkdir -p "$MULTI_RIR_OUT_DIR"

eval "$(python3 "$CASE_INFO_SCRIPT" --bundle-root "$BUNDLE_ROOT" --rir "$RIR" --format env)"

SNAPSHOT_DB_DIR="${SNAPSHOT_DB_DIR:-$MULTI_RIR_OUT_DIR/${RIR}_snapshot_replay_db}"
SNAPSHOT_REPORT_MD="${SNAPSHOT_REPORT_MD:-$MULTI_RIR_OUT_DIR/${RIR}_snapshot_compare_summary.md}"
SNAPSHOT_META_JSON="${SNAPSHOT_META_JSON:-$MULTI_RIR_OUT_DIR/${RIR}_snapshot_meta.json}"
SNAPSHOT_RUN_LOG="${SNAPSHOT_RUN_LOG:-$MULTI_RIR_OUT_DIR/${RIR}_snapshot_run.log}"
SNAPSHOT_REPORT_JSON="${SNAPSHOT_REPORT_JSON:-$MULTI_RIR_OUT_DIR/${RIR}_snapshot_report.json}"
SNAPSHOT_VRPS_CSV="${SNAPSHOT_VRPS_CSV:-$MULTI_RIR_OUT_DIR/${RIR}_snapshot_vrps.csv}"
SNAPSHOT_ONLY_OURS="${SNAPSHOT_ONLY_OURS:-$MULTI_RIR_OUT_DIR/${RIR}_snapshot_only_in_ours.csv}"
SNAPSHOT_ONLY_RECORD="${SNAPSHOT_ONLY_RECORD:-$MULTI_RIR_OUT_DIR/${RIR}_snapshot_only_in_record.csv}"

DELTA_DB_DIR="${DELTA_DB_DIR:-$MULTI_RIR_OUT_DIR/${RIR}_delta_replay_db}"
DELTA_REPORT_MD="${DELTA_REPORT_MD:-$MULTI_RIR_OUT_DIR/${RIR}_delta_compare_summary.md}"
DELTA_META_JSON="${DELTA_META_JSON:-$MULTI_RIR_OUT_DIR/${RIR}_delta_meta.json}"
DELTA_RUN_LOG="${DELTA_RUN_LOG:-$MULTI_RIR_OUT_DIR/${RIR}_delta_run.log}"
DELTA_REPORT_JSON="${DELTA_REPORT_JSON:-$MULTI_RIR_OUT_DIR/${RIR}_delta_report.json}"
DELTA_VRPS_CSV="${DELTA_VRPS_CSV:-$MULTI_RIR_OUT_DIR/${RIR}_delta_vrps.csv}"
DELTA_ONLY_OURS="${DELTA_ONLY_OURS:-$MULTI_RIR_OUT_DIR/${RIR}_delta_only_in_ours.csv}"
DELTA_ONLY_RECORD="${DELTA_ONLY_RECORD:-$MULTI_RIR_OUT_DIR/${RIR}_delta_only_in_record.csv}"

CASE_REPORT_JSON="${CASE_REPORT_JSON:-$MULTI_RIR_OUT_DIR/${RIR}_case_report.json}"
CASE_REPORT_MD="${CASE_REPORT_MD:-$MULTI_RIR_OUT_DIR/${RIR}_case_report.md}"

case "$MODE" in
  describe)
    python3 "$CASE_INFO_SCRIPT" --bundle-root "$BUNDLE_ROOT" --rir "$RIR"
    ;;
  snapshot)
    rm -rf "$SNAPSHOT_DB_DIR"
    ROUTINATOR_RECORD_CSV="$ROUTINATOR_BASE_RECORD_CSV" \
      VALIDATION_TIME="$SNAPSHOT_VALIDATION_TIME" \
      OUT_DIR="$MULTI_RIR_OUT_DIR" \
      DB_DIR="$SNAPSHOT_DB_DIR" \
      RUN_NAME="${RUN_NAME:-${RIR}_snapshot_replay}" \
      META_JSON="$SNAPSHOT_META_JSON" \
      RUN_LOG="$SNAPSHOT_RUN_LOG" \
      REPORT_JSON="$SNAPSHOT_REPORT_JSON" \
      VRPS_CSV="$SNAPSHOT_VRPS_CSV" \
      COMPARE_SUMMARY_MD="$SNAPSHOT_REPORT_MD" \
      ONLY_IN_OURS_CSV="$SNAPSHOT_ONLY_OURS" \
      ONLY_IN_RECORD_CSV="$SNAPSHOT_ONLY_RECORD" \
      ./scripts/payload_replay/run_apnic_replay.sh
    ;;
  delta)
    rm -rf "$DELTA_DB_DIR"
    ROUTINATOR_RECORD_CSV="$ROUTINATOR_DELTA_RECORD_CSV" \
      VALIDATION_TIME="$DELTA_VALIDATION_TIME" \
      PAYLOAD_BASE_VALIDATION_TIME="$SNAPSHOT_VALIDATION_TIME" \
      OUT_DIR="$MULTI_RIR_OUT_DIR" \
      DB_DIR="$DELTA_DB_DIR" \
      RUN_NAME="${RUN_NAME:-${RIR}_delta_replay}" \
      DELTA_ROOT="$RIR_ROOT" \
      META_JSON="$DELTA_META_JSON" \
      RUN_LOG="$DELTA_RUN_LOG" \
      REPORT_JSON="$DELTA_REPORT_JSON" \
      VRPS_CSV="$DELTA_VRPS_CSV" \
      COMPARE_SUMMARY_MD="$DELTA_REPORT_MD" \
      ONLY_IN_OURS_CSV="$DELTA_ONLY_OURS" \
      ONLY_IN_RECORD_CSV="$DELTA_ONLY_RECORD" \
      ./scripts/payload_replay/run_apnic_delta_replay.sh
    ;;
  both)
    rm -rf "$SNAPSHOT_DB_DIR" "$DELTA_DB_DIR"
    ROUTINATOR_RECORD_CSV="$ROUTINATOR_BASE_RECORD_CSV" \
      VALIDATION_TIME="$SNAPSHOT_VALIDATION_TIME" \
      OUT_DIR="$MULTI_RIR_OUT_DIR" \
      DB_DIR="$SNAPSHOT_DB_DIR" \
      RUN_NAME="${RUN_NAME_SNAPSHOT:-${RIR}_snapshot_replay}" \
      META_JSON="$SNAPSHOT_META_JSON" \
      RUN_LOG="$SNAPSHOT_RUN_LOG" \
      REPORT_JSON="$SNAPSHOT_REPORT_JSON" \
      VRPS_CSV="$SNAPSHOT_VRPS_CSV" \
      COMPARE_SUMMARY_MD="$SNAPSHOT_REPORT_MD" \
      ONLY_IN_OURS_CSV="$SNAPSHOT_ONLY_OURS" \
      ONLY_IN_RECORD_CSV="$SNAPSHOT_ONLY_RECORD" \
      ./scripts/payload_replay/run_apnic_replay.sh
    ROUTINATOR_RECORD_CSV="$ROUTINATOR_DELTA_RECORD_CSV" \
      VALIDATION_TIME="$DELTA_VALIDATION_TIME" \
      PAYLOAD_BASE_VALIDATION_TIME="$SNAPSHOT_VALIDATION_TIME" \
      OUT_DIR="$MULTI_RIR_OUT_DIR" \
      DB_DIR="$DELTA_DB_DIR" \
      RUN_NAME="${RUN_NAME_DELTA:-${RIR}_delta_replay}" \
      DELTA_ROOT="$RIR_ROOT" \
      META_JSON="$DELTA_META_JSON" \
      RUN_LOG="$DELTA_RUN_LOG" \
      REPORT_JSON="$DELTA_REPORT_JSON" \
      VRPS_CSV="$DELTA_VRPS_CSV" \
      COMPARE_SUMMARY_MD="$DELTA_REPORT_MD" \
      ONLY_IN_OURS_CSV="$DELTA_ONLY_OURS" \
      ONLY_IN_RECORD_CSV="$DELTA_ONLY_RECORD" \
      ./scripts/payload_replay/run_apnic_delta_replay.sh
    python3 "$CASE_REPORT_SCRIPT" \
      --rir "$RIR" \
      --snapshot-meta "$SNAPSHOT_META_JSON" \
      --snapshot-compare "$SNAPSHOT_REPORT_MD" \
      --delta-meta "$DELTA_META_JSON" \
      --delta-compare "$DELTA_REPORT_MD" \
      --routinator-base-seconds "$ROUTINATOR_BASE_REPLAY_SECONDS" \
      --routinator-delta-seconds "$ROUTINATOR_DELTA_REPLAY_SECONDS" \
      --out-md "$CASE_REPORT_MD" \
      --out-json "$CASE_REPORT_JSON" >/dev/null
    echo "- case report: $CASE_REPORT_MD" >&2
    echo "- case report json: $CASE_REPORT_JSON" >&2
    ;;
  *)
    echo "unsupported mode: $MODE; expected describe|snapshot|delta|both" >&2
    exit 2
    ;;
esac
