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

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
raw_data_dir="${PARTY2D_RAW_DATA_DIR:-$repo_root/_local/raw}"
report_dir="${PARTY2D_REPORT_DIR:-$repo_root/_local/reports}"
mkdir -p "$report_dir"
missing_report="$report_dir/raw_data_preflight_missing.txt"
: > "$missing_report"

required_files=(
  "manifesto/MPDataset_MPDS2025a.csv"
  "poldem/poldem-election_all.csv"
  "partyfacts/partyfacts-external-parties.csv"
  "ches/1999-2019_CHES_dataset_means(v3).csv"
  "ches/CHES_2024_final_v2.csv"
  "ches/CHES_2024_expert_level.csv"
  "ches/CHES_CA2023.csv"
  "ches/CHES_CA2023_expert_level.csv"
  "ches/ches_la_2020_aggregate_level_v01.csv"
  "ches/CHES_LA2020_expert_level.csv"
  "ches/CHES_ISRAEL_means_2021_2022.csv"
  "ches/CHES_IL_expert_level.csv"
  "vparty/V-Dem-CPD-Party-V2.rds"
  "poppa/poppa_integrated_v2.rds"
  "gps/Global Party Survey by Party SPSS V2_1_Apr_2020-2.tab"
  "morgan/morgan_positions_raw.csv"
)

echo "Raw data directory: $raw_data_dir"
echo
echo "Required raw inputs for regeneration:"

missing=0
for rel in "${required_files[@]}"; do
  path="$raw_data_dir/$rel"
  if [ -s "$path" ]; then
    bytes="$(wc -c < "$path")"
    read -r sha _ < <(sha256sum "$path")
    echo "  OK       $rel ($bytes bytes, sha256=$sha)"
  else
    echo "  MISSING  $rel"
    printf '%s\n' "$rel" >> "$missing_report"
    missing=1
  fi
done

if [ "$missing" -ne 0 ]; then
  echo
  echo "At least one required raw input is missing." >&2
  echo "Missing-file report: $missing_report" >&2
  echo "See data-setup/README.md and docs/RAW_DATA_SOURCES.md for instructions." >&2
  exit 1
fi

echo
echo "Required raw data preflight passed."
rm -f "$missing_report"
