/**
 * Chainlink Data Feed registry for Vaultfire x402 oracle endpoints.
 *
 * All 15 feeds verified live on-chain on 2026-05-01 via:
 *   node scripts/verify-chainlink-feeds.mjs
 *
 * Addresses sourced from the canonical Chainlink docs
 * (https://data.chain.link/feeds/<chain>/<network>/<pair>) and independently
 * verified by calling latestRoundData() on each — all returned fresh answers
 * (< 24h old, most < 10 min).
 *
 * Vaultfire does NOT operate these feeds. It reads them via RPC and exposes
 * them as x402 paid endpoints for agent consumption. The authoritative source
 * of truth is Chainlink's decentralized oracle network.
 */

export type ChainKey = 'base' | 'avalanche' | 'arbitrum' | 'polygon';
export type PairKey = 'eth-usd' | 'btc-usd' | 'usdc-usd' | 'link-usd' | 'avax-usd' | 'matic-usd';

export interface FeedEntry {
  /** Chainlink proxy contract address on this chain */
  address: string;
  /** Decimals returned by .decimals() — all USD feeds are 8 */
  decimals: number;
  /** Block explorer URL (uses the chain's canonical explorer) */
  explorerUrl: string;
  /** Freshness note — optional human-readable warning */
  note?: string;
}

/**
 * Nested map: chain → pair → feed entry.
 * If a pair is not supported on a chain, it's absent from the inner map.
 */
export const FEEDS: Record<ChainKey, Partial<Record<PairKey, FeedEntry>>> = {
  base: {
    'eth-usd':  { address: '0x71041dddad3595F9CEd3DcCFBe3D1F4b0a16Bb70', decimals: 8, explorerUrl: 'https://basescan.org/address/0x71041dddad3595F9CEd3DcCFBe3D1F4b0a16Bb70' },
    'btc-usd':  { address: '0x64c911996D3c6aC71f9b455B1E8E7266BcbD848F', decimals: 8, explorerUrl: 'https://basescan.org/address/0x64c911996D3c6aC71f9b455B1E8E7266BcbD848F' },
    'usdc-usd': { address: '0x7e860098F58bBFC8648a4311b374B1D669a2bc6B', decimals: 8, explorerUrl: 'https://basescan.org/address/0x7e860098F58bBFC8648a4311b374B1D669a2bc6B' },
    'link-usd': { address: '0x17CAb8FE31E32f08326e5E27412894e49B0f9D65', decimals: 8, explorerUrl: 'https://basescan.org/address/0x17CAb8FE31E32f08326e5E27412894e49B0f9D65' },
  },
  avalanche: {
    'avax-usd': { address: '0x0A77230d17318075983913bC2145DB16C7366156', decimals: 8, explorerUrl: 'https://snowtrace.io/address/0x0A77230d17318075983913bC2145DB16C7366156' },
    'eth-usd':  { address: '0x976B3D034E162d8bD72D6b9C989d545b839003b0', decimals: 8, explorerUrl: 'https://snowtrace.io/address/0x976B3D034E162d8bD72D6b9C989d545b839003b0' },
    'btc-usd':  { address: '0x2779D32d5166BAaa2B2b658333bA7e6Ec0C65743', decimals: 8, explorerUrl: 'https://snowtrace.io/address/0x2779D32d5166BAaa2B2b658333bA7e6Ec0C65743' },
    'link-usd': { address: '0x49ccd9ca821EfEab2b98c60dC60F518E765EDe9a', decimals: 8, explorerUrl: 'https://snowtrace.io/address/0x49ccd9ca821EfEab2b98c60dC60F518E765EDe9a' },
  },
  arbitrum: {
    'eth-usd':  { address: '0x639Fe6ab55C921f74e7fac1ee960C0B6293ba612', decimals: 8, explorerUrl: 'https://arbiscan.io/address/0x639Fe6ab55C921f74e7fac1ee960C0B6293ba612' },
    'btc-usd':  { address: '0x6ce185860a4963106506C203335A2910413708e9', decimals: 8, explorerUrl: 'https://arbiscan.io/address/0x6ce185860a4963106506C203335A2910413708e9' },
    'link-usd': { address: '0x86E53CF1B870786351Da77A57575e79CB55812CB', decimals: 8, explorerUrl: 'https://arbiscan.io/address/0x86E53CF1B870786351Da77A57575e79CB55812CB' },
  },
  polygon: {
    'matic-usd': { address: '0xAB594600376Ec9fD91F8e885dADF0CE036862dE0', decimals: 8, explorerUrl: 'https://polygonscan.com/address/0xAB594600376Ec9fD91F8e885dADF0CE036862dE0', note: 'Legacy MATIC feed — still actively updated on-chain as of 2026-05-01 (21s freshness observed). Chainlink has signaled a POL rebrand; consumers should track both identifiers.' },
    'eth-usd':   { address: '0xF9680D99D6C9589e2a93a78A04A279e509205945', decimals: 8, explorerUrl: 'https://polygonscan.com/address/0xF9680D99D6C9589e2a93a78A04A279e509205945' },
    'btc-usd':   { address: '0xc907E116054Ad103354f2D350FD2514433D57F6f', decimals: 8, explorerUrl: 'https://polygonscan.com/address/0xc907E116054Ad103354f2D350FD2514433D57F6f' },
    'link-usd':  { address: '0xd9FFdb71EbE7496cC440152d43986Aae0AB76665', decimals: 8, explorerUrl: 'https://polygonscan.com/address/0xd9FFdb71EbE7496cC440152d43986Aae0AB76665' },
  },
};

/** Canonical ABI fragment for Chainlink AggregatorV3Interface. */
export const AGGREGATOR_V3_ABI = [
  'function latestRoundData() view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)',
  'function decimals() view returns (uint8)',
  'function description() view returns (string)',
] as const;

/** Flat list of every supported (chain, pair) combination — useful for discovery. */
export function allFeeds(): Array<{ chain: ChainKey; pair: PairKey; entry: FeedEntry }> {
  const out: Array<{ chain: ChainKey; pair: PairKey; entry: FeedEntry }> = [];
  for (const chain of Object.keys(FEEDS) as ChainKey[]) {
    for (const pair of Object.keys(FEEDS[chain]) as PairKey[]) {
      const entry = FEEDS[chain][pair];
      if (entry) out.push({ chain, pair, entry });
    }
  }
  return out;
}

export const SUPPORTED_PAIRS: PairKey[] = ['eth-usd', 'btc-usd', 'usdc-usd', 'link-usd', 'avax-usd', 'matic-usd'];
export const SUPPORTED_CHAINS: ChainKey[] = ['base', 'avalanche', 'arbitrum', 'polygon'];
