/**
 * Cross-chain bridge config for Chain 138 (and optional 651940).
 * Used by cross-chain-indexer for CCIP/Alltra/UniversalCCIP event aggregation.
 */

export interface BridgeLane {
  destSelector: string;
  destChainId: number;
  destChainName: string;
  bridgeType?: string;
}

export interface BridgeConfig {
  address: string;
  chainId: number;
  type: 'ccip_weth9' | 'ccip_weth10' | 'ccip_stable' | 'alltra' | 'universal_ccip';
  tokenSymbol?: string;
  lanes: BridgeLane[];
}

/** Chain 138 bridge addresses from env; lanes can be extended per deployment. */
const chainId138 = 138;

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

function envAnyAddr(...keys: string[]): string {
  for (const key of keys) {
    const value = envAddr(key);
    if (value) return value;
  }
  return '';
}

export const CHAIN_138_BRIDGES: BridgeConfig[] = [];
const STABLE_ASSET_SYMBOLS = new Set(['USDT', 'USDC', 'CUSDT', 'CUSDC']);

if (envAddr('CCIPWETH9_BRIDGE_CHAIN138')) {
  CHAIN_138_BRIDGES.push({
    address: envAddr('CCIPWETH9_BRIDGE_CHAIN138'),
    chainId: chainId138,
    type: 'ccip_weth9',
    tokenSymbol: 'WETH',
    lanes: [
      { destSelector: '5009297550715157269', destChainId: 1, destChainName: 'Ethereum' },
      { destSelector: '16015286601757825753', destChainId: 651940, destChainName: 'ALL Mainnet' },
    ],
  });
}

if (envAddr('CCIPWETH10_BRIDGE_CHAIN138')) {
  CHAIN_138_BRIDGES.push({
    address: envAddr('CCIPWETH10_BRIDGE_CHAIN138'),
    chainId: chainId138,
    type: 'ccip_weth10',
    tokenSymbol: 'WETH',
    lanes: [
      { destSelector: '5009297550715157269', destChainId: 1, destChainName: 'Ethereum' },
      { destSelector: '16015286601757825753', destChainId: 651940, destChainName: 'ALL Mainnet' },
    ],
  });
}

if (envAnyAddr('CCIP_STABLE_BRIDGE_CHAIN138', 'CCIP_STABLECOIN_BRIDGE_CHAIN138')) {
  CHAIN_138_BRIDGES.push({
    address: envAnyAddr('CCIP_STABLE_BRIDGE_CHAIN138', 'CCIP_STABLECOIN_BRIDGE_CHAIN138'),
    chainId: chainId138,
    type: 'ccip_stable',
    tokenSymbol: 'STABLE',
    lanes: [
      { destSelector: '5009297550715157269', destChainId: 1, destChainName: 'Ethereum' },
      { destSelector: '16015286601757825753', destChainId: 651940, destChainName: 'ALL Mainnet' },
    ],
  });
}

if (envAddr('ALLTRA_CUSTOM_BRIDGE_ADDRESS') || envAddr('ALLTRA_ADAPTER_ADDRESS')) {
  const addr = envAddr('ALLTRA_CUSTOM_BRIDGE_ADDRESS') || envAddr('ALLTRA_ADAPTER_ADDRESS');
  CHAIN_138_BRIDGES.push({
    address: addr,
    chainId: chainId138,
    type: 'alltra',
    lanes: [{ destSelector: '0', destChainId: 651940, destChainName: 'ALL Mainnet' }],
  });
}

if (envAnyAddr('UNIVERSAL_CCIP_BRIDGE_ADDRESS', 'UNIVERSAL_CCIP_BRIDGE')) {
  CHAIN_138_BRIDGES.push({
    address: envAnyAddr('UNIVERSAL_CCIP_BRIDGE_ADDRESS', 'UNIVERSAL_CCIP_BRIDGE'),
    chainId: chainId138,
    type: 'universal_ccip',
    lanes: [
      { destSelector: '5009297550715157269', destChainId: 1, destChainName: 'Ethereum' },
      { destSelector: '16015286601757825753', destChainId: 651940, destChainName: 'ALL Mainnet' },
    ],
  });
}

/** Routing registry entry: path type ALT | CCIP, bridge address, label. Aligns with config/routing-registry.json. */
export interface RoutingRegistryEntry {
  pathType: 'ALT' | 'CCIP';
  bridgeAddress: string;
  bridgeChainId: number;
  label: string;
  fromChain: number;
  toChain: number;
  asset?: string;
}

const ALLTRA_ADAPTER_138 = envAddr('ALLTRA_ADAPTER_ADDRESS') || envAddr('ALLTRA_CUSTOM_BRIDGE_ADDRESS') || '0x66FEBA2fC9a0B47F26DD4284DAd24F970436B8Dc';
const CCIP_WETH9_138 = envAddr('CCIPWETH9_BRIDGE_CHAIN138') || '0x971cD9D156f193df8051E48043C476e53ECd4693';
const CCIP_STABLE_138 = envAnyAddr('CCIP_STABLE_BRIDGE_CHAIN138', 'CCIP_STABLECOIN_BRIDGE_CHAIN138');
const UNIVERSAL_CCIP_138 = envAnyAddr('UNIVERSAL_CCIP_BRIDGE_ADDRESS', 'UNIVERSAL_CCIP_BRIDGE');

/**
 * Get routing registry entry for (fromChain, toChain, asset).
 * Used by UI and indexer to choose ALT vs CCIP and to fill routing in activity_events.
 * Canonical registry JSON: repo root config/routing-registry.json.
 */
export function getRouteFromRegistry(
  fromChain: number,
  toChain: number,
  asset: string = 'WETH',
): RoutingRegistryEntry | null {
  if (fromChain === toChain) return null;
  const is138To651940 = fromChain === 138 && toChain === 651940;
  const is651940To138 = fromChain === 651940 && toChain === 138;
  if (is138To651940 || is651940To138) {
    return {
      pathType: 'ALT',
      bridgeAddress: ALLTRA_ADAPTER_138,
      bridgeChainId: fromChain === 138 ? 138 : 651940,
      label: 'AlltraAdapter',
      fromChain,
      toChain,
      asset,
    };
  }
  if (fromChain === 138 || toChain === 138) {
    const normalizedAsset = asset.trim().toUpperCase();
    const isStableAsset = STABLE_ASSET_SYMBOLS.has(normalizedAsset);

    if (isStableAsset) {
      if (CCIP_STABLE_138) {
        return {
          pathType: 'CCIP',
          bridgeAddress: CCIP_STABLE_138,
          bridgeChainId: 138,
          label: 'CCIPStableBridge',
          fromChain,
          toChain,
          asset,
        };
      }

      if (UNIVERSAL_CCIP_138) {
        return {
          pathType: 'CCIP',
          bridgeAddress: UNIVERSAL_CCIP_138,
          bridgeChainId: 138,
          label: 'UniversalCCIPBridge',
          fromChain,
          toChain,
          asset,
        };
      }

      return null;
    }

    if (normalizedAsset !== 'WETH' && UNIVERSAL_CCIP_138) {
      return {
        pathType: 'CCIP',
        bridgeAddress: UNIVERSAL_CCIP_138,
        bridgeChainId: 138,
        label: 'UniversalCCIPBridge',
        fromChain,
        toChain,
        asset,
      };
    }

    return {
      pathType: 'CCIP',
      bridgeAddress: CCIP_WETH9_138,
      bridgeChainId: 138,
      label: 'CCIPWETH9Bridge',
      fromChain,
      toChain,
      asset,
    };
  }
  return null;
}
