#!/usr/bin/env bash
# ai_node/run_duplicates.sh — duplicate-payment detection + reconciliation tie-out
# on a CSV you provide, via the vendored CPA Audit Pack "match" module
# (fuzzy-propose -> deterministic-confirm). This script computes nothing itself:
# it converts your CSV to the module's input shape and prints the module's report.
set -euo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
[ "$#" -ge 1 ] || { echo "usage: $0 <ledger.csv>" >&2; exit 2; }
CSV="$1"
[ -f "$CSV" ] || { echo "no such file: $CSV" >&2; exit 3; }
OUTDIR="$(cd "$(dirname "$CSV")" && pwd)"
BASE="$(basename "$CSV" .csv)"
LEDGER_JSON="$OUTDIR/${BASE}.ai_node_ledger.json"
RESULTS_JSON="$OUTDIR/${BASE}.duplicates_results.json"
python3 "$HERE/audit_pack/csv_to_ledger.py" "$CSV" "$LEDGER_JSON"
CPA_AUDIT_LEDGER_JSON="$LEDGER_JSON" CPA_AUDIT_OUT_JSON="$RESULTS_JSON" \
  python3 "$HERE/audit_pack/match/run.py"
echo
echo "Full results: $RESULTS_JSON"
