#!/usr/bin/env bash
# Create the full PMM pool mesh on Chain 138: all c* vs c* pairs plus c* vs official USDT/USDC.
# Uses DODOPMMIntegration.createPool() and registers each pool with DODOPMMProvider.
# Skip pairs that already have a pool. Requires POOL_MANAGER_ROLE on integration and provider.
#
# Usage:
#   ./scripts/create-pmm-full-mesh-chain138.sh              # all c* mesh + optional official pairs
#   MESH_ONLY_C_STAR=1 ./scripts/create-pmm-full-mesh-chain138.sh   # only c* vs c* (no official)
#   DRY_RUN=1 ./scripts/create-pmm-full-mesh-chain138.sh   # print only, no txs
#
# Requires: PRIVATE_KEY, RPC_URL_138, DODO_PMM_INTEGRATION_ADDRESS, DODO_PMM_PROVIDER_ADDRESS in .env

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$PROJECT_ROOT"
[[ -f .env ]] && set -a && source .env && set +a

RPC="${RPC_URL_138:-${RPC_URL:-http://192.168.11.211:8545}}"
INT="${DODO_PMM_INTEGRATION_ADDRESS:-${DODO_PMM_INTEGRATION:-}}"
PROV="${DODO_PMM_PROVIDER_ADDRESS:-}"
LP_FEE="${LP_FEE_RATE:-3}"
I="${INITIAL_PRICE:-1000000000000000000}"
K="${K_FACTOR:-500000000000000000}"
TWAP="${ENABLE_TWAP:-false}"
DRY_RUN="${DRY_RUN:-0}"
MESH_ONLY_C_STAR="${MESH_ONLY_C_STAR:-0}"

# 12 c* tokens on Chain 138 (symbol:address)
declare -a C_STAR_SYMS=(cUSDT cUSDC cEURC cEURT cGBPC cGBPT cAUDC cJPYC cCHFC cCADC cXAUC cXAUT)
declare -a C_STAR_ADDRS=(
  0x93E66202A11B1772E55407B32B44e5Cd8eda7f22
  0xf22258f57794CC8E06237084b353Ab30fFfa640b
  0x8085961F9cF02b4d800A3c6d386D31da4B34266a
  0xdf4b71c61E5912712C1Bdd451416B9aC26949d72
  0x003960f16D9d34F2e98d62723B6721Fb92074aD2
  0x350f54e4D23795f86A9c03988c7135357CCaD97c
  0xD51482e567c03899eecE3CAe8a058161FD56069D
  0xEe269e1226a334182aace90056EE4ee5Cc8A6770
  0x873990849DDa5117d7C644f0aF24370797C03885
  0x54dBd40cF05e15906A2C21f600937e96787f5679
  0x290E52a8819A4fbD0714E517225429aA2B70EC6b
  0x94e408E26c6FD8F4ee00b54dF19082FDA07dC96E
)

[[ -n "${PRIVATE_KEY:-}" ]] || { echo "PRIVATE_KEY not set"; exit 1; }
[[ -n "$INT" ]] || { echo "DODO_PMM_INTEGRATION_ADDRESS (or DODO_PMM_INTEGRATION) not set"; exit 1; }
[[ -n "$PROV" ]] || { echo "DODO_PMM_PROVIDER_ADDRESS not set"; exit 1; }

# Official USDT/USDC on 138 (from integration if available)
OFFICIAL_USDT="${OFFICIAL_USDT_ADDRESS:-0x15DF1D5BFDD8Aa4b380445D4e3E9B38d34283619}"
OFFICIAL_USDC="${OFFICIAL_USDC_ADDRESS:-}"
if [[ -z "$OFFICIAL_USDC" ]] && command -v cast &>/dev/null; then
  OFFICIAL_USDC=$(cast call "$INT" "officialUSDC()(address)" --rpc-url "$RPC" 2>/dev/null | cast --to-addr 2>/dev/null || true)
fi
[[ -z "$OFFICIAL_USDC" ]] && OFFICIAL_USDC="0x0000000000000000000000000000000000000000"

created=0
skipped=0
failed=0

pool_exists() {
  local base="$1" quote="$2"
  local addr
  addr=$(cast call "$INT" "pools(address,address)(address)" "$base" "$quote" --rpc-url "$RPC" 2>/dev/null | cast --to-addr 2>/dev/null || echo "0x0")
  [[ -n "$addr" && "$addr" != "0x0000000000000000000000000000000000000000" ]]
}

create_pool() {
  local base="$1" quote="$2" label="$3"
  if pool_exists "$base" "$quote"; then
    echo "  SKIP $label (pool exists)"
    ((skipped++)) || true
    return 0
  fi
  if [[ "$DRY_RUN" == "1" ]]; then
    echo "  [DRY] would create $label"
    ((created++)) || true
    return 0
  fi
  if cast send "$INT" "createPool(address,address,uint256,uint256,uint256,bool)" \
    "$base" "$quote" "$LP_FEE" "$I" "$K" "$TWAP" \
    --rpc-url "$RPC" --private-key "$PRIVATE_KEY" --legacy --gas-limit 500000 -q 2>/dev/null; then
    local pool_addr
    pool_addr=$(cast call "$INT" "pools(address,address)(address)" "$base" "$quote" --rpc-url "$RPC" | cast --to-addr)
    echo "  OK $label -> $pool_addr"
    if [[ -n "$PROV" ]]; then
      cast send "$PROV" "registerPool(address,address,address)" "$base" "$quote" "$pool_addr" \
        --rpc-url "$RPC" --private-key "$PRIVATE_KEY" --legacy --gas-limit 200000 -q 2>/dev/null && echo "    registered with provider" || echo "    provider register failed"
    fi
    ((created++)) || true
  else
    echo "  FAIL $label"
    ((failed++)) || true
  fi
}

echo "=== PMM full mesh Chain 138 ==="
echo "  Integration: $INT  Provider: $PROV  RPC: $RPC"
echo "  Mesh: c* vs c* (+ c* vs official USDT/USDC if MESH_ONLY_C_STAR != 1)"
echo ""

# 1) All c* vs c* pairs (base < quote by address to avoid duplicates)
n=${#C_STAR_ADDRS[@]}
for ((i=0; i<n; i++)); do
  for ((j=i+1; j<n; j++)); do
    base="${C_STAR_ADDRS[$i]}"
    quote="${C_STAR_ADDRS[$j]}"
    label="${C_STAR_SYMS[$i]}/${C_STAR_SYMS[$j]}"
    create_pool "$base" "$quote" "$label"
  done
done

# 2) c* vs official USDT and c* vs official USDC (optional)
if [[ "$MESH_ONLY_C_STAR" != "1" ]] && [[ "$OFFICIAL_USDT" != "0x0000000000000000000000000000000000000000" ]]; then
  for ((i=0; i<n; i++)); do
    create_pool "${C_STAR_ADDRS[$i]}" "$OFFICIAL_USDT" "${C_STAR_SYMS[$i]}/USDT"
  done
fi
if [[ "$MESH_ONLY_C_STAR" != "1" ]] && [[ "$OFFICIAL_USDC" != "0x0000000000000000000000000000000000000000" ]]; then
  for ((i=0; i<n; i++)); do
    create_pool "${C_STAR_ADDRS[$i]}" "$OFFICIAL_USDC" "${C_STAR_SYMS[$i]}/USDC"
  done
fi

echo ""
echo "Done. Created: $created  Skipped: $skipped  Failed: $failed"
