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

usage() {
  cat <<'EOF'
Usage: scrape_slot.sh <morning|midday|evening> [YYYY-MM-DD]

Environment overrides:
  SCRAPE_BATCHES   Batches passed to twitter-scraper feed (default: 6)
  SCRAPE_DELAY     Delay range passed to twitter-scraper (default: 30-60)
  SCRAPER_BIN      Scraper command path (default: ./bin/twitter-scraper)
  MERGER_BIN       Merge command path (default: ./bin/tweet-csv-merge)
EOF
}

if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
  usage
  exit 0
fi

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
SLOT="${1:-}"
DATE_ISO="${2:-$(date +%F)}"
HHMM="$(date +%H%M)"
DAY_DIR="${ROOT}/PHASES/PHASE-01-INGEST/tweets/archive/${DATE_ISO}"
RUN_DIR="${DAY_DIR}/raw_feed_runs"
LOCK="/tmp/twitter-scrape-${DATE_ISO}-${SLOT}.lock"

SCRAPE_BATCHES="${SCRAPE_BATCHES:-6}"
SCRAPE_DELAY="${SCRAPE_DELAY:-30-60}"
SCRAPER_BIN="${SCRAPER_BIN:-./bin/twitter-scraper}"
MERGER_BIN="${MERGER_BIN:-./bin/tweet-csv-merge}"

if [[ -z "${SLOT}" ]]; then
  echo "[scrape] ERROR: missing slot"
  usage
  exit 2
fi

case "${SLOT}" in
  morning|midday|evening) ;;
  *)
    echo "[scrape] ERROR: invalid slot '${SLOT}' (expected morning|midday|evening)"
    exit 2
    ;;
esac

mkdir -p "${RUN_DIR}" "${ROOT}/PHASES/PHASE-01-INGEST/logs/scrape"

exec 9>"${LOCK}"
flock -n 9 || { echo "[scrape] already running date=${DATE_ISO} slot=${SLOT}"; exit 0; }

cd "${ROOT}"
if [[ -f "${ROOT}/venv/bin/activate" ]]; then
  # shellcheck source=/dev/null
  source "${ROOT}/venv/bin/activate"
fi

if [[ ! -x "${SCRAPER_BIN}" ]]; then
  echo "[scrape] ERROR: scraper binary not executable: ${SCRAPER_BIN}"
  exit 2
fi
if [[ ! -x "${MERGER_BIN}" ]]; then
  echo "[scrape] ERROR: merger binary not executable: ${MERGER_BIN}"
  exit 2
fi

OUT="${RUN_DIR}/${HHMM}_${SLOT}.csv"

echo "[scrape] start date=${DATE_ISO} slot=${SLOT} out=${OUT} batches=${SCRAPE_BATCHES} delay=${SCRAPE_DELAY} time=$(date -Is)"
"${SCRAPER_BIN}" feed "${SCRAPE_BATCHES}" --delay "${SCRAPE_DELAY}" --output "${OUT}"

shopt -s nullglob
run_files=("${RUN_DIR}"/*.csv)
shopt -u nullglob

if [[ "${#run_files[@]}" -eq 0 ]]; then
  echo "[scrape] ERROR: no run CSV files found in ${RUN_DIR}"
  exit 2
fi

echo "[scrape] merge runs=${#run_files[@]} -> ${DAY_DIR}/raw_feed.csv"
"${MERGER_BIN}" "${run_files[@]}" -o "${DAY_DIR}/raw_feed.csv" --keep first

echo "[scrape] done date=${DATE_ISO} slot=${SLOT} time=$(date -Is)"
