/**
 * Bridge Capability Matrix for Chain138->Tezos routing
 */

export type BridgeMethod = 'lock_mint' | 'burn_mint' | 'wrapped' | 'custodial';

export interface BridgeCapability {
  provider: string;
  sourceChains: number[];
  destinationChains: number[];
  assets: string[];
  method: BridgeMethod;
  custodial: boolean;
  minAmount?: string;
  maxAmount?: string;
  status: 'active' | 'deprecated' | 'planned';
}

export const CHAIN_138 = 138;
export const CHAIN_ETHEREUM = 1;
export const CHAIN_TEZOS = 1729;
export const CHAIN_ALL_MAINNET = 651940;

/** Hop Protocol L1/L2 chains: ethereum, optimism, arbitrum, polygon, gnosis, nova, base */
export const HOP_SUPPORTED_CHAINS = [1, 10, 42161, 137, 100, 42170, 8453] as const;

export const BRIDGE_CAPABILITY_MATRIX: BridgeCapability[] = [
  {
    provider: 'CCIP',
    sourceChains: [CHAIN_138],
    destinationChains: [CHAIN_ETHEREUM],
    assets: ['WETH', 'WETH10', 'cUSDT', 'cUSDC', 'LINK'],
    method: 'lock_mint',
    custodial: false,
    status: 'active',
  },
  {
    provider: 'CCIP',
    sourceChains: [CHAIN_ETHEREUM],
    destinationChains: [CHAIN_138],
    assets: ['WETH', 'WETH10', 'USDT', 'USDC', 'LINK'],
    method: 'burn_mint',
    custodial: false,
    status: 'active',
  },
  {
    provider: 'Bridge Vault',
    sourceChains: [CHAIN_ETHEREUM, 137, 56],
    destinationChains: [CHAIN_ETHEREUM, 137, 56],
    assets: ['cUSDT', 'cUSDC'],
    method: 'lock_mint',
    custodial: false,
    status: 'active',
  },
  {
    provider: 'Wrap Protocol',
    sourceChains: [CHAIN_ETHEREUM],
    destinationChains: [CHAIN_TEZOS],
    assets: ['USDC', 'USDT', 'DAI', 'WETH'],
    method: 'wrapped',
    custodial: false,
    status: 'active',
    minAmount: '0',
    maxAmount: '1000000000000',
  },
  {
    provider: 'Allbridge Core',
    sourceChains: [CHAIN_ETHEREUM],
    destinationChains: [CHAIN_TEZOS],
    assets: ['USDC', 'USDT'],
    method: 'lock_mint',
    custodial: false,
    status: 'planned',
  },
  // ALL Mainnet (651940) → Ethereum via AlltraAdapter/AlltraCustomBridge
  {
    provider: 'AlltraAdapter',
    sourceChains: [CHAIN_ALL_MAINNET],
    destinationChains: [CHAIN_ETHEREUM],
    assets: ['USDC', 'USDT', 'AUSDC', 'AUSDT', 'WETH'],
    method: 'lock_mint',
    custodial: false,
    status: 'active',
  },
  {
    provider: 'AlltraAdapter',
    sourceChains: [CHAIN_ETHEREUM],
    destinationChains: [CHAIN_ALL_MAINNET],
    assets: ['USDC', 'USDT', 'WETH'],
    method: 'burn_mint',
    custodial: false,
    status: 'active',
  },
  // Hop Protocol: Ethereum ↔ L2 (Optimism, Arbitrum, Polygon, Base, Gnosis, Nova)
  {
    provider: 'Hop Protocol',
    sourceChains: [1, 10, 42161, 137, 100, 42170, 8453],
    destinationChains: [1, 10, 42161, 137, 100, 42170, 8453],
    assets: ['USDC', 'USDT', 'DAI', 'ETH', 'MATIC', 'xDAI'],
    method: 'lock_mint',
    custodial: false,
    status: 'active',
  },
];

export function getCandidateBridges(
  sourceChainId: number,
  destChainId: number,
  asset?: string
): BridgeCapability[] {
  return BRIDGE_CAPABILITY_MATRIX.filter(
    (b) =>
      b.status === 'active' &&
      b.sourceChains.includes(sourceChainId) &&
      b.destinationChains.includes(destChainId) &&
      (!asset || b.assets.includes(asset) || b.assets.includes('native'))
  );
}

export function hasDirectBridge(sourceChainId: number, destChainId: number): boolean {
  return getCandidateBridges(sourceChainId, destChainId).length > 0;
}

export function getNonCustodialBridges(
  sourceChainId: number,
  destChainId: number,
  asset?: string
): BridgeCapability[] {
  return getCandidateBridges(sourceChainId, destChainId, asset).filter((b) => !b.custodial);
}

export function getCandidateBridgesForPlanning(
  sourceChainId: number,
  destChainId: number,
  asset?: string
): BridgeCapability[] {
  const active = getCandidateBridges(sourceChainId, destChainId, asset);
  if (active.length > 0) return active;
  return BRIDGE_CAPABILITY_MATRIX.filter(
    (b) =>
      (b.status === 'active' || b.status === 'planned') &&
      b.sourceChains.includes(sourceChainId) &&
      b.destinationChains.includes(destChainId) &&
      (!asset || b.assets.includes(asset) || b.assets.includes('native'))
  );
}

