/**
 * Built-in CCIP / Trustless bridge route payload when BRIDGE_LIST_JSON_URL is unset.
 * Aligns with MetaMask Snap expectations and docs/07-ccip/CCIP_BRIDGE_MAINNET_CONNECTION.md.
 */

import { getActivePublicPools, getActiveTransportPairs, getGruTransportMetadata } from '../../config/gru-transport';

export interface BridgeRoutesPayload {
  routes: {
    weth9: Record<string, string>;
    weth10: Record<string, string>;
    trustless?: Record<string, string>;
  };
  chain138Bridges: {
    weth9: string;
    weth10: string;
    trustless?: string;
  };
  tokenMappingApi: {
    basePath: string;
    pairs: string;
    resolve: string;
    note: string;
  };
  gruTransport?: {
    system: unknown;
    summary?: Record<string, number>;
    activeTransportPairs: unknown[];
    activePublicPools: unknown[];
  };
}

const DEFAULT_WETH9_138 = '0xcacfd227A040002e49e2e01626363071324f820a';
const DEFAULT_WETH10_138 = '0xe0E93247376aa097dB308B92e6Ba36bA015535D0';
const DEFAULT_LOCKBOX_138 = '0xFce6f50B312B3D936Ea9693C5C9531CF92a3324c';

/** Destination-side WETH9 receivers (relay-backed on mainnet per CCIP_BRIDGE_MAINNET_CONNECTION.md). */
const WETH9_DESTINATIONS: Record<string, string> = {
  'Ethereum Mainnet (1)': '0xF9A32F37099c582D28b4dE7Fca6eaC1e5259f939',
  'BNB Chain (56)': '0x886C6A4ABC064dbf74E7caEc460b7eeC31F1b78C',
  'Polygon (137)': '0xF7736443f02913e7e0773052103296CfE1637448',
  'Avalanche C-Chain (43114)': '0x3f8C409C6072a2B6a4Ff17071927bA70F80c725F',
  'Base (8453)': '0x24293CA562aE1100E60a4640FF49bd656cFf93B4',
  'Arbitrum One (42161)': '0x937824f2516fa58f25aeAb92E7BFf7D74F463B4c',
  'Optimism (10)': '0x6e94e53F73893b2a6784Df663920D31043A6dE07',
  'Gnosis (100)': '0xc8656F24488cb90c452058da92d1a25BA464eaAE',
  'Cronos (25)': '0x3Cc23d086fCcbAe1e5f3FE2bA4A263E1D27d8Cab',
  'Celo (42220)': '0xAb57BF30F1354CA0590af22D8974c7f24DB2DbD7',
  'Wemix (1111)': '0xD3AD6831aacB5386B8A25BB8D8176a6C8a026f04',
  'Monad (143)': '0xC158b6cD3A3088C52F797D41f5Aa02825361629e',
};

const WETH10_DESTINATIONS: Record<string, string> = {
  'Ethereum Mainnet (1)': '0x04E1e22B0D41e99f4275bd40A50480219bc9A223',
  'BNB Chain (56)': '0x937824f2516fa58f25aeAb92E7BFf7D74F463B4c',
  'Polygon (137)': '0x0CA60e6f8589c540200daC9D9Cb27BC2e48eE66A',
  'Avalanche C-Chain (43114)': '0x937824f2516fa58f25aeAb92E7BFf7D74F463B4c',
  'Base (8453)': '0x937824f2516fa58f25aeAb92E7BFf7D74F463B4c',
  'Arbitrum One (42161)': '0x73376eB92c16977B126dB9112936A20Fa0De3442',
  'Optimism (10)': '0x24293CA562aE1100E60a4640FF49bd656cFf93B4',
  'Gnosis (100)': '0xa846aeAD3071df1b6439d5D813156aCE7C2c1DA1',
  'Cronos (25)': '0x105F8A15b819948a89153505762444Ee9f324684',
  'Celo (42220)': '0xa780ef19A041745d353c9432f2a7f5A241335ffE',
  'Wemix (1111)': '0xa4B9DD039565AeD9641D45b57061f99d9cA6Df08',
};

function envAddr(key: string, fallback: string): string {
  const v = process.env[key];
  return typeof v === 'string' && v.startsWith('0x') ? v : fallback;
}

export function buildDefaultBridgeRoutes(): BridgeRoutesPayload {
  const inboxEth = process.env.INBOX_ETH?.trim();
  const trustlessRoutes: Record<string, string> = {};
  if (inboxEth?.startsWith('0x')) {
    trustlessRoutes['Ethereum Mainnet (1)'] = inboxEth;
  }

  const gruTransportMetadata = getGruTransportMetadata();

  return {
    routes: {
      weth9: { ...WETH9_DESTINATIONS },
      weth10: { ...WETH10_DESTINATIONS },
      ...(Object.keys(trustlessRoutes).length > 0 ? { trustless: trustlessRoutes } : {}),
    },
    chain138Bridges: {
      weth9: envAddr('CCIPWETH9_BRIDGE_CHAIN138', DEFAULT_WETH9_138),
      weth10: envAddr('CCIPWETH10_BRIDGE_CHAIN138', DEFAULT_WETH10_138),
      trustless: envAddr('LOCKBOX_138', DEFAULT_LOCKBOX_138),
    },
    tokenMappingApi: {
      basePath: '/api/v1/token-mapping',
      pairs: '/api/v1/token-mapping/pairs',
      resolve: '/api/v1/token-mapping/resolve',
      note: 'Resolve bridged token addresses between chains; requires monorepo config/token-mapping-multichain.json on server.',
    },
    gruTransport: gruTransportMetadata
      ? {
          system: gruTransportMetadata.system,
          summary: gruTransportMetadata.counts,
          activeTransportPairs: getActiveTransportPairs(),
          activePublicPools: getActivePublicPools(),
        }
      : undefined,
  };
}
