/**
 * Deploy VaultfireForumRegistry to a single chain.
 *
 * Usage:
 *   PRIVATE_KEY=<deployer-key> \
 *     npx hardhat run scripts/deploy-forum-registry.js --network <network>
 *
 *   Networks: baseMainnet, avalanche, arbitrum, polygon
 *
 * SECURITY: Private key MUST come from environment. Never hardcode.
 */

const hre = require('hardhat');

async function main() {
  const { ethers, network } = hre;

  console.log('═'.repeat(60));
  console.log('  VaultfireForumRegistry Deployment');
  console.log('═'.repeat(60));
  console.log('Network:', network.name);

  const [deployer] = await ethers.getSigners();
  const balance = await deployer.provider.getBalance(deployer.address);
  console.log('Deployer:', deployer.address);
  console.log('Balance: ', ethers.formatEther(balance), 'ETH');

  if (balance === 0n) {
    throw new Error('Deployer has zero balance. Fund the wallet first.');
  }

  console.log('\nDeploying VaultfireForumRegistry...');
  const Factory = await ethers.getContractFactory('VaultfireForumRegistry');
  const registry = await Factory.deploy();
  await registry.waitForDeployment();
  const address = await registry.getAddress();

  console.log('\n✅ VaultfireForumRegistry deployed at:', address);

  // Wait a few blocks before verification
  if (network.name !== 'hardhat' && network.name !== 'localhost') {
    console.log('\nWaiting for confirmations before verification...');
    await registry.deploymentTransaction().wait(5);

    console.log('Verifying on block explorer...');
    try {
      await hre.run('verify:verify', {
        address,
        constructorArguments: [],
      });
      console.log('✅ Contract verified');
    } catch (e) {
      console.log('⚠️  Verification failed (can retry later):', e.message);
    }
  }

  console.log('\n📋 Deployment Summary');
  console.log('═'.repeat(60));
  console.log('Network:', network.name);
  console.log('Address:', address);
  console.log('Deployer:', deployer.address);
  console.log('═'.repeat(60));
  console.log('\nNext: add this address to app/lib/contracts.ts under FORUM_REGISTRY for', network.name);
}

main()
  .then(() => process.exit(0))
  .catch(err => {
    console.error('❌ Deployment failed:', err);
    process.exit(1);
  });
