#!/usr/bin/env bash
# Canary CCIP proof: Chain 138 -> Monad (143) via CCIPWETH9Bridge (LINK fees on 138).
#
# Source-side only until a Monad inbound relay profile exists; verifies MessageSent on 138.
#
# Usage:
#   DRY_RUN=1 ./scripts/deployment/proof-ccip-chain138-monad.sh
#   ./scripts/deployment/proof-ccip-chain138-monad.sh

set -euo pipefail

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

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

DRY_RUN="${DRY_RUN:-0}"
AMOUNT_ETH="${AMOUNT_ETH:-0.001}"
RPC="${RPC_URL_138:-http://192.168.11.211:8545}"
BRIDGE="${CCIPWETH9_BRIDGE_CHAIN138:-}"
SEL="${MONAD_SELECTOR:-8481857512324358265}"
WETH="${WETH9_CHAIN138:-${WETH9:-0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2}}"
LINK="${LINK_TOKEN_CHAIN138:-0xb7721dD53A8c629d9f1Ba31a5819AFe250002b03}"
GAS="--legacy --gas-limit 800000 --gas-price 2000000000"

for v in BRIDGE PRIVATE_KEY; do
  [[ -z "${!v}" ]] && { echo "Missing $v" >&2; exit 1; }
done
require_private_key_env || exit 1

SENDER=$(cast wallet address --private-key "$PRIVATE_KEY")
RECIPIENT="${PROOF_RECIPIENT:-$SENDER}"
AMOUNT_WEI=$(cast --to-wei "$AMOUNT_ETH" ether)

echo "=== Chain 138 -> Monad CCIP canary ==="
echo "Bridge: $BRIDGE"
echo "Amount: $AMOUNT_ETH WETH ($AMOUNT_WEI wei)"
echo "Monad selector: $SEL"
echo "Recipient (Monad): $RECIPIENT"

if ! cast call "$BRIDGE" "destinations(uint64)(uint64,address,bool)" "$SEL" --rpc-url "$RPC" 2>/dev/null | awk 'NR==3{exit ($1=="true")?0:1}'; then
  echo "ERROR: 138 bridge destination for Monad not enabled. Run complete-config-monad-chain138.sh" >&2
  exit 1
fi

FEE_WEI=$(cast call "$BRIDGE" "calculateFee(uint64,uint256)" "$SEL" "$AMOUNT_WEI" --rpc-url "$RPC" | cast --to-dec)
echo "CCIP fee (LINK wei): $FEE_WEI"

if [[ "$DRY_RUN" == "1" ]]; then
  echo "[DRY RUN] approve WETH + LINK, then sendCrossChain"
  cast call "$BRIDGE" "sendCrossChain(uint64,address,uint256)" "$SEL" "$RECIPIENT" "$AMOUNT_WEI" \
    --rpc-url "$RPC" --from "$SENDER" && echo "Simulation: OK" || echo "Simulation: FAILED"
  exit 0
fi

cast send "$WETH" "approve(address,uint256)" "$BRIDGE" "$AMOUNT_WEI" \
  --rpc-url "$RPC" --private-key "$PRIVATE_KEY" $GAS
cast send "$LINK" "approve(address,uint256)" "$BRIDGE" "$((FEE_WEI + 1))" \
  --rpc-url "$RPC" --private-key "$PRIVATE_KEY" $GAS

OUT=$(cast send "$BRIDGE" "sendCrossChain(uint64,address,uint256)" "$SEL" "$RECIPIENT" "$AMOUNT_WEI" \
  --rpc-url "$RPC" --private-key "$PRIVATE_KEY" $GAS --json)
echo "$OUT"
HASH=$(echo "$OUT" | jq -r '.transactionHash // empty')
echo "$OUT"
HASH=$(echo "$OUT" | awk '/transactionHash/{print $2}')
REPORT="/home/intlc/projects/proxmox/reports/status/proof-ccip-chain138-monad-latest.json"
mkdir -p "$(dirname "$REPORT")"
cat > "$REPORT" <<EOF
{
  "generatedAt": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  "direction": "chain138_to_monad",
  "amountEth": "$AMOUNT_ETH",
  "amountWei": "$AMOUNT_WEI",
  "feeWei": "$FEE_WEI",
  "bridge": "$BRIDGE",
  "recipient": "$RECIPIENT",
  "txHash": "${HASH:-}",
  "note": "138 event-only router; Monad delivery via ccip-relay-monad on r630-01"
}
EOF
echo "Report: $REPORT"
