#!/usr/bin/env bash
# run_reconcile.sh — reconcile a bank CSV against a ledger CSV and place the
# witnessed report next to your files.
#
# Usage:  bash run_reconcile.sh <bank.csv> <ledger.csv> [--html <workpaper.html>]
#
# Prefers the small standalone reconciler (zig-out/bin/apostol-reconcile) when
# it is present; falls back to the full desktop binary otherwise. Both produce
# the SAME integer-exact, witnessed report (proven by diff_gate.sh). Nothing
# leaves this machine.
#
# --html <path>  also writes a self-contained, print-ready HTML workpaper
#                (standalone reconciler only; the desktop fallback writes the
#                text report only and says so).
set -euo pipefail

if [ "$#" -lt 2 ]; then
  echo "usage: bash run_reconcile.sh <bank.csv> <ledger.csv> [--html <workpaper.html>]" >&2
  exit 2
fi

BANK="$1"
LEDGER="$2"
shift 2

HTML=""
while [ "$#" -gt 0 ]; do
  case "$1" in
    --html)
      HTML="${2:-}"
      [ -n "$HTML" ] || { echo "--html needs a file path" >&2; exit 2; }
      shift 2 ;;
    *)
      echo "unknown argument: $1" >&2
      echo "usage: bash run_reconcile.sh <bank.csv> <ledger.csv> [--html <workpaper.html>]" >&2
      exit 2 ;;
  esac
done

for f in "$BANK" "$LEDGER"; do
  [ -f "$f" ] || { echo "no such file: $f" >&2; exit 3; }
done

# Locate the reconciler binary. Prefer ./bin/apostol-reconcile next to this
# script (the shipped-package layout: run_reconcile.sh sits at the package
# root, bin/ is a sibling). Fall back to the in-repo build output
# (zig-out/bin, two levels up) so the same script also works from
# experiments/cpa_reconcile_demo/ inside the source tree.
HERE="$(cd "$(dirname "$0")" && pwd)"
PKG_CLI="$HERE/bin/apostol-reconcile"
OVERLAY=""
if [ -x "$PKG_CLI" ]; then
  CLI="$PKG_CLI"
else
  REPO="$(cd "$HERE/../.." && pwd)"
  CLI="$REPO/zig-out/bin/apostol-reconcile"
  OVERLAY="$REPO/zig-out/bin/apostol-overlay-linux"
fi
OUT="$(dirname "$BANK")/reconciliation_report.txt"

echo "Reconciling:"
echo "  bank   $BANK"
echo "  ledger $LEDGER"
echo

if [ -x "$CLI" ]; then
  # Preferred path: the standalone reconciler writes the report directly.
  if [ -n "$HTML" ]; then
    "$CLI" "$BANK" "$LEDGER" -o "$OUT" --html "$HTML"
  else
    "$CLI" "$BANK" "$LEDGER" -o "$OUT"
  fi
elif [ -n "$OVERLAY" ] && [ -x "$OVERLAY" ]; then
  # Fallback: the desktop binary writes to the canonical audit path; copy it out.
  [ -n "$HTML" ] && echo "note: --html needs the standalone reconciler; the desktop fallback wrote the text report only." >&2
  "$OVERLAY" --run-angel reconcile "$BANK" "$LEDGER"
  REPORT="$REPO/audit/reports/reconcile_demo/report.txt"
  [ -f "$REPORT" ] && cp "$REPORT" "$OUT"
else
  echo "no reconciler binary found." >&2
  echo "build the standalone tool once:  cd '$REPO' && zig build apostol-reconcile" >&2
  exit 4
fi

if [ -f "$OUT" ]; then
  echo
  echo "Full witnessed report written to: $OUT"
  [ -n "$HTML" ] && [ -f "$HTML" ] && echo "Printable HTML workpaper written to: $HTML"
  echo "----------------------------------------------------------------------"
  cat "$OUT"
fi
