#!/usr/bin/env node
// Verify CCIP/LiFi support for 138, 42793, 651940. Run: node scripts/verify-tezos-etherlink-support.js

const LIFI_URL = 'https://li.quest/v1/chains';
const CCIP_URL = 'https://docs.chain.link/ccip/supported-networks';

async function fetchText(url) {
  const res = await fetch(url, {
    headers: {
      'user-agent': 'proxmox-operator-support-check/1.0',
      accept: 'text/html,application/json;q=0.9,*/*;q=0.8',
    },
  });
  if (!res.ok) throw new Error(`${url} returned ${res.status}`);
  return res.text();
}

async function fetchJson(url) {
  const res = await fetch(url, {
    headers: {
      'user-agent': 'proxmox-operator-support-check/1.0',
      accept: 'application/json',
    },
  });
  if (!res.ok) throw new Error(`${url} returned ${res.status}`);
  return res.json();
}

async function main() {
  console.log('Tezos/Etherlink support check\n');
  try {
    const lifi = await fetchJson(LIFI_URL);
    const chains = lifi.chains || [];
    const ids = chains.map((c) => c.id || c.chainId).filter(Boolean);
    const etherlink = chains.find((c) => (c.id || c.chainId) === 42793);
    console.log('LiFi chains include 138:', ids.includes(138));
    console.log('LiFi chains include 42793:', ids.includes(42793));
    console.log('LiFi chains include 651940:', ids.includes(651940));
    if (etherlink) {
      console.log('LiFi Etherlink entry:', JSON.stringify({
        id: etherlink.id || etherlink.chainId,
        key: etherlink.key,
        name: etherlink.name,
        coin: etherlink.coin,
        mainnet: etherlink.mainnet,
      }));
    }
  } catch (e) {
    console.log('LiFi error:', e.message);
  }
  try {
    const html = await fetchText(CCIP_URL);
    const hasEtherlinkMention = html.includes('42793') || html.toLowerCase().includes('etherlink');
    console.log('\nCCIP page mentions 42793/Etherlink:', hasEtherlinkMention);
    console.log('CCIP page mention is not treated as support without selector/router confirmation.');
  } catch (e) {
    console.log('CCIP error:', e.message);
  }
  console.log('\nJumper: still verify manually; see docs/07-ccip/TEZOS_JUMPER_SUPPORT_MATRIX.md');
}

main().catch((e) => { console.error(e); process.exit(1); });
