const { ethers } = require("hardhat");
require("dotenv").config();

/**
 * Deploy CCIPLogger to Ethereum Mainnet
 * This contract receives and logs Chain-138 transactions via CCIP
 */
async function main() {
  const [deployer] = await ethers.getSigners();
  console.log("Deploying CCIPLogger with account:", deployer.address);
  console.log("Account balance:", (await ethers.provider.getBalance(deployer.address)).toString());

  // Get configuration from environment
  const routerAddress = process.env.CCIP_ETH_ROUTER || "0x80226fc0Ee2b096224EeAc085Bb9a8cba1146f7D"; // Official Chainlink CCIP Router on Mainnet
  const authorizedSigner = process.env.AUTHORIZED_SIGNER || ethers.ZeroAddress;
  const sourceChainSelector = process.env.CHAIN138_SELECTOR || "0x000000000000008a"; // Chain-138 selector (update with actual value from CCIP Directory)

  console.log("\nConfiguration:");
  console.log("  Router:", routerAddress);
  console.log("  Authorized Signer:", authorizedSigner);
  console.log("  Source Chain Selector:", sourceChainSelector);

  // Deploy CCIPLogger
  const CCIPLogger = await ethers.getContractFactory("CCIPLogger");
  console.log("\nDeploying CCIPLogger...");
  
  const logger = await CCIPLogger.deploy(
    routerAddress,
    authorizedSigner,
    sourceChainSelector
  );

  await logger.waitForDeployment();
  const loggerAddress = await logger.getAddress();

  console.log("\n✅ CCIPLogger deployed to:", loggerAddress);
  console.log("\nDeployment details:");
  console.log("  Router:", await logger.getRouter());
  console.log("  Authorized Signer:", await logger.authorizedSigner());
  console.log("  Source Chain Selector:", await logger.expectedSourceChainSelector());
  console.log("  Owner:", await logger.owner());

  console.log("\n📝 Next steps:");
  console.log("  1. Verify contract on Etherscan:");
  console.log(`     npx hardhat verify --network mainnet ${loggerAddress} "${routerAddress}" "${authorizedSigner}" "${sourceChainSelector}"`);
  console.log("  2. Update .env with CCIP_LOGGER_ETH_ADDRESS=" + loggerAddress);
  console.log("  3. Deploy CCIPTxReporter to Chain-138 with this address as destination");
}

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