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

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SMOM_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
ENV_FILE="$SMOM_ROOT/.env"
ORIGINAL_RPC_URL_138="${RPC_URL_138:-}"

if [[ -f "$SMOM_ROOT/scripts/lib/deployment/dotenv.sh" ]]; then
  # shellcheck disable=SC1091
  source "$SMOM_ROOT/scripts/lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "$SMOM_ROOT"
elif [[ -f "$ENV_FILE" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$ENV_FILE"
  set +a
fi

if [[ -n "$ORIGINAL_RPC_URL_138" ]]; then
  export RPC_URL_138="$ORIGINAL_RPC_URL_138"
fi

command -v cast >/dev/null 2>&1 || { echo "cast is required" >&2; exit 1; }

RPC_URL_138="${RPC_URL_138:-${RPC_URL:-http://192.168.11.221:8545}}"
DODO_PMM_INTEGRATION_ADDRESS="${CHAIN_138_DODO_PMM_INTEGRATION:-${DODO_PMM_INTEGRATION_ADDRESS:-0x86ADA6Ef91A3B450F89f2b751e93B1b7A3218895}}"
PRIVATE_KEY="${PRIVATE_KEY:-${DEPLOYER_PRIVATE_KEY:-}}"
CHAIN_GAS_PRICE="${CHAIN_GAS_PRICE:-1000000000}"
APPROVE_GAS_LIMIT="${APPROVE_GAS_LIMIT:-200000}"
ADD_LIQUIDITY_GAS_LIMIT="${ADD_LIQUIDITY_GAS_LIMIT:-900000}"
FIAT_SEED_HUMAN="${FIAT_SEED_HUMAN:-25000}"
XAU_SEED_OZ="${XAU_SEED_OZ:-100}"
XAU_SPOT_USD="${XAU_SPOT_USD:-4506.30}"

[[ -n "$DODO_PMM_INTEGRATION_ADDRESS" ]] || { echo "DODO_PMM_INTEGRATION_ADDRESS not set" >&2; exit 1; }
require_private_key_env || exit 1

DEPLOYER="$(derive_deployer_address || true)"
[[ -n "$DEPLOYER" ]] || { echo "ERROR: Could not derive DEPLOYER_ADDRESS from PRIVATE_KEY." >&2; exit 1; }
MAX_UINT='115792089237316195423570985008687907853269984665640564039457584007913129639935'
ZERO_ADDR='0x0000000000000000000000000000000000000000'

declare -A TOKENS=(
  [cUSDT]='0x93E66202A11B1772E55407B32B44e5Cd8eda7f22'
  [cUSDC]='0xf22258f57794CC8E06237084b353Ab30fFfa640b'
  [cEURC]='0x8085961F9cF02b4d800A3c6d386D31da4B34266a'
  [cEURT]='0xdf4b71c61E5912712C1Bdd451416B9aC26949d72'
  [cGBPC]='0x003960f16D9d34F2e98d62723B6721Fb92074aD2'
  [cGBPT]='0x350f54e4D23795f86A9c03988c7135357CCaD97c'
  [cAUDC]='0xD51482e567c03899eecE3CAe8a058161FD56069D'
  [cJPYC]='0xEe269e1226a334182aace90056EE4ee5Cc8A6770'
  [cCHFC]='0x873990849DDa5117d7C644f0aF24370797C03885'
  [cCADC]='0x54dBd40cF05e15906A2C21f600937e96787f5679'
  [cXAUC]='0x290E52a8819A4fbD0714E517225429aA2B70EC6b'
  [cXAUT]='0x94e408E26c6FD8F4ee00b54dF19082FDA07dC96E'
)

read -r FIAT_RAW XAU_RAW XAU_USDC_RAW <<<"$(FIAT_SEED_HUMAN="$FIAT_SEED_HUMAN" XAU_SEED_OZ="$XAU_SEED_OZ" XAU_SPOT_USD="$XAU_SPOT_USD" python3 <<'PY'
from decimal import Decimal, ROUND_DOWN
import os
fiat = Decimal(os.environ["FIAT_SEED_HUMAN"])
xau_oz = Decimal(os.environ["XAU_SEED_OZ"])
spot = Decimal(os.environ["XAU_SPOT_USD"])
fiat_raw = int((fiat * Decimal(10**6)).to_integral_value(rounding=ROUND_DOWN))
xau_raw = int((xau_oz * Decimal(10**6)).to_integral_value(rounding=ROUND_DOWN))
xau_usdc_raw = int((xau_oz * spot * Decimal(10**6)).to_integral_value(rounding=ROUND_DOWN))
print(fiat_raw, xau_raw, xau_usdc_raw)
PY
)"

PAIRS=(
  "cEURC|cUSDC|${FIAT_RAW}|${FIAT_RAW}"
  "cEURT|cUSDC|${FIAT_RAW}|${FIAT_RAW}"
  "cGBPC|cUSDC|${FIAT_RAW}|${FIAT_RAW}"
  "cGBPT|cUSDC|${FIAT_RAW}|${FIAT_RAW}"
  "cAUDC|cUSDC|${FIAT_RAW}|${FIAT_RAW}"
  "cJPYC|cUSDC|${FIAT_RAW}|${FIAT_RAW}"
  "cCHFC|cUSDC|${FIAT_RAW}|${FIAT_RAW}"
  "cCADC|cUSDC|${FIAT_RAW}|${FIAT_RAW}"
  "cXAUC|cUSDC|${XAU_RAW}|${XAU_USDC_RAW}"
  "cXAUT|cUSDC|${XAU_RAW}|${XAU_USDC_RAW}"
  "cEURC|cEURT|${FIAT_RAW}|${FIAT_RAW}"
  "cGBPC|cGBPT|${FIAT_RAW}|${FIAT_RAW}"
  "cXAUC|cXAUT|${XAU_RAW}|${XAU_RAW}"
)

declare -A APPROVED=()

approve_if_needed() {
  local token="$1"
  if [[ -n "${APPROVED[$token]:-}" ]]; then
    return 0
  fi
  cast send "$token" "approve(address,uint256)" "$DODO_PMM_INTEGRATION_ADDRESS" "$MAX_UINT" \
    --rpc-url "$RPC_URL_138" \
    --private-key "$PRIVATE_KEY" \
    --legacy \
    --gas-price "$CHAIN_GAS_PRICE" \
    --gas-limit "$APPROVE_GAS_LIMIT" >/dev/null
  APPROVED["$token"]=1
}

pool_for() {
  cast call "$DODO_PMM_INTEGRATION_ADDRESS" "pools(address,address)(address)" "$1" "$2" --rpc-url "$RPC_URL_138" | awk '{print $1}'
}

balance_for() {
  cast call "$1" "balanceOf(address)(uint256)" "$2" --rpc-url "$RPC_URL_138" | awk '{print $1}'
}

echo "=== Seed Chain 138 canonical PMM pools ==="
echo "RPC: $RPC_URL_138"
echo "Deployer: $DEPLOYER"
echo "Fiat seed: ${FIAT_SEED_HUMAN} -> ${FIAT_RAW} raw"
echo "XAU seed:  ${XAU_SEED_OZ} oz -> ${XAU_RAW} raw; quote USDC raw=${XAU_USDC_RAW}"
echo

for row in "${PAIRS[@]}"; do
  IFS='|' read -r base_sym quote_sym base_amount quote_amount <<<"$row"
  base_token="${TOKENS[$base_sym]}"
  quote_token="${TOKENS[$quote_sym]}"
  pool="$(pool_for "$base_token" "$quote_token")"
  label="${base_sym}/${quote_sym}"
  if [[ -z "$pool" || "${pool,,}" == "${ZERO_ADDR,,}" ]]; then
    echo "FAIL seed $label -> pool missing"
    exit 1
  fi

  base_balance="$(balance_for "$base_token" "$DEPLOYER")"
  quote_balance="$(balance_for "$quote_token" "$DEPLOYER")"
  if (( base_balance < base_amount )); then
    echo "SKIP seed $label -> insufficient $base_sym balance ($base_balance < $base_amount)"
    continue
  fi
  if (( quote_balance < quote_amount )); then
    echo "SKIP seed $label -> insufficient $quote_sym balance ($quote_balance < $quote_amount)"
    continue
  fi

  approve_if_needed "$base_token"
  approve_if_needed "$quote_token"

  cast send "$DODO_PMM_INTEGRATION_ADDRESS" "addLiquidity(address,uint256,uint256)" "$pool" "$base_amount" "$quote_amount" \
    --rpc-url "$RPC_URL_138" \
    --private-key "$PRIVATE_KEY" \
    --legacy \
    --gas-price "$CHAIN_GAS_PRICE" \
    --gas-limit "$ADD_LIQUIDITY_GAS_LIMIT" >/dev/null
  echo "OK seed $label -> $pool"
done

echo
echo "Done."
