#!/usr/bin/env bash
# Scenario 2 — plateau-over-peak is opt-in; the default is unchanged.
#
# The milestone adds a robustness objective: prefer a parameter PLATEAU (a
# member whose grid neighbourhood is uniformly decent) over a sharp in-sample
# PEAK. The contract (ledger) is that this is OPT-IN via `--select`, the default
# stays the bare-argmax selection rule, and the plateau ANNOTATION reaches the
# recorded winner (orthogonal rule x annotation).
#
# Asserts:
#   - default walk-forward records mode "Argmax" + deflation annotation
#     (deflated_score present, plateau keys absent)
#   - `--select plateau:worst` records mode "PlateauWorst" + plateau annotation
#     (neighbourhood_score + n_neighbours present, deflation keys absent)
#   - `--select plateau:mean` records mode "PlateauMean"
#   - a bad --select token is rejected non-zero
set -euo pipefail
cd "$(git rev-parse --show-toplevel)"
AURA="${AURA:-cargo run -q --bin aura --}"

FROM=1704067200000   # GER40 full 2024
TO=1735689599999
GRID=(--fast 2,3 --slow 8,12 --stop-length 14 --stop-k 2.0)

fail() { echo "FAIL: $1" >&2; exit 1; }
# Capture the whole stream into a var, THEN slice the first window. Piping the
# subcommand straight into `head -1` closes stdout early and makes `aura` panic
# with a Broken-pipe on stdout (recorded as a finding) — a careful consumer
# captures first.
first_window() { printf '%s\n' "$1" | head -1; }

echo "### Scenario 2 — plateau-over-peak (opt-in; default unchanged)"
echo

echo "--- (a) default selection (no --select) ---"
DEF="$(first_window "$($AURA walkforward --strategy stage1-r --real GER40 --from "$FROM" --to "$TO" \
        "${GRID[@]}" --name ft_plateau_default)")"
printf '%s\n' "$DEF"
printf '%s' "$DEF" | grep -q '"mode":"Argmax"' \
  || fail 'default mode is not Argmax'
printf '%s' "$DEF" | grep -q '"deflated_score"' \
  || fail 'default did not carry the deflation annotation'
printf '%s' "$DEF" | grep -q '"neighbourhood_score"' \
  && fail 'default unexpectedly carried a plateau annotation'
echo

echo "--- (b) --select plateau:worst ---"
PW="$(first_window "$($AURA walkforward --strategy stage1-r --real GER40 --from "$FROM" --to "$TO" \
        "${GRID[@]}" --select plateau:worst --name ft_plateau_worst)")"
printf '%s\n' "$PW"
printf '%s' "$PW" | grep -q '"mode":"PlateauWorst"' \
  || fail 'plateau:worst did not record mode PlateauWorst'
printf '%s' "$PW" | grep -q '"neighbourhood_score"' \
  || fail 'plateau:worst did not record neighbourhood_score'
printf '%s' "$PW" | grep -q '"n_neighbours"' \
  || fail 'plateau:worst did not record n_neighbours'
printf '%s' "$PW" | grep -q '"deflated_score"' \
  && fail 'plateau:worst unexpectedly carried the deflation annotation'
echo

echo "--- (c) --select plateau:mean ---"
PM="$(first_window "$($AURA walkforward --strategy stage1-r --real GER40 --from "$FROM" --to "$TO" \
        "${GRID[@]}" --select plateau:mean --name ft_plateau_mean)")"
printf '%s' "$PM" | grep -q '"mode":"PlateauMean"' \
  || fail 'plateau:mean did not record mode PlateauMean'
echo "PlateauMean recorded OK"
echo

echo "--- (d) bad --select token is rejected ---"
set +e
$AURA walkforward --strategy stage1-r --real GER40 --from "$FROM" --to "$TO" \
   "${GRID[@]}" --select plateau:median --name ft_plateau_bad >/dev/null 2>&1
RC=$?
set -e
[ "$RC" -ne 0 ] || fail 'bad --select token was not rejected (exit 0)'
echo "bad token --select plateau:median rejected, exit=$RC"
echo

echo "PASS: plateau is opt-in; default stays Argmax+deflation; plateau:worst /"
echo "      plateau:mean record the plateau annotation; bad token rejected."
