#!/usr/bin/env bash
# Scenario 1 — trials-deflation provenance reaches the output.
#
# A researcher runs a walk-forward over a stage1-r parameter grid on real GER40
# bars (full 2024, long enough for an IS+OOS split). Each window's in-sample
# optimisation selects a winner; the milestone promise is that the recorded
# winner carries a trials-DEFLATED score + an overfit probability for the family
# size, NOT a bare argmax with no luck penalty.
#
# Asserts:
#   - the default selection mode is "Argmax" (deflation is the default annotation)
#   - the winner manifest carries a `selection` block with `deflated_score`,
#     `overfit_probability`, `n_trials`, and the resampling provenance
#     (`n_resamples`, `block_len`, `seed`)
#   - the deflated score is BELOW the raw winner metric (the luck penalty bit)
set -euo pipefail
cd "$(git rev-parse --show-toplevel)"
AURA="${AURA:-cargo run -q --bin aura --}"

# GER40, full calendar 2024 (Unix-ms). 9 IS+OOS windows.
FROM=1704067200000
TO=1735689599999

echo "### Scenario 1 — trials-deflation provenance"
echo "# cmd: aura walkforward --strategy stage1-r --real GER40 --from $FROM --to $TO \\"
echo "#        --fast 2,3 --slow 8,12 --stop-length 14 --stop-k 2.0 --name ft_deflation"
echo

OUT="$($AURA walkforward --strategy stage1-r --real GER40 --from "$FROM" --to "$TO" \
        --fast 2,3 --slow 8,12 --stop-length 14 --stop-k 2.0 --name ft_deflation)"

# Print the first window's selection block for the record.
FIRST="$(printf '%s\n' "$OUT" | head -1)"
echo "--- first window winner (verbatim) ---"
printf '%s\n' "$FIRST"
echo

fail() { echo "FAIL: $1" >&2; exit 1; }

printf '%s' "$FIRST" | grep -q '"mode":"Argmax"' \
  || fail 'default selection mode is not Argmax'
printf '%s' "$FIRST" | grep -q '"deflated_score"' \
  || fail 'no deflated_score in the selection block'
printf '%s' "$FIRST" | grep -q '"overfit_probability"' \
  || fail 'no overfit_probability in the selection block'
printf '%s' "$FIRST" | grep -q '"n_trials":4' \
  || fail 'n_trials is not the family size (4)'
printf '%s' "$FIRST" | grep -q '"n_resamples"' \
  || fail 'no n_resamples (bootstrap provenance) in the selection block'
printf '%s' "$FIRST" | grep -q '"block_len"' \
  || fail 'no block_len (moving-block provenance) in the selection block'

# raw_winner_metric > deflated_score (the penalty actually bites).
RAW="$(printf '%s' "$FIRST" | grep -oE '"raw_winner_metric":[-0-9.eE]+' | head -1 | cut -d: -f2)"
DEF="$(printf '%s' "$FIRST" | grep -oE '"deflated_score":[-0-9.eE]+' | head -1 | cut -d: -f2)"
echo "raw_winner_metric = $RAW   deflated_score = $DEF"
awk -v r="$RAW" -v d="$DEF" 'BEGIN{ exit !(d < r) }' \
  || fail "deflated_score ($DEF) is not below raw_winner_metric ($RAW)"

echo
echo "PASS: deflation provenance (deflated_score + overfit_probability + n_trials"
echo "      + resampling config) reaches the recorded winner, default mode Argmax,"
echo "      and the deflated score sits below the raw winner metric."
