#!/usr/bin/env bash
# Test: argo-train.sh --multi-seed N --folds K --dry-run emits one
# WorkflowTask per seed (N total). Each task internally runs all K folds via
# the binary's `--max-folds K` argument — train_baseline_rl is a multi-fold
# walk-forward executor, NOT a single-fold one.
#
# This is the validation surface for the Plan 5 Task 5 Phase B multi-seed
# Argo DAG (was N*K (seed, fold) jobs in Task 1A; pivoted to N seed-only jobs
# after the first deploy failed at startup with `--fold` not recognized).
# Verifies the matrix + binary command is generated correctly without
# submitting to a live cluster.
set -euo pipefail

# Resolve repo root from this script's location so the test runs from any cwd.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
cd "${REPO_ROOT}"

# Dry-run: --multi-seed 3 --folds 2 should emit 3 training jobs (one per seed),
# each invoking the binary with `--max-folds 2` to run both folds internally.
output=$(./scripts/argo-train.sh dqn --multi-seed 3 --folds 2 --dry-run 2>&1)
actual_count=$(echo "$output" | grep -c "kind: WorkflowTask" || true)
expected=3
if [[ "$actual_count" -ne "$expected" ]]; then
    echo "FAIL: expected $expected jobs (3 seeds, fold sweep inside binary), got $actual_count"
    echo "--- output ---"
    echo "$output"
    exit 1
fi
echo "PASS: multi-seed harness produces $expected per-seed jobs for --multi-seed 3 --folds 2"

# The rendered binary command must include `--max-folds {{workflow.parameters.folds}}`
# (drives the walk-forward sweep inside the single training process) and must
# NOT include any per-fold flag — `train_baseline_rl` rejects `--fold`, which
# is what broke the first L40S deploy attempt (workflow train-multi-seed-z2llf,
# every job exited at startup with `error: unexpected argument '--fold' found`).
# The Argo `{{workflow.parameters.folds}}` placeholder is resolved at
# workflow submission time (argo-train.sh passes `-p folds=$FOLDS` to argo
# submit), not at template render time, so the dry-run shows the placeholder.
if ! echo "$output" | grep -qE -- "--max-folds[[:space:]]+\{\{workflow\.parameters\.folds\}\}"; then
    echo "FAIL: rendered binary command missing '--max-folds {{workflow.parameters.folds}}'"
    echo "--- output (rendered template, first 300 lines) ---"
    echo "$output" | head -300
    exit 1
fi
echo "PASS: rendered binary invocation includes --max-folds placeholder"

# Folds parameter must be declared at the workflow level so `argo submit -p
# folds=K` (which argo-train.sh emits) can override the default at submit time.
if ! echo "$output" | grep -qE "^[[:space:]]*-[[:space:]]*name:[[:space:]]*folds[[:space:]]*$"; then
    echo "FAIL: rendered template does not declare 'folds' workflow parameter"
    exit 1
fi
echo "PASS: rendered template declares folds workflow parameter"

if echo "$output" | grep -qE -- '--fold[[:space:]]+"?[0-9{]'; then
    echo "FAIL: rendered binary command still contains a per-fold flag (--fold N)"
    echo "      train_baseline_rl rejects --fold; only --max-folds is supported."
    echo "--- offending lines ---"
    echo "$output" | grep -E -- '--fold[[:space:]]+"?[0-9{]'
    exit 1
fi
echo "PASS: rendered binary invocation has no per-fold flag (Path B compliance)"

# Backward-compat: --multi-seed 1 --folds 1 must NOT emit WorkflowTask lines
# (single-job path uses the existing template, no DAG matrix).
output_single=$(./scripts/argo-train.sh dqn --multi-seed 1 --folds 1 --dry-run 2>&1)
single_tasks=$(echo "$output_single" | grep -c "kind: WorkflowTask" || true)
if [[ "$single_tasks" -ne 0 ]]; then
    echo "FAIL: single-job dry-run should not emit multi-seed DAG tasks (got $single_tasks)"
    exit 1
fi
echo "PASS: single-job dry-run preserves existing single-template path"
