#!/usr/bin/env bash
# Export flattened Solidity source for Cronos manual verification.
# Paste each file at https://explorer.cronos.org/verifyContract

set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
OUT_DIR="$PROJECT_ROOT/.cronos-verify"
cd "$PROJECT_ROOT"
# Load .env via dotenv (RPC CR/LF trim). Fallback: raw source.
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:-$REPO_ROOT}"
elif [[ -n "${PROJECT_ROOT:-}" && -f "$PROJECT_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$PROJECT_ROOT/.env"
  set +a
elif [[ -n "${REPO_ROOT:-}" && -f "$REPO_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$REPO_ROOT/.env"
  set +a
fi

mkdir -p "$OUT_DIR"

CONTRACTS=(
  "contracts/tokens/WETH.sol:WETH"
  "contracts/tokens/WETH10.sol:WETH10"
  "contracts/ccip/CCIPWETH9Bridge.sol:CCIPWETH9Bridge"
  "contracts/ccip/CCIPWETH10Bridge.sol:CCIPWETH10Bridge"
)
ADDRESSES=(
  "0x99B3511A2d315A497C8112C1fdd8D508d4B1E506"
  "0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6"
  "0x3Cc23d086fCcbAe1e5f3FE2bA4A263E1D27d8Cab"
  "0x105F8A15b819948a89153505762444Ee9f324684"
)

echo "Exporting flattened source for Cronos manual verification..."
echo ""

for i in "${!CONTRACTS[@]}"; do
  entry="${CONTRACTS[$i]}"
  addr="${ADDRESSES[$i]}"
  path="${entry%%:*}"
  name="${entry##*:}"
  base=$(basename "$path" .sol)
  out="$OUT_DIR/${base}_flattened.sol"
  forge flatten "$path" > "$out" 2>/dev/null
  echo "  ✓ $name -> $out"
done

# Export Standard JSON Input (required — includes viaIR:true)
echo "Exporting Standard JSON Input (includes viaIR — required for bytecode match)..."
forge verify-contract "${ADDRESSES[0]}" "${CONTRACTS[0]}" --chain cronos --show-standard-json-input 2>/dev/null | jq -c . > "$OUT_DIR/WETH_standard_input.json"
forge verify-contract "${ADDRESSES[1]}" "${CONTRACTS[1]}" --chain cronos --show-standard-json-input 2>/dev/null | jq -c . > "$OUT_DIR/WETH10_standard_input.json"
forge verify-contract "${ADDRESSES[2]}" "${CONTRACTS[2]}" --chain cronos --constructor-args "$(cast abi-encode 'constructor(address,address,address)' 0xE26B0A098D861d5C7d9434aD471c0572Ca6EAa67 0x99B3511A2d315A497C8112C1fdd8D508d4B1E506 0x8c80A01F461f297Df7F9DA3A4f740D7297C8Ac85)" --show-standard-json-input 2>/dev/null | jq -c . > "$OUT_DIR/CCIPWETH9Bridge_standard_input.json"
forge verify-contract "${ADDRESSES[3]}" "${CONTRACTS[3]}" --chain cronos --constructor-args "$(cast abi-encode 'constructor(address,address,address)' 0xE26B0A098D861d5C7d9434aD471c0572Ca6EAa67 0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6 0x8c80A01F461f297Df7F9DA3A4f740D7297C8Ac85)" --show-standard-json-input 2>/dev/null | jq -c . > "$OUT_DIR/CCIPWETH10Bridge_standard_input.json"
echo "  ✓ Standard JSON files written to $OUT_DIR/*_standard_input.json"
echo ""
echo "IMPORTANT: Use Standard-Json-Input (not flattened source). Contracts were deployed with via_ir=true."
echo "  Flattened source produces different bytecode → Unmatched."
echo ""
echo "Next: https://explorer.cronos.org/verifyContract"
echo "  1. Select 'Solidity (Standard-Json-Input)'"
echo "  2. Upload the *_standard_input.json file for each contract"
echo "  3. See docs/deployment/CRONOS_VERIFICATION_RUNBOOK.md"
echo ""
echo "Sources: $OUT_DIR/"
