/**
 * Full EIP-3085 chain params and oracles for Snap / wallet_addEthereumChain.
 * Single source of truth for Chain 138, Ethereum Mainnet, and ALL Mainnet.
 */

export interface OracleEntry {
  name: string;
  address: string;
  decimals?: number;
}

export interface NetworkEntry {
  chainId: string;
  chainIdDecimal: number;
  chainName: string;
  rpcUrls: string[];
  nativeCurrency: { name: string; symbol: string; decimals: number };
  blockExplorerUrls: string[];
  iconUrls?: string[];
  oracles: OracleEntry[];
}

export const NETWORKS: NetworkEntry[] = [
  {
    chainId: '0x8a',
    chainIdDecimal: 138,
    chainName: 'DeFi Oracle Meta Mainnet',
    rpcUrls: [
      'https://rpc-http-pub.d-bis.org',
      'https://rpc.d-bis.org',
      'https://rpc2.d-bis.org',
      'https://rpc.defi-oracle.io',
    ],
    nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
    blockExplorerUrls: ['https://explorer.d-bis.org'],
    iconUrls: [
      'https://explorer.d-bis.org/favicon.ico',
      'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2/logo.png',
    ],
    oracles: [
      { name: 'ETH/USD', address: '0x3304b747e565a97ec8ac220b0b6a1f6ffdb837e6', decimals: 8 },
    ],
  },
  {
    chainId: '0x1',
    chainIdDecimal: 1,
    chainName: 'Ethereum Mainnet',
    rpcUrls: [
      'https://eth.llamarpc.com',
      'https://rpc.ankr.com/eth',
      'https://ethereum.publicnode.com',
      'https://1rpc.io/eth',
    ],
    nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
    blockExplorerUrls: ['https://etherscan.io'],
    iconUrls: [
      'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2/logo.png',
    ],
    oracles: [
      { name: 'ETH/USD', address: '0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419', decimals: 8 },
    ],
  },
  {
    chainId: '0x9f2c4',
    chainIdDecimal: 651940,
    chainName: 'ALL Mainnet',
    rpcUrls: ['https://mainnet-rpc.alltra.global'],
    nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
    blockExplorerUrls: ['https://alltra.global'],
    iconUrls: [
      'https://alltra.global/favicon.ico',
      'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2/logo.png',
    ],
    oracles: [],
  },
  {
    chainId: '0x38',
    chainIdDecimal: 56,
    chainName: 'BNB Smart Chain',
    rpcUrls: ['https://bsc-dataseed.binance.org', 'https://bsc-dataseed1.defibit.io'],
    nativeCurrency: { name: 'BNB', symbol: 'BNB', decimals: 18 },
    blockExplorerUrls: ['https://bscscan.com'],
    iconUrls: ['https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/smartchain/info/logo.png'],
    oracles: [],
  },
  {
    chainId: '0x89',
    chainIdDecimal: 137,
    chainName: 'Polygon',
    rpcUrls: ['https://polygon-rpc.com', 'https://rpc.ankr.com/polygon'],
    nativeCurrency: { name: 'MATIC', symbol: 'MATIC', decimals: 18 },
    blockExplorerUrls: ['https://polygonscan.com'],
    iconUrls: ['https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/polygon/info/logo.png'],
    oracles: [],
  },
  {
    chainId: '0x64',
    chainIdDecimal: 100,
    chainName: 'Gnosis Chain',
    rpcUrls: ['https://rpc.gnosischain.com', 'https://rpc.ankr.com/gnosis'],
    nativeCurrency: { name: 'xDAI', symbol: 'xDAI', decimals: 18 },
    blockExplorerUrls: ['https://gnosisscan.io'],
    iconUrls: ['https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/xdai/info/logo.png'],
    oracles: [],
  },
  {
    chainId: '0xa',
    chainIdDecimal: 10,
    chainName: 'Optimism',
    rpcUrls: ['https://mainnet.optimism.io', 'https://optimism.llamarpc.com'],
    nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
    blockExplorerUrls: ['https://optimistic.etherscan.io'],
    iconUrls: ['https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/optimism/info/logo.png'],
    oracles: [],
  },
  {
    chainId: '0xa4b1',
    chainIdDecimal: 42161,
    chainName: 'Arbitrum One',
    rpcUrls: ['https://arb1.arbitrum.io/rpc', 'https://arbitrum.llamarpc.com'],
    nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
    blockExplorerUrls: ['https://arbiscan.io'],
    iconUrls: ['https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/arbitrum/info/logo.png'],
    oracles: [],
  },
  {
    chainId: '0x2105',
    chainIdDecimal: 8453,
    chainName: 'Base',
    rpcUrls: ['https://mainnet.base.org', 'https://base.llamarpc.com'],
    nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
    blockExplorerUrls: ['https://basescan.org'],
    iconUrls: ['https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/base/info/logo.png'],
    oracles: [],
  },
  {
    chainId: '0xa86a',
    chainIdDecimal: 43114,
    chainName: 'Avalanche C-Chain',
    rpcUrls: ['https://api.avax.network/ext/bc/C/rpc', 'https://avalanche-c-chain-rpc.publicnode.com'],
    nativeCurrency: { name: 'AVAX', symbol: 'AVAX', decimals: 18 },
    blockExplorerUrls: ['https://snowtrace.io'],
    iconUrls: ['https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/avalanchec/info/logo.png'],
    oracles: [],
  },
  {
    chainId: '0x19',
    chainIdDecimal: 25,
    chainName: 'Cronos',
    rpcUrls: ['https://evm.cronos.org', 'https://cronos-rpc.publicnode.com'],
    nativeCurrency: { name: 'CRO', symbol: 'CRO', decimals: 18 },
    blockExplorerUrls: ['https://cronoscan.com'],
    iconUrls: ['https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/cronos/info/logo.png'],
    oracles: [],
  },
  {
    chainId: '0xa4ec',
    chainIdDecimal: 42220,
    chainName: 'Celo',
    rpcUrls: ['https://forno.celo.org', 'https://celo-rpc.publicnode.com'],
    nativeCurrency: { name: 'CELO', symbol: 'CELO', decimals: 18 },
    blockExplorerUrls: ['https://celoscan.io'],
    iconUrls: ['https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/celo/info/logo.png'],
    oracles: [],
  },
  {
    chainId: '0x457',
    chainIdDecimal: 1111,
    chainName: 'Wemix',
    rpcUrls: ['https://api.wemix.com', 'https://wemix-rpc.publicnode.com'],
    nativeCurrency: { name: 'WEMIX', symbol: 'WEMIX', decimals: 18 },
    blockExplorerUrls: ['https://scan.wemix.com'],
    iconUrls: ['https://scan.wemix.com/favicon.ico'],
    oracles: [],
  },
];

export const API_VERSION = '1.0.0';

export function getNetworks(): NetworkEntry[] {
  return NETWORKS;
}

export function getConfigByChain(chainId: number): { oracles: OracleEntry[] } | undefined {
  const net = NETWORKS.find((n) => n.chainIdDecimal === chainId);
  return net ? { oracles: net.oracles } : undefined;
}
