/**
 * Shared network definitions for Wagmi, Viem, CowSwap, Jumper, and others.
 * Single source of truth for chainId, RPC, explorers, native currency.
 * Chainlist entries: pr-workspace/chains/_data/chains/eip155-*.json
 */

import { defineChain } from 'viem'
import {
  mainnet,
  base,
  arbitrum,
  polygon,
  optimism,
} from 'wagmi/chains'

const rpcUrl138 = import.meta.env.VITE_RPC_URL_138 || 'https://rpc-http-pub.d-bis.org'
const rpcUrl651940 = import.meta.env.VITE_RPC_URL_651940 || 'https://mainnet-rpc.alltra.global'
const rpcUrl42793 = import.meta.env.VITE_RPC_URL_42793 || 'https://node.mainnet.etherlink.com'

/** Chain 138 - DeFi Oracle Meta Mainnet */
export const chain138 = defineChain({
  id: 138,
  name: 'DeFi Oracle Meta Mainnet',
  network: 'chain138',
  nativeCurrency: {
    decimals: 18,
    name: 'Ether',
    symbol: 'ETH',
  },
  rpcUrls: {
    default: { http: [rpcUrl138] },
    public: { http: [rpcUrl138] },
  },
  blockExplorers: {
    default: {
      name: 'DBIS Explorer',
      url: 'https://explorer.d-bis.org',
    },
  },
})

/** Chain 651940 - ALL Mainnet */
export const allMainnet = defineChain({
  id: 651940,
  name: 'ALL Mainnet',
  network: 'all-mainnet',
  nativeCurrency: {
    decimals: 18,
    name: 'ALL',
    symbol: 'ALL',
  },
  rpcUrls: {
    default: { http: [rpcUrl651940] },
    public: { http: [rpcUrl651940] },
  },
  blockExplorers: {
    default: {
      name: 'Alltra Explorer',
      url: 'https://alltra.global',
    },
  },
})

/** Chain 42793 - Etherlink Mainnet (Tezos EVM L2) */
export const etherlink = defineChain({
  id: 42793,
  name: 'Etherlink Mainnet',
  network: 'etherlink-mainnet',
  nativeCurrency: {
    decimals: 18,
    name: 'tez',
    symbol: 'XTZ',
  },
  rpcUrls: {
    default: { http: [rpcUrl42793] },
    public: { http: [rpcUrl42793] },
  },
  blockExplorers: {
    default: {
      name: 'Etherlink Explorer',
      url: 'https://explorer.etherlink.com',
    },
  },
})

const rpcUrlBsc = import.meta.env.VITE_BSC_RPC_URL || 'https://bsc-dataseed.binance.org'
const rpcUrlAvalanche = import.meta.env.VITE_AVALANCHE_RPC_URL || 'https://api.avax.network/ext/bc/C/rpc'
const rpcUrlCronos = import.meta.env.VITE_CRONOS_RPC_URL || 'https://evm.cronos.org'
const rpcUrlGnosis = import.meta.env.VITE_GNOSIS_RPC_URL || 'https://rpc.gnosischain.com'

/** BSC - Binance Smart Chain (56) */
export const bsc = defineChain({
  id: 56,
  name: 'BNB Smart Chain',
  network: 'bsc',
  nativeCurrency: { decimals: 18, name: 'BNB', symbol: 'BNB' },
  rpcUrls: { default: { http: [rpcUrlBsc] }, public: { http: [rpcUrlBsc] } },
  blockExplorers: { default: { name: 'BscScan', url: 'https://bscscan.com' } },
})

/** Avalanche C-Chain (43114) */
export const avalanche = defineChain({
  id: 43114,
  name: 'Avalanche C-Chain',
  network: 'avalanche',
  nativeCurrency: { decimals: 18, name: 'AVAX', symbol: 'AVAX' },
  rpcUrls: { default: { http: [rpcUrlAvalanche] }, public: { http: [rpcUrlAvalanche] } },
  blockExplorers: { default: { name: 'Snowtrace', url: 'https://snowtrace.io' } },
})

/** Cronos (25) */
export const cronos = defineChain({
  id: 25,
  name: 'Cronos',
  network: 'cronos',
  nativeCurrency: { decimals: 18, name: 'CRO', symbol: 'CRO' },
  rpcUrls: { default: { http: [rpcUrlCronos] }, public: { http: [rpcUrlCronos] } },
  blockExplorers: { default: { name: 'CronosScan', url: 'https://cronoscan.com' } },
})

/** Gnosis Chain (100) */
export const gnosis = defineChain({
  id: 100,
  name: 'Gnosis',
  network: 'gnosis',
  nativeCurrency: { decimals: 18, name: 'xDAI', symbol: 'xDAI' },
  rpcUrls: { default: { http: [rpcUrlGnosis] }, public: { http: [rpcUrlGnosis] } },
  blockExplorers: { default: { name: 'GnosisScan', url: 'https://gnosisscan.io' } },
})

/** All custom chains (Viem Chain type) - for use with Wagmi/Viem */
export const customChains = [chain138, allMainnet, etherlink, bsc, avalanche, cronos, gnosis] as const

/** Standard L2s from wagmi/chains (Ethereum, Base, Arbitrum, Polygon, Optimism) */
export const standardChains = [mainnet, base, arbitrum, polygon, optimism] as const

/** All networks: custom + standard. Use in Wagmi createConfig({ chains: allNetworks }) */
export const allNetworks = [...customChains, ...standardChains] as const

/** Chain IDs we support (for CowSwap, Jumper, UI filters) - all deployed contract networks */
export const supportedChainIds = [
  chain138.id,
  allMainnet.id,
  etherlink.id,
  mainnet.id,
  base.id,
  arbitrum.id,
  polygon.id,
  optimism.id,
  bsc.id,
  avalanche.id,
  cronos.id,
  gnosis.id,
] as const

/** Map chainId -> RPC URL for transports */
export const chainRpcUrls: Record<number, string> = {
  [chain138.id]: rpcUrl138,
  [allMainnet.id]: rpcUrl651940,
  [etherlink.id]: rpcUrl42793,
  [bsc.id]: rpcUrlBsc,
  [avalanche.id]: rpcUrlAvalanche,
  [cronos.id]: rpcUrlCronos,
  [gnosis.id]: rpcUrlGnosis,
}
