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

SCRIPT_VERSION="download_monthly_table_pools_v1_2026_05_03"
echo "[script_version] $0 SCRIPT_VERSION=${SCRIPT_VERSION}"

# Download decoded Aerodrome Slipstream events for pools from the full-cycle
# recommendation tables. Default mode starts with pools that were only tested
# on one day / short smoke windows.
#
# Usage:
#   bash dex_platform/scripts/download_monthly_table_pools_v1.sh
#   MODE=all bash dex_platform/scripts/download_monthly_table_pools_v1.sh
#   POOL_FILTER=bio_usdc_03,check_usdc_025_new bash dex_platform/scripts/download_monthly_table_pools_v1.sh
#
# Required network:
#   BASE_RPC_URL defaults to https://mainnet.base.org but a paid/archive Base
#   RPC is strongly preferred for full monthly runs.

ROOT_DIR="${ROOT_DIR:-$(pwd)}"
COLLECTOR="${COLLECTOR:-dex_platform/data_collectors/full_cycle_import/fetch_aerodrome_slipstream_events_v2.py}"

MODE="${MODE:-one_day}"
TIME_FROM="${TIME_FROM:-2026-04-01T00:00:00Z}"
TIME_TO="${TIME_TO:-2026-05-01T00:00:00Z}"
OUT_ROOT="${OUT_ROOT:-DEX_DATA/aerodrome_slipstream/monthly_table_pools_v1}"

RPC_URL="${BASE_RPC_URL:-https://mainnet.base.org}"
EVENTS="${EVENTS:-Swap,Mint,Burn,Collect}"
CHUNK_SIZE="${CHUNK_SIZE:-3000}"
MIN_CHUNK_SIZE="${MIN_CHUNK_SIZE:-100}"
SLEEP_S="${SLEEP_S:-0}"
ATTEMPTS="${ATTEMPTS:-6}"
NO_PARQUET="${NO_PARQUET:-1}"
POOL_FILTER="${POOL_FILTER:-}"
DRY_RUN="${DRY_RUN:-0}"

mkdir -p "${OUT_ROOT}"

want_pool() {
  local name="$1"
  if [[ -z "${POOL_FILTER}" ]]; then
    return 0
  fi
  case ",${POOL_FILTER}," in
    *",${name},"*) return 0 ;;
    *) return 1 ;;
  esac
}

run_pool() {
  local group="$1"
  local name="$2"
  local address="$3"
  local note="$4"

  if ! want_pool "${name}"; then
    echo "[skip_filter] ${name}"
    return 0
  fi

  local out_dir="${OUT_ROOT}/${name}_${TIME_FROM//[:]/-}_${TIME_TO//[:]/-}"
  echo "[pool] group=${group} name=${name} address=${address} note=${note}"
  echo "[out] ${out_dir}"

  local cmd=(
    python3 "${COLLECTOR}"
    --pool "${address}"
    --time-from "${TIME_FROM}"
    --time-to "${TIME_TO}"
    --out-dir "${out_dir}"
    --rpc-url "${RPC_URL}"
    --chunk-size "${CHUNK_SIZE}"
    --min-chunk-size "${MIN_CHUNK_SIZE}"
    --sleep-s "${SLEEP_S}"
    --attempts "${ATTEMPTS}"
    --events "${EVENTS}"
  )

  if [[ "${NO_PARQUET}" == "1" ]]; then
    cmd+=(--no-parquet)
  fi

  if [[ "${DRY_RUN}" == "1" ]]; then
    printf '[dry_run]'
    printf ' %q' "${cmd[@]}"
    printf '\n'
    return 0
  fi

  "${cmd[@]}"
}

run_one_day_group() {
  # Pools from reports where evidence was one-day, half-day, or smoke-window
  # tick data and therefore need monthly collection first.
  run_pool "one_day" "bio_usdc_03" \
    "0xd40bffa9c9e35493b88a2b6744c49d8716b00898" \
    "BIO/USDC 0.3%, Apr17 one-day/0.65d champion"

  run_pool "one_day" "check_usdc_025_new" \
    "0x3c4384f3664b37a3cb5a5cb3452b4b4a3aa1256f" \
    "CHECK/USDC new 0.25%, smoke-only in table"

  run_pool "one_day" "virtual_weth_005" \
    "0x3f0296bf652e19bca772ec3df08b32732f93014a" \
    "VIRTUAL/WETH 0.05%, short/smoke table candidate"

  run_pool "one_day" "rsc_weth_03" \
    "0x6cca90e732942d73c276f73b805ca2948f6b3018" \
    "RSC/WETH, smoke table candidate"

  run_pool "one_day" "pros_usdc_03" \
    "0x24bddff0577d1ee32f92a0b92296ca9702611c6a" \
    "PROS/USDC, smoke table candidate"

  run_pool "one_day" "aixbt_weth_03" \
    "0x22a52bb644f855ebd5ca2edb643ff70222d70c31" \
    "AIXBT/WETH, smoke table candidate"

  run_pool "one_day" "bnkr_weth" \
    "0xcdd442e2de893c07146b2f1072f8e077559f9aa4" \
    "BNKR/WETH, smoke/monthly candidate"

  run_pool "one_day" "synd_usdc_03" \
    "0x9dcbb8258e0015d6cb81061b3f5c47d5c5d6188f" \
    "SYND/USDC, smoke/monthly candidate"

  run_pool "one_day" "synd_weth_03" \
    "0x50f8f7ffbd70c6c87b1668eee4e03f5ac057de3f" \
    "SYND/WETH, smoke candidate"
}

run_all_group() {
  run_one_day_group

  # Already had broader evidence or monthly sources, but useful for refreshing
  # the same monthly table consistently.
  run_pool "all" "check_usdc_2pct_orig" \
    "0x5a7b4970b2610aee4776a6944d9f2171ee6060b0" \
    "CHECK/USDC original, validated quarter source"

  run_pool "all" "aero_weth_1pct" \
    "0x82321f3beb69f503380d6b233857d5c43562e2d0" \
    "AERO/WETH 1%, basket candidate"

  run_pool "all" "bio_weth_005" \
    "0x222ccbe78890a4d9ef8d22d7fc1b968be0f2c653" \
    "BIO/WETH 0.05%, imported monthly candidate"
}

case "${MODE}" in
  one_day)
    run_one_day_group
    ;;
  all)
    run_all_group
    ;;
  *)
    echo "Unknown MODE=${MODE}. Use MODE=one_day or MODE=all." >&2
    exit 2
    ;;
esac

echo "[done] monthly table pool download script finished"
