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

BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$BASE_DIR"

PYTHON_BIN="${PYTHON_BIN:-python3}"
if ! command -v "$PYTHON_BIN" >/dev/null 2>&1; then
  echo "Missing Python executable: $PYTHON_BIN" >&2
  exit 1
fi

LOG_DIR="${LOG_DIR:-$BASE_DIR/output/logs}"
mkdir -p "$LOG_DIR"
STAMP="$(date +%Y%m%d_%H%M%S)"
LOG_PATH="$LOG_DIR/weekly_digest_${STAMP}.log"

echo "[$(date --iso-8601=seconds)] Starting Wichita By E.B. weekly digest run." | tee -a "$LOG_PATH"
"$PYTHON_BIN" "$BASE_DIR/scripts/fetch_calendar.py" >>"$LOG_PATH" 2>&1
"$PYTHON_BIN" "$BASE_DIR/scripts/build_weekly_email.py" >>"$LOG_PATH" 2>&1
EXIT_CODE=$?
echo "[$(date --iso-8601=seconds)] Finished with exit code $EXIT_CODE." | tee -a "$LOG_PATH"
exit "$EXIT_CODE"
