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

usage() {
  cat >&2 <<'EOF'
Usage: bash data-setup/run_data_setup.sh

Optional maintenance modes:
  --dry-run        check setup scripts only
  --download-only  download source files where scriptable
  --build-test     rebuild inputs from existing local sources
  --compare        compare regenerated inputs with committed data/
EOF
}

if [ "$#" -gt 1 ]; then
  usage
  exit 1
fi

MODE="${1:---full-test}"
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
cd "$repo_root"

case "$MODE" in
  --dry-run|--download-only|--build-test|--compare|--full-test) ;;
  *)
    usage
    exit 1
    ;;
esac

export PARTY2D_RAW_DATA_DIR="${PARTY2D_RAW_DATA_DIR:-$repo_root/_local/raw}"
export PARTY2D_BUILD_DIR="${PARTY2D_BUILD_DIR:-$repo_root/_local/build}"
export PARTY2D_GENERATED_INPUT_DIR="${PARTY2D_GENERATED_INPUT_DIR:-$repo_root/_local/generated-inputs}"
export PARTY2D_REPORT_DIR="${PARTY2D_REPORT_DIR:-$repo_root/_local/reports}"
export R_LIBS_USER="${R_LIBS_USER:-$repo_root/_local/R/library}"
mkdir -p "$R_LIBS_USER"

if [ "$MODE" = "--dry-run" ]; then
  echo "Checking data-setup scripts only; no downloads and no rebuild."
  command -v bash >/dev/null
  command -v Rscript >/dev/null
  command -v python3 >/dev/null
  bash -n data-setup/check_raw_data.sh
  python3 -m py_compile data-setup/download_sources.py
  python3 -m py_compile data-setup/compare_generated_inputs.py
  Rscript -e 'files <- list.files("data-setup/R", pattern="\\.R$", full.names=TRUE); invisible(lapply(files, parse)); cat("R setup scripts parse OK\n")'
  echo "Data setup dry run passed."
  exit 0
fi

if [ "$MODE" = "--download-only" ]; then
  python3 data-setup/download_sources.py
  exit 0
fi

if [ "$MODE" = "--compare" ]; then
  python3 data-setup/compare_generated_inputs.py
  exit 0
fi

if [ "$MODE" = "--full-test" ]; then
  python3 data-setup/download_sources.py || true
fi

bash data-setup/check_raw_data.sh
rm -rf "$PARTY2D_BUILD_DIR" "$PARTY2D_GENERATED_INPUT_DIR"
mkdir -p "$PARTY2D_BUILD_DIR" "$PARTY2D_GENERATED_INPUT_DIR"
Rscript data-setup/R/00_install_dependencies.R
Rscript data-setup/R/02_build_model_inputs.R
if [ "$MODE" = "--full-test" ]; then
  python3 data-setup/compare_generated_inputs.py || true
else
  python3 data-setup/compare_generated_inputs.py
fi
