import { resolveChain138RpcUrl } from './chain138-rpc';

export interface ChainConfig {
  chainId: number;
  name: string;
  rpcUrl: string;
  explorerUrl: string;
  nativeCurrency: {
    name: string;
    symbol: string;
    decimals: number;
  };
  blockTime: number; // Average block time in seconds
  confirmations: number; // Required confirmations for finality
}

export const CHAIN_CONFIGS: Record<number, ChainConfig> = {
  138: {
    chainId: 138,
    name: 'DeFi Oracle Meta Mainnet',
    rpcUrl: resolveChain138RpcUrl(),
    explorerUrl: 'https://explorer.d-bis.org',
    nativeCurrency: {
      name: 'Ether',
      symbol: 'ETH',
      decimals: 18,
    },
    blockTime: 5, // QBFT consensus, ~5 second blocks
    confirmations: 1, // QBFT finality
  },
  651940: {
    chainId: 651940,
    name: 'ALL Mainnet',
    rpcUrl: process.env.CHAIN_651940_RPC_URL || 'https://mainnet-rpc.alltra.global',
    explorerUrl: 'https://alltra.global',
    nativeCurrency: {
      name: 'ALL',
      symbol: 'ALL',
      decimals: 18,
    },
    blockTime: 3, // Estimated
    confirmations: 12, // Standard EVM confirmations
  },
  // cW* edge pool chains (pool-matrix); set CHAIN_*_RPC_URL to enable indexing
  1: {
    chainId: 1,
    name: 'Ethereum Mainnet',
    rpcUrl: process.env.CHAIN_1_RPC_URL || process.env.ETHEREUM_MAINNET_RPC || 'https://eth.llamarpc.com',
    explorerUrl: 'https://etherscan.io',
    nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
    blockTime: 12,
    confirmations: 12,
  },
  10: {
    chainId: 10,
    name: 'Optimism',
    rpcUrl: process.env.CHAIN_10_RPC_URL || 'https://mainnet.optimism.io',
    explorerUrl: 'https://optimistic.etherscan.io',
    nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
    blockTime: 2,
    confirmations: 1,
  },
  56: {
    chainId: 56,
    name: 'BSC (BNB Chain)',
    rpcUrl: process.env.CHAIN_56_RPC_URL || 'https://bsc-dataseed.binance.org',
    explorerUrl: 'https://bscscan.com',
    nativeCurrency: { name: 'BNB', symbol: 'BNB', decimals: 18 },
    blockTime: 3,
    confirmations: 15,
  },
  100: {
    chainId: 100,
    name: 'Gnosis Chain',
    rpcUrl: process.env.CHAIN_100_RPC_URL || 'https://rpc.gnosischain.com',
    explorerUrl: 'https://gnosisscan.io',
    nativeCurrency: { name: 'xDAI', symbol: 'xDAI', decimals: 18 },
    blockTime: 5,
    confirmations: 12,
  },
  137: {
    chainId: 137,
    name: 'Polygon',
    rpcUrl: process.env.CHAIN_137_RPC_URL || 'https://polygon-bor-rpc.publicnode.com',
    explorerUrl: 'https://polygonscan.com',
    nativeCurrency: { name: 'MATIC', symbol: 'MATIC', decimals: 18 },
    blockTime: 2,
    confirmations: 128,
  },
  42161: {
    chainId: 42161,
    name: 'Arbitrum One',
    rpcUrl: process.env.CHAIN_42161_RPC_URL || 'https://arb1.arbitrum.io/rpc',
    explorerUrl: 'https://arbiscan.io',
    nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
    blockTime: 0.25,
    confirmations: 1,
  },
  8453: {
    chainId: 8453,
    name: 'Base',
    rpcUrl: process.env.CHAIN_8453_RPC_URL || 'https://mainnet.base.org',
    explorerUrl: 'https://basescan.org',
    nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
    blockTime: 2,
    confirmations: 1,
  },
  43114: {
    chainId: 43114,
    name: 'Avalanche C-Chain',
    rpcUrl: process.env.CHAIN_43114_RPC_URL || 'https://api.avax.network/ext/bc/C/rpc',
    explorerUrl: 'https://snowtrace.io',
    nativeCurrency: { name: 'AVAX', symbol: 'AVAX', decimals: 18 },
    blockTime: 2,
    confirmations: 1,
  },
  25: {
    chainId: 25,
    name: 'Cronos',
    rpcUrl: process.env.CHAIN_25_RPC_URL || 'https://evm.cronos.org',
    explorerUrl: 'https://cronoscan.com',
    nativeCurrency: { name: 'CRO', symbol: 'CRO', decimals: 18 },
    blockTime: 6,
    confirmations: 12,
  },
  42220: {
    chainId: 42220,
    name: 'Celo',
    rpcUrl: process.env.CHAIN_42220_RPC_URL || 'https://forno.celo.org',
    explorerUrl: 'https://celoscan.io',
    nativeCurrency: { name: 'CELO', symbol: 'CELO', decimals: 18 },
    blockTime: 5,
    confirmations: 1,
  },
  1111: {
    chainId: 1111,
    name: 'Wemix',
    rpcUrl: process.env.CHAIN_1111_RPC_URL || 'https://api.wemix.com',
    explorerUrl: 'https://scan.wemix.com',
    nativeCurrency: { name: 'WEMIX', symbol: 'WEMIX', decimals: 18 },
    blockTime: 2,
    confirmations: 1,
  },
  143: {
    chainId: 143,
    name: 'Monad Mainnet',
    rpcUrl: process.env.CHAIN_143_RPC_URL || process.env.MONAD_MAINNET_RPC || 'https://rpc.monad.xyz',
    explorerUrl: 'https://monadscan.com',
    nativeCurrency: { name: 'Monad', symbol: 'MON', decimals: 18 },
    blockTime: 1,
    confirmations: 1,
  },
};

export function getChainConfig(chainId: number): ChainConfig | undefined {
  return CHAIN_CONFIGS[chainId];
}

export function getSupportedChainIds(): number[] {
  return Object.keys(CHAIN_CONFIGS).map((id) => parseInt(id, 10));
}
