#!/usr/bin/env bash
set -euo pipefail

# run_generate_illegal_boards.sh
# Convenience wrapper for generate_illegal_boards.py with toggleable params.
#
# Requirements: python in PATH and repository layout unchanged.
#
# Usage examples:
#   ./scripts/run_generate_illegal_boards.sh \
#       -o /tmp/illegal.json -s both -p
#
#   ./scripts/run_generate_illegal_boards.sh \
#       -o /tmp/double_win.json -s both -r DOUBLE_WIN,COUNT_DIFF_GT1 -m any -n 200
#
#   ./scripts/run_generate_illegal_boards.sh -B /mnt/shared/data/stlm-logic/datasets \
#       -F  # auto-pick filename and overwrite if exists
#
# Flags:
#   -s <start_player>   : x | o | both (default: x)
#   -r <reasons_csv>    : Comma-separated list of reason codes to include (default: DOUBLE_WIN,COUNT_DIFF_GT1)
#   -m <reason_mode>    : any | all (default: any) when -r provided
#   -n <sample_size>    : Integer cap on number of boards
#   -o <output_file>    : Explicit output file path (.json). If omitted, auto name used in base dir.
#   -B <base_dir>       : Base directory for auto output (default: ./outputs)
#   -p                  : Pretty print JSON
#   -F                  : Force overwrite if file exists
#   -q                  : Quiet (suppress distribution summary)
#   -u                  : Unbalanced sampling (disable balancing across reason codes)
#   -h                  : Help
#
# Reason codes (current):
#   COUNT_DIFF_GT1, DOUBLE_WIN, WIN_COUNT_MISMATCH, CONTINUED_AFTER_WIN,
#   START_COUNT_INVALID, GENERIC_UNREACHABLE

PYTHON_SCRIPT="/home/data/stlm-game-logic/scripts/python/generate_illegal_boards.py"

if [[ ! -f "$PYTHON_SCRIPT" ]]; then
  echo "ERROR: Cannot find python script at $PYTHON_SCRIPT" >&2
  exit 1
fi

START_PLAYER="x"
REASONS_CSV="DOUBLE_WIN,COUNT_DIFF_GT1"
REASON_MODE="any"
SAMPLE_SIZE="5000"
OUTPUT_FILE=""
BASE_DIR="/mnt/shared/data/stlm-logic/datasets"
PRETTY=false
FORCE=true
QUIET=false
BALANCED=true

print_help() {
  sed -n '1,80p' "$0" | grep -E '^#' | sed 's/^# //'
}

while getopts ":s:r:m:n:o:B:pfFquh" opt; do
  case $opt in
    s) START_PLAYER="$OPTARG" ;;
    r) REASONS_CSV="$OPTARG" ;;
    m) REASON_MODE="$OPTARG" ;;
    n) SAMPLE_SIZE="$OPTARG" ;;
    o) OUTPUT_FILE="$OPTARG" ;;
    B) BASE_DIR="$OPTARG" ;;
    p) PRETTY=true ;;
    F) FORCE=true ;;
    q) QUIET=true ;;
  u) BALANCED=false ;;
    h) print_help; exit 0 ;;
    :) echo "Option -$OPTARG requires an argument" >&2; exit 1 ;;
    \?) echo "Unknown option -$OPTARG" >&2; exit 1 ;;
  esac
done

case "$START_PLAYER" in
  x|o|both) ;; 
  *) echo "Invalid start player: $START_PLAYER" >&2; exit 1 ;;
esac

case "$REASON_MODE" in
  any|all) ;;
  *) echo "Invalid reason mode: $REASON_MODE" >&2; exit 1 ;;
esac

mkdir -p "$BASE_DIR"

timestamp="$(date +%Y%m%d_%H%M%S)"
# Derive a tag for filename:
#  - If explicit reasons provided, use them (comma replaced by +)
#  - Else fall back to reason mode label (any|all)
if [[ -n "$REASONS_CSV" ]]; then
  reason_tag="$(echo "$REASONS_CSV" | tr ',' '+')_${REASON_MODE}"
else
  reason_tag="$REASON_MODE"
fi
if [[ -z "$OUTPUT_FILE" ]]; then
  base_name="illegal_boards_${START_PLAYER}_${reason_tag}_${SAMPLE_SIZE}_${timestamp}.json"
  OUTPUT_FILE="${BASE_DIR%/}/$base_name"
fi

if [[ -f "$OUTPUT_FILE" && $FORCE == false ]]; then
  echo "File exists: $OUTPUT_FILE (use -F to overwrite)" >&2
  exit 1
fi

CMD=(python "$PYTHON_SCRIPT" --start-player "$START_PLAYER" --output "$OUTPUT_FILE")

if [[ -n "$SAMPLE_SIZE" ]]; then
  if ! [[ "$SAMPLE_SIZE" =~ ^[0-9]+$ ]]; then
    echo "Sample size must be integer" >&2; exit 1
  fi
  CMD+=(--sample-size "$SAMPLE_SIZE")
fi

if [[ -n "$REASONS_CSV" ]]; then
  IFS=',' read -r -a REASON_ARRAY <<< "$REASONS_CSV"
  CMD+=(--reasons "${REASON_ARRAY[@]}")
  CMD+=(--reason-mode "$REASON_MODE")
fi

if [[ "$BALANCED" == false ]]; then
  CMD+=(--no-balance)
fi

if $PRETTY; then
  CMD+=(--pretty)
fi

echo "Running: ${CMD[*]}" >&2
"${CMD[@]}" | { if $QUIET; then cat > /dev/null; else cat; fi; }

# Write a small metadata sidecar
META_FILE="${OUTPUT_FILE%.json}.meta.json"
{
  echo '{'
  echo "  \"command\": \"${CMD[*]//\"/\\\"}\"," 
  echo "  \"generated_at\": \"$(date -Iseconds)\"," 
  echo "  \"start_player\": \"$START_PLAYER\"," 
  echo "  \"reasons_filter\": \"$REASONS_CSV\"," 
  echo "  \"reason_mode\": \"$REASON_MODE\"," 
  echo "  \"sample_size\": \"${SAMPLE_SIZE:-}\"" 
  echo '}'
} > "$META_FILE"

echo "Output: $OUTPUT_FILE" >&2
echo "Meta:   $META_FILE" >&2
