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

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)"
project_root="$(cd "$repo_root/.." && pwd -P)"
raw_data_dir="${PARTY2D_RAW_DATA_DIR:-$project_root/_local/raw}"

required_files=(
  "poldem/poldem-election_all.csv"
)

optional_files=(
  "manifesto/MPDataset_MPDS2025a.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"
    missing=1
  fi
done

echo
echo "Optional raw inputs used only if cached processed files are regenerated:"
for rel in "${optional_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"
  fi
done

if [ "$missing" -ne 0 ]; then
  echo
  echo "At least one required raw input is missing." >&2
  echo "See docs/RAW_DATA_SOURCES.md for download/local-placement instructions." >&2
  exit 1
fi

echo
echo "Required raw data preflight passed."
