/**
 * Deploy CCIPLogger to a configurable network (Mainnet, BSC, Polygon, Gnosis, Cronos).
 * Usage: NETWORK=bsc npx hardhat run scripts/ccip-deployment/deploy-ccip-logger-multichain.js
 *
 * Prerequisites:
 * - contracts/ccip-integration/CCIPLogger.sol must exist (currently missing - see TASK12_CCIP_LOGGER_STATUS.md)
 * - PRIVATE_KEY in .env
 * - Chain-specific RPC and CCIP Router in .env (e.g. CCIP_BSC_ROUTER for BSC)
 */
require("dotenv").config();
const network = process.env.NETWORK || process.env.HARDHAT_NETWORK || "mainnet";

const ROUTERS = {
  mainnet: process.env.CCIP_ETH_ROUTER || "0x80226fc0Ee2b096224EeAc085Bb9a8cba1146f7D",
  bsc: process.env.CCIP_BSC_ROUTER || "",
  polygon: process.env.CCIP_POLYGON_ROUTER || "",
  gnosis: process.env.CCIP_GNOSIS_ROUTER || "",
  cronos: process.env.CCIP_CRONOS_ROUTER || "0xE26B0A098D861d5C7d9434aD471c0572Ca6EAa67",
};

async function main() {
  const { ethers } = require("hardhat");
  const routerAddress = ROUTERS[network] || ROUTERS.mainnet;
  if (!routerAddress) {
    throw new Error(`CCIP Router not configured for ${network}. Set CCIP_${network.toUpperCase()}_ROUTER in .env`);
  }
  const authorizedSigner = process.env.AUTHORIZED_SIGNER || ethers.ZeroAddress;
  const sourceChainSelector = process.env.CHAIN138_SELECTOR || "0x000000000000008a";

  const [deployer] = await ethers.getSigners();
  console.log(`Deploying CCIPLogger to ${network}...`);
  console.log("  Account:", deployer.address);
  console.log("  Balance:", (await ethers.provider.getBalance(deployer.address)).toString());
  console.log("  Router:", routerAddress);

  const CCIPLogger = await ethers.getContractFactory("CCIPLogger");
  const logger = await CCIPLogger.deploy(routerAddress, authorizedSigner, sourceChainSelector);
  await logger.waitForDeployment();
  const addr = await logger.getAddress();
  console.log("\n✅ CCIPLogger deployed:", addr);
  console.log("\nNext: npx hardhat verify --network", network, addr, `"${routerAddress}"`, `"${authorizedSigner}"`, `"${sourceChainSelector}"`);
}

main().then(() => process.exit(0)).catch((e) => { console.error(e); process.exit(1); });
