/**
 * 13-chain liquidity heatmap config: chain groups and route list.
 * Aligns with real-robinhood project_plans and ultra_advanced_global_arbitrage_engine_blueprint.
 */

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

export type ChainGroup = 'hub' | 'edge' | 'althub' | 'external';

export interface HeatmapChain {
  chainId: number;
  name: string;
  rpc: string;
  explorer: string;
  group: ChainGroup;
}

export interface RouteEntry {
  routeId: string;
  type: 'swap-bridge-swap' | 'bridge' | 'alt';
  fromChainId: number;
  toChainId: number;
  status: 'live' | 'partial' | 'design' | 'disabled';
  bridge?: { type: string; asset?: string };
}

const CHAIN_INDEX = [138, 1, 56, 137, 10, 42161, 43114, 8453, 100, 25, 42220, 1111, 651940] as const;

const CHAIN_NAMES: Record<number, string> = {
  138: 'DBIS / DeFi Oracle',
  1: 'Ethereum',
  56: 'BSC',
  137: 'Polygon',
  10: 'Optimism',
  42161: 'Arbitrum',
  43114: 'Avalanche',
  8453: 'Base',
  100: 'Gnosis',
  25: 'Cronos',
  42220: 'Celo',
  1111: 'Wemix',
  651940: 'ALL Mainnet',
};

/** Default asset set for heatmap columns (spec). */
export const DEFAULT_HEATMAP_ASSETS = [
  'WETH',
  'cUSDT',
  'cUSDC',
  'cEURT',
  'cWUSDT',
  'cWUSDC',
  'USDW',
  'AUSDT',
  'USDC',
  'USDT',
  'XAU',
];

function buildChains(): HeatmapChain[] {
  const rpc = (cid: number) =>
    cid === 138
      ? resolveChain138RpcUrl()
      : process.env[`CHAIN_${cid}_RPC_URL`] || 'https://rpc.d-bis.org';
  const explorer = (cid: number) => {
    const urls: Record<number, string> = {
      138: 'https://explorer.d-bis.org',
      1: 'https://etherscan.io',
      56: 'https://bscscan.com',
      137: 'https://polygonscan.com',
      10: 'https://optimistic.etherscan.io',
      42161: 'https://arbiscan.io',
      43114: 'https://snowtrace.io',
      8453: 'https://basescan.org',
      100: 'https://gnosisscan.io',
      25: 'https://cronoscan.com',
      42220: 'https://celoscan.io',
      1111: 'https://scan.wemix.com',
      651940: 'https://alltra.global',
    };
    return urls[cid] || '';
  };
  return CHAIN_INDEX.map((chainId) => ({
    chainId,
    name: CHAIN_NAMES[chainId] || `Chain ${chainId}`,
    rpc: rpc(chainId),
    explorer: explorer(chainId),
    group:
      chainId === 138
        ? ('hub' as ChainGroup)
        : chainId === 651940
          ? ('althub' as ChainGroup)
          : ('edge' as ChainGroup),
  }));
}

export const HEATMAP_CHAINS = buildChains();

export function getChainsByGroup(group: ChainGroup): HeatmapChain[] {
  return HEATMAP_CHAINS.filter((c) => c.group === group);
}

/** Build route list from 13×13 matrix (hub-routed). */
export function getRoutesList(): RouteEntry[] {
  const routes: RouteEntry[] = [];
  const fromIds = [...CHAIN_INDEX];
  const toIds = [...CHAIN_INDEX];
  for (const fromChainId of fromIds) {
    for (const toChainId of toIds) {
      if (fromChainId === toChainId) continue;
      const mode =
        fromChainId === 138 && toChainId === 651940
          ? 'ALT'
          : toChainId === 138 && fromChainId === 651940
            ? 'ALT'
            : fromChainId === 138
              ? 'B/SBS'
              : toChainId === 138
                ? 'B/SBS'
                : 'via 138';
      const status: RouteEntry['status'] = 'partial';
      routes.push({
        routeId: `SBS:${fromChainId}->${toChainId}`,
        type: mode === 'ALT' ? 'alt' : 'swap-bridge-swap',
        fromChainId,
        toChainId,
        status,
        bridge: mode.includes('SBS') || mode.includes('B') ? { type: 'CCIP', asset: 'WETH' } : undefined,
      });
    }
  }
  return routes;
}

export function getChainIds(): number[] {
  return [...CHAIN_INDEX];
}
