/**
 * Vaultfire Protocol — Multi-Chain Configuration
 *
 * All contract addresses are verified and deployed on mainnet.
 * Base is the primary hub chain. Avalanche, Arbitrum, and Polygon
 * are spoke chains with bridges pointing back to Base.
 */

// ============================================================================
// Types
// ============================================================================

export type VaultfireChain = 'base' | 'avalanche' | 'arbitrum' | 'polygon';

export interface ChainContracts {
  identity: string;
  partnership: string;
  accountability: string;
  reputation: string;
  bridge: string;
  vns?: string;
}

export interface ChainConfig {
  name: string;
  chainId: number;
  rpcUrl: string;
  explorer: string;
  nativeCurrency: string;
  contracts: ChainContracts;
}

// ============================================================================
// Contract Addresses — Verified on Mainnet
// ============================================================================

// Production addresses verified on-chain 2026-04-29
// Source: https://theloopbreaker.com/api/contracts (134 contracts live across 4 chains)
export const CHAIN_CONFIG: Record<VaultfireChain, ChainConfig> = {
  base: {
    name: 'Base',
    chainId: 8453,
    rpcUrl: 'https://mainnet.base.org',
    explorer: 'https://basescan.org',
    nativeCurrency: 'ETH',
    contracts: {
      identity: '0xa7BD20bf5De63df949cA5Be2F20835978eCba81A',
      partnership: '0x01C479F0c039fEC40c0Cf1c5C921bab457d57441',
      accountability: '0x6750D28865434344e04e1D0a6044394b726C3dfE',
      reputation: '0x98afd1440B2238D73c1394720277a6d031fCbbD0',
      bridge: '0x62edf0cBc8Fa3d98B6A07197DFE9cb1c128a7eC7',
      vns: '0xA9e6c2c0a731F1f56F6720Dfac2eB1440Ab9453a',
    },
  },
  avalanche: {
    name: 'Avalanche',
    chainId: 43114,
    rpcUrl: 'https://api.avax.network/ext/bc/C/rpc',
    explorer: 'https://snowtrace.io',
    nativeCurrency: 'AVAX',
    contracts: {
      identity: '0x7448057C95Fb8a8B974a566cdcc9Cd042166A3f8',
      partnership: '0xDC8447c66fE9D9c7D54607A98346A15324b7985D',
      accountability: '0x376831fB2457E34559891c32bEb61c442053C066',
      reputation: '0x5DB1B4b412b80d03819395794fCcF5A73BF30656',
      bridge: '0x1762e484A48558Ea5Cd056ac2CDf43FBbA52ba58',
      // VNS not deployed on Avalanche
    },
  },
  arbitrum: {
    name: 'Arbitrum',
    chainId: 42161,
    rpcUrl: 'https://arbitrum-one.publicnode.com',
    explorer: 'https://arbiscan.io',
    nativeCurrency: 'ETH',
    contracts: {
      identity: '0x83dd216449B3F0574E39043ECFE275946fa492e9',
      partnership: '0xdB54B8925664816187646174bdBb6Ac658A55a5F',
      accountability: '0xef3A944f4d7bb376699C83A29d7Cb42C90D9B6F0',
      reputation: '0x8B8Ba34F8AAB800F0Ba8391fb1388c6EFb911F92',
      bridge: '0x7b9293B05Bd0fcb7458d06A364078761B8E8848e',
      vns: '0x7448057C95Fb8a8B974a566cdcc9Cd042166A3f8',
    },
  },
  polygon: {
    name: 'Polygon',
    chainId: 137,
    rpcUrl: 'https://polygon-bor-rpc.publicnode.com',
    explorer: 'https://polygonscan.com',
    nativeCurrency: 'POL',
    contracts: {
      identity: '0xD9bF6D92a1D9ee44a48c38481c046a819CBdf2ba',
      partnership: '0x83dd216449B3F0574E39043ECFE275946fa492e9',
      accountability: '0xdB54B8925664816187646174bdBb6Ac658A55a5F',
      reputation: '0x54e00081978eE2C8d9Ada8e9975B0Bb543D06A55',
      bridge: '0x7b9293B05Bd0fcb7458d06A364078761B8E8848e',
      vns: '0x7448057C95Fb8a8B974a566cdcc9Cd042166A3f8',
    },
  },
};

/**
 * Get chain config by name or chain ID
 */
export function getChainConfig(chainOrId: VaultfireChain | number): ChainConfig {
  if (typeof chainOrId === 'number') {
    const entry = Object.entries(CHAIN_CONFIG).find(([, c]) => c.chainId === chainOrId);
    if (!entry) throw new Error(`Unsupported chain ID: ${chainOrId}`);
    return entry[1];
  }
  const config = CHAIN_CONFIG[chainOrId];
  if (!config) throw new Error(`Unsupported chain: ${chainOrId}`);
  return config;
}

/**
 * List all supported chains
 */
export function getSupportedChains(): VaultfireChain[] {
  return Object.keys(CHAIN_CONFIG) as VaultfireChain[];
}
