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

usage() {
  cat <<'EOF'
Usage:
  ./scripts/cir/run_cir_sequence_matrix_multi_rir.sh \
    --root <path> \
    [--rir <afrinic,apnic,arin,lacnic,ripe>] \
    [--rpki-bin <path>] \
    [--routinator-root <path>] \
    [--routinator-bin <path>] \
    [--rpki-client-build-dir <path>] \
    [--drop-bin <path>]
EOF
}

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
ROOT=""
RIRS="afrinic,apnic,arin,lacnic,ripe"
RPKI_BIN="${RPKI_BIN:-$ROOT_DIR/target/release/rpki}"
ROUTINATOR_ROOT="${ROUTINATOR_ROOT:-/home/yuyr/dev/rust_playground/routinator}"
ROUTINATOR_BIN="${ROUTINATOR_BIN:-$ROUTINATOR_ROOT/target/debug/routinator}"
RPKI_CLIENT_BUILD_DIR="${RPKI_CLIENT_BUILD_DIR:-/home/yuyr/dev/rpki-client-9.7/build-m5}"
DROP_BIN="${DROP_BIN:-$ROOT_DIR/target/release/cir_drop_report}"

OURS_SCRIPT="$ROOT_DIR/scripts/cir/run_cir_replay_sequence_ours.sh"
ROUTINATOR_SCRIPT="$ROOT_DIR/scripts/cir/run_cir_replay_sequence_routinator.sh"
RPKIC_SCRIPT="$ROOT_DIR/scripts/cir/run_cir_replay_sequence_rpki_client.sh"
DROP_SCRIPT="$ROOT_DIR/scripts/cir/run_cir_drop_sequence.sh"

while [[ $# -gt 0 ]]; do
  case "$1" in
    --root) ROOT="$2"; shift 2 ;;
    --rir) RIRS="$2"; shift 2 ;;
    --rpki-bin) RPKI_BIN="$2"; shift 2 ;;
    --routinator-root) ROUTINATOR_ROOT="$2"; shift 2 ;;
    --routinator-bin) ROUTINATOR_BIN="$2"; shift 2 ;;
    --rpki-client-build-dir) RPKI_CLIENT_BUILD_DIR="$2"; shift 2 ;;
    --drop-bin) DROP_BIN="$2"; shift 2 ;;
    -h|--help) usage; exit 0 ;;
    *) echo "unknown argument: $1" >&2; usage; exit 2 ;;
  esac
done

[[ -n "$ROOT" ]] || { usage >&2; exit 2; }

SUMMARY_JSON="$ROOT/final-summary.json"
SUMMARY_MD="$ROOT/final-summary.md"
IFS=',' read -r -a ITEMS <<< "$RIRS"

results=()
for rir in "${ITEMS[@]}"; do
  seq_root="$ROOT/$rir"
  "$OURS_SCRIPT" --sequence-root "$seq_root" --rpki-bin "$RPKI_BIN"
  "$ROUTINATOR_SCRIPT" --sequence-root "$seq_root" --routinator-root "$ROUTINATOR_ROOT" --routinator-bin "$ROUTINATOR_BIN"
  "$RPKIC_SCRIPT" --sequence-root "$seq_root" --build-dir "$RPKI_CLIENT_BUILD_DIR"
  "$DROP_SCRIPT" --sequence-root "$seq_root" --drop-bin "$DROP_BIN"
done

python3 - <<'PY' "$ROOT" "$RIRS" "$SUMMARY_JSON" "$SUMMARY_MD"
import json, sys
from pathlib import Path
from collections import Counter

root = Path(sys.argv[1]).resolve()
rirs = [item for item in sys.argv[2].split(',') if item]
summary_json = Path(sys.argv[3])
summary_md = Path(sys.argv[4])
items = []
total_steps = 0
total_dropped_vrps = 0
total_dropped_objects = 0
reason_counter = Counter()
for rir in rirs:
    seq_root = root / rir
    ours = json.loads((seq_root / "sequence-summary.json").read_text(encoding="utf-8"))
    routinator = json.loads((seq_root / "sequence-summary-routinator.json").read_text(encoding="utf-8"))
    rpki_client = json.loads((seq_root / "sequence-summary-rpki-client.json").read_text(encoding="utf-8"))
    drop = json.loads((seq_root / "drop-summary.json").read_text(encoding="utf-8"))
    step_count = len(ours["steps"])
    total_steps += step_count
    rir_dropped_vrps = 0
    rir_dropped_objects = 0
    for step in drop["steps"]:
        drop_path = Path(step["reportPath"])
        detail = json.loads(drop_path.read_text(encoding="utf-8"))
        summary = detail.get("summary", {})
        rir_dropped_vrps += int(summary.get("droppedVrpCount", 0))
        rir_dropped_objects += int(summary.get("droppedObjectCount", 0))
        total_dropped_vrps += int(summary.get("droppedVrpCount", 0))
        total_dropped_objects += int(summary.get("droppedObjectCount", 0))
        for reason, count in summary.get("droppedByReason", {}).items():
            reason_counter[reason] += int(count)
    items.append({
        "rir": rir,
        "stepCount": step_count,
        "oursAllMatch": ours["allMatch"],
        "routinatorAllMatch": routinator["allMatch"],
        "rpkiClientAllMatch": rpki_client["allMatch"],
        "dropSummary": drop["steps"],
        "droppedVrpCount": rir_dropped_vrps,
        "droppedObjectCount": rir_dropped_objects,
    })
summary = {
    "version": 1,
    "totalStepCount": total_steps,
    "totalDroppedVrpCount": total_dropped_vrps,
    "totalDroppedObjectCount": total_dropped_objects,
    "topReasons": [{"reason": reason, "count": count} for reason, count in reason_counter.most_common(10)],
    "rirs": items,
}
summary_json.write_text(json.dumps(summary, indent=2), encoding="utf-8")
lines = ["# Multi-RIR CIR Sequence Matrix Summary", ""]
lines.append(f"- `total_step_count`: `{total_steps}`")
lines.append(f"- `total_dropped_vrps`: `{total_dropped_vrps}`")
lines.append(f"- `total_dropped_objects`: `{total_dropped_objects}`")
lines.append("")
if reason_counter:
    lines.append("## Top Drop Reasons")
    lines.append("")
    for reason, count in reason_counter.most_common(10):
        lines.append(f"- `{reason}`: `{count}`")
    lines.append("")
for item in items:
    lines.append(
        f"- `{item['rir']}`: `steps={item['stepCount']}` `ours={item['oursAllMatch']}` `routinator={item['routinatorAllMatch']}` `rpki-client={item['rpkiClientAllMatch']}` `drop_vrps={item['droppedVrpCount']}` `drop_objects={item['droppedObjectCount']}`"
    )
summary_md.write_text("\n".join(lines) + "\n", encoding="utf-8")
PY

echo "done: $ROOT"
