#!/bin/bash

# Validation Checklist Script for GRU-SAC Predictor v3
# Ref: revisions.txt Section 8

# Exit immediately if a command exits with a non-zero status.
set -e

# --- Configuration --- #
# Define paths relative to the script location or project root?
# Assuming script is run from project root (e.g., ./scripts/run_validation.sh)
PROJECT_ROOT="."
CONFIG_DIR="${PROJECT_ROOT}/configs"
TEST_CONFIG_DIR="${PROJECT_ROOT}/tests"
SCRIPTS_DIR="${PROJECT_ROOT}/scripts"
RESULTS_DIR="${PROJECT_ROOT}/results"
SRC_DIR="${PROJECT_ROOT}/src" # Or wherever run.py lives relative to root

SMOKE_CONFIG="${TEST_CONFIG_DIR}/smoke.yaml"
VAL_CONFIG="${CONFIG_DIR}/quick_val.yaml"
METRICS_AGG_SCRIPT="${SCRIPTS_DIR}/aggregate_metrics.py"
PYTHON_EXEC="python"

# --- Validation Steps --- #

echo "[Validation Step 1/4] Running Unit Tests..."
pytest -q ${PROJECT_ROOT}/tests/

echo "\n[Validation Step 2/4] Running Smoke Test..."
# Assume run.py is executable from project root
${PYTHON_EXEC} ${SRC_DIR}/run_pipeline.py --config ${SMOKE_CONFIG}

echo "\n[Validation Step 3/4] Running Quick Validation Training & Backtest..."
${PYTHON_EXEC} ${SRC_DIR}/run_pipeline.py --config ${VAL_CONFIG} \
              --train_gru true --train_sac true \
              --run_backtest true \
              --use_v3 true # Ensure v3 model is tested if needed
              # Add other relevant CLI overrides if necessary for validation

echo "\n[Validation Step 4/4] Aggregating and Checking Metrics..."
# Check if aggregate script exists
if [ ! -f "${METRICS_AGG_SCRIPT}" ]; then
    echo "ERROR: Metrics aggregation script not found at ${METRICS_AGG_SCRIPT}" >&2
    echo "Skipping metrics aggregation and final checks." >&2
    exit 0 # Exit gracefully for now, but ideally should fail?
fi

# Aggregate results from the validation run (or potentially all runs?)
# Need to determine how to target the specific run or use a pattern
# Assuming results are in subdirs under RESULTS_DIR
METRICS_PATTERN="${RESULTS_DIR}/*/performance_metrics*.txt"

${PYTHON_EXEC} ${METRICS_AGG_SCRIPT} ${METRICS_PATTERN}

# The Python script aggregate_metrics.py should contain the logic 
# to parse the metrics files and exit with a non-zero status if 
# the final checks fail (Sharpe < 1.2 or Max DD > 15%).

echo "\nValidation Checklist Completed Successfully."
exit 0 