#!/usr/bin/env bash
# After cw-l1-bootstrap-gru-v2-ccip-routes.sh: approve LINK + tokens, then lockAndSend
# 90% / 10 per-chain amounts to RECIPIENT_ADDRESS (default Ulysse EVM).
#
# Usage:
#   RECIPIENT_ADDRESS=0x... bash scripts/deployment/cw-l1-bridge-gru-v2-90pct-lock-and-send.sh [--dry-run]
#
# Env: CW_L1_BRIDGE_CHAIN138, RPC_URL_138, PRIVATE_KEY
#      LINK: fee token on bridge (defaults to feeToken() on bridge)

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SMOM_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
PROXMOX_ROOT="$(cd "$SMOM_ROOT/.." && pwd)"
cd "$SMOM_ROOT"

DRY_RUN=0
[[ "${1:-}" == "--dry-run" ]] && DRY_RUN=1

if [[ -f "$PROXMOX_ROOT/scripts/lib/load-project-env.sh" ]]; then
  PROJECT_ROOT="$PROXMOX_ROOT" source "$PROXMOX_ROOT/scripts/lib/load-project-env.sh"
elif [[ -f .env ]]; then
  set -a && source .env && set +a
fi

BRIDGE="${CW_L1_BRIDGE_CHAIN138:-}"
RPC="${RPC_URL_138:-http://192.168.11.211:8545}"
RECIPIENT="${RECIPIENT_ADDRESS:-0xCbe669ECe9e3Da7dD1688207e545EDDA9a64A514}"

[[ -n "$BRIDGE" ]] || { echo "Set CW_L1_BRIDGE_CHAIN138" >&2; exit 1; }
[[ -n "${PRIVATE_KEY:-}" ]] || { echo "PRIVATE_KEY required" >&2; exit 1; }

LINK_TOKEN="${LINK_TOKEN_CHAIN138:-${LINK_TOKEN:-}}"
if [[ -z "$LINK_TOKEN" ]]; then
  LINK_TOKEN=$(cast call "$BRIDGE" "feeToken()(address)" --rpc-url "$RPC" 2>/dev/null | grep -oE '0x[a-fA-F0-9]{40}' | head -1)
fi
[[ -n "$LINK_TOKEN" && "$LINK_TOKEN" != "0x0000000000000000000000000000000000000000" ]] || {
  echo "Could not resolve LINK fee token; set LINK_TOKEN_CHAIN138" >&2
  exit 1
}

MAX_U256="115792089237316195423570985008687907853269984665640564039457584007913129639935"

# token|per_chain_wei (90% / 10 floors from operator plan)
declare -a PAIRS=(
  "0x243e6581Dc8a98d98B92265858b322b193555C81|3822172740000"
  "0x2bAFA83d8fF8BaE9505511998987D0659791605B|2778945930000"
  "0x707508D223103f5D2d9EFBc656302c9d48878b29|4123219860000"
  "0xee17c18E10E55ce23F7457D018aAa2Fb1E64B281|4744441350000"
  "0xfb37aFd415B70C5cEDc9bA58a72D517207b769Bb|6522556410000"
  "0x2c751bBE4f299b989b3A8c333E0A966cdcA6Fd98|3557466000000"
  "0x60B7FB8e0DD0Be8595AD12Fe80AE832861Be747c|6810124410000"
  "0xe799033c87fE0CE316DAECcefBE3134CC74b76a9|2471337720000"
  "0xF0F0F81bE3D033D8586bAfd2293e37eE2f615647|2421904773"
  "0x89477E982847023aaB5C3492082cd1bB4b1b9Ef1|2421904773"
)

# Same 10 selectors as bootstrap script (order must match)
declare -a SELECTORS=(
  5009297550715157269
  3734403246176062136
  1456215246176062136
  11344663589394136015
  465200170687744372
  4051577828743386545
  15971525489660198786
  4949039107694359620
  1346049177634351622
  6433500567565415381
)

send() {
  if [[ "$DRY_RUN" -eq 1 ]]; then
    echo "[dry-run] $*"
    return 0
  fi
  "$@"
}

echo "Bridge=$BRIDGE recipient=$RECIPIENT LINK=$LINK_TOKEN"
echo "=== Approvals: LINK + each GRU v2 token to bridge (max) ==="
send cast send "$LINK_TOKEN" "approve(address,uint256)" "$BRIDGE" "$MAX_U256" \
  --rpc-url "$RPC" --private-key "$PRIVATE_KEY" --legacy --gas-limit 120000

for row in "${PAIRS[@]}"; do
  token="${row%%|*}"
  echo "approve token $token"
  send cast send "$token" "approve(address,uint256)" "$BRIDGE" "$MAX_U256" \
    --rpc-url "$RPC" --private-key "$PRIVATE_KEY" --legacy --gas-limit 120000
done

echo ""
echo "=== lockAndSend (10 tokens x 10 chains) ==="
for row in "${PAIRS[@]}"; do
  token="${row%%|*}"
  amt="${row#*|}"
  for sel in "${SELECTORS[@]}"; do
    echo "lockAndSend token=$token selector=$sel amount=$amt"
    send cast send "$BRIDGE" "lockAndSend(address,uint64,address,uint256)" \
      "$token" "$sel" "$RECIPIENT" "$amt" \
      --rpc-url "$RPC" --private-key "$PRIVATE_KEY" --legacy --gas-limit 5000000
  done
done

echo "Done."
