#!/usr/bin/env bash
# Milestone fieldtest — "Cost-model graph (in R)" (#148).
#
# Downstream-consumer reproduction: a research user with ONLY the public
# interface (the `aura` CLI, the design ledger C10, the glossary) investigating
# how much friction costs their edge on the stage1-r harness — and whether a
# HOLDING cost changes the SHAPE of the net-R equity curve.
#
# Run from the repo root after `cargo build -p aura-cli`:
#   bash fieldtests/milestone-cost-model-graph/run_scenarios.sh
#
# net_expectancy_r is the headline net-R scalar; expectancy_r is gross R.
# The full per-cycle net-R curve is the `net_r_equity` tap (persisted with --trace).
set -u
AURA=./target/debug/aura
nr() { grep -oE '"(expectancy_r|net_expectancy_r)":[-0-9.e]+'; }

echo "### Scenario 1 — bare gross-R, NO cost model (the zero-cost floor)"
$AURA run --harness stage1-r | nr
echo

echo "### Scenario 2 — flat per-trade cost (ConstantCost, deduct AT CLOSE)"
echo "# gross expectancy_r is fixed; net_expectancy_r drops monotonically with cost:"
for c in 0.0001 0.001 0.01; do
  echo -n "  --cost-per-trade $c -> "; $AURA run --harness stage1-r --cost-per-trade "$c" | nr | tr '\n' ' '; echo
done
$AURA run --harness stage1-r --cost-per-trade 0.001 --trace pertrade_cost >/dev/null
echo

echo "### Scenario 3 — per-HELD-CYCLE carry (CarryCost, ACCRUE over the hold) — the capstone"
$AURA run --harness stage1-r --carry-per-cycle 0.001 --trace carry_cost | nr
# The contrast lives in the net_r_equity gap during a hold (cycles ts4..ts7):
#   per-trade gap = FLAT 0.367 (would-be-close offset, steps only at close)
#   carry     gap = GROWING 0.367 -> 0.734 -> 1.101 -> 1.468 (continuous bleed)
echo

echo "### Scenario 4 — compose all three costs into ONE net-R curve"
echo -n "  slip-vol-mult 0.5  alone -> "; $AURA run --harness stage1-r --slip-vol-mult 0.5    | nr | tr '\n' ' '; echo
echo -n "  cost-per-trade 0.001 alone -> "; $AURA run --harness stage1-r --cost-per-trade 0.001 | nr | tr '\n' ' '; echo
echo -n "  carry-per-cycle 0.001 alone -> "; $AURA run --harness stage1-r --carry-per-cycle 0.001 | nr | tr '\n' ' '; echo
echo -n "  ALL THREE composed         -> "; $AURA run --harness stage1-r --cost-per-trade 0.001 --slip-vol-mult 0.5 --carry-per-cycle 0.001 --trace composed_cost | nr | tr '\n' ' '; echo
# The composed net-R drag equals the EXACT sum of the three single-cost drags
# (gross - composed == drag_pertrade + drag_slip + drag_carry), and composed net
# is strictly below every single cost alone.
echo

echo "### Chart the bleed (self-contained HTML, series baked in):"
echo "  $AURA chart carry_cost --tap net_r_equity > chart_carry_net_r_equity.html"
echo

echo "### Probes (robustness + foot-guns)"
echo -n "  negative carry rate -> "; $AURA run --harness stage1-r --carry-per-cycle -0.001 >/dev/null 2>&1; echo "exit=$? (guard: refused)"
echo -n "  cost flag on --harness sma (no R block) -> "; $AURA run --harness sma --cost-per-trade 0.001 | grep -oE '"total_pips":[-0-9.e]+'; echo "  ^ exit=0, cost flag SILENTLY IGNORED (no net block, no warning)"
