#!/usr/bin/env bash
# Reproduction check: run each case WITHOUT perf and confirm cost + operation
# counts match the recorded results_20260519_stdgen.csv row exactly. This proves
# the current source/toolchain reproduces the CSV before we spend time profiling.
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${HERE}/cases.sh"

[[ -x "${BIN}" ]] || { echo "Build first: ${HERE}/build_prof.sh"; exit 1; }

VAL="${OUT}/validation.csv"
: > "${VAL}"
# CSV column indices: 22=cost 25=n_shake 26=n_vnd1 27=n_vnd2 28=n_vnd3 29=n_vnd4
FIELDS="22,25,26,27,28,29"
status=0

for entry in "${CASES[@]}"; do
  IFS='|' read -r name d k file kmax iters seed bk <<< "${entry}"
  echo "=== ${name}: ${file} d=${d} k=${k} seed=${seed} ==="

  line="$(${BIN} $(args_for "${entry}") --csv)"
  echo "${line}" >> "${VAL}"

  got="$(echo "${line}"    | cut -d, -f"${FIELDS}")"
  exp="$(awk -F, -v f="${file}" -v d="${d}" -v k="${k}" -v s="${seed}" \
           '$1==f && $3==d && $4==k && $7==s {print; exit}' "${CSV}" | cut -d, -f"${FIELDS}")"

  echo "  expected (cost,n_shake,n_vnd1..4): ${exp}"
  echo "  got      (cost,n_shake,n_vnd1..4): ${got}"
  if [[ "${got}" == "${exp}" ]]; then
    echo "  MATCH"
  else
    echo "  *** MISMATCH ***"
    status=1
  fi
  echo
done

if [[ ${status} -eq 0 ]]; then
  echo "All three cases reproduce the CSV exactly. Safe to profile."
else
  echo "One or more mismatches -- investigate before profiling (RNG/toolchain drift?)."
fi
exit ${status}
