#!/usr/bin/env bash
# Rebuild Guard + reptest CSV later (no new calls).
#
# Usage:
#   bash scripts/guard-rebuild-report.sh <old-report.csv> 'YYYY-MM-DD HH:MM:SS'
#
# Example:
#   bash scripts/guard-rebuild-report.sh \
#     scripts/logs/guard-staging-20261117-090359-report.csv \
#     '2026-11-17 08:55:00'
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MAIN="$SCRIPT_DIR/guard-staging-call-loop.sh"

if [[ $# -lt 2 ]]; then
  echo "Usage: bash scripts/guard-rebuild-report.sh <old-report.csv> 'YYYY-MM-DD HH:MM:SS'" >&2
  exit 1
fi

REPORT_CSV="$1"
SINCE_UTC="$2"

if [[ ! -f "$REPORT_CSV" ]]; then
  echo "Report not found: $REPORT_CSV" >&2
  exit 1
fi
if [[ ! -f "$MAIN" ]]; then
  echo "Missing $MAIN" >&2
  exit 1
fi

sed -i 's/\r$//' "$MAIN" 2>/dev/null || true
exec bash "$MAIN" --rebuild-from-report "$REPORT_CSV" "$SINCE_UTC"
