export interface BGPCommunity {
  name: string;
  desc: string;
  type: 'rfc' | 'carrier' | 'ixp';
  asn?: number;
}

export const BGP_COMMUNITY_DB: Record<string, BGPCommunity> = {
  '65535:666':  { name:'BLACKHOLE',      desc:'RFC 7999 — Null-route this prefix',          type:'rfc' },
  '65535:65281':{ name:'NO_EXPORT',      desc:'RFC 1997 — Do not export to EBGP peers',      type:'rfc' },
  '65535:65282':{ name:'NO_ADVERTISE',   desc:'RFC 1997 — Do not advertise to any peer',     type:'rfc' },
  '65535:65283':{ name:'NO_EXPORT_SUBCONFED', desc:'RFC 1997 — No export to sub-AS',        type:'rfc' },
  // Lumen/CenturyLink 3356
  '3356:2':  { name:'Lumen Peer',        desc:'Lumen — Learned from settlement-free peer',   type:'carrier', asn:3356 },
  '3356:3':  { name:'Lumen Customer',    desc:'Lumen — Learned from customer',               type:'carrier', asn:3356 },
  '3356:100':{ name:'Lumen Blackhole',   desc:'Lumen — RTBH trigger',                        type:'carrier', asn:3356 },
  // NTT 2914
  '2914:420':{ name:'NTT Peer',          desc:'NTT — Settlement-free peer route',            type:'carrier', asn:2914 },
  '2914:421':{ name:'NTT Customer',      desc:'NTT — Downstream customer route',             type:'carrier', asn:2914 },
  '2914:666':{ name:'NTT Blackhole',     desc:'NTT — RTBH trigger',                          type:'carrier', asn:2914 },
  // Cogent 174
  '174:21000':{ name:'Cogent Peer',      desc:'Cogent — Learned from peer',                  type:'carrier', asn:174 },
  '174:22000':{ name:'Cogent Customer',  desc:'Cogent — Learned from customer',              type:'carrier', asn:174 },
  '174:666':  { name:'Cogent Blackhole', desc:'Cogent — RTBH trigger',                       type:'carrier', asn:174 },
  // HE 6939
  '6939:7000':{ name:'HE RTBH',          desc:'Hurricane Electric — Remotely triggered blackhole', type:'carrier', asn:6939 },
  // Telia 1299
  '1299:35000':{ name:'Telia RTBH',      desc:'Telia — Remotely triggered blackhole',        type:'carrier', asn:1299 },
  '1299:3000': { name:'Telia Peer',      desc:'Telia — Learned from peer',                   type:'carrier', asn:1299 },
  // DTAG 3320
  '3320:1278':{ name:'DTAG Peer',        desc:'Deutsche Telekom — Peering route',            type:'carrier', asn:3320 },
  '3320:2001':{ name:'DTAG Customer',    desc:'Deutsche Telekom — Customer route',           type:'carrier', asn:3320 },
  '3320:9900':{ name:'DTAG Blackhole',   desc:'Deutsche Telekom — RTBH trigger',             type:'carrier', asn:3320 },
  // Cloudflare 13335
  '13335:10000':{ name:'CF Customer',    desc:'Cloudflare — Customer route',                 type:'carrier', asn:13335 },
  '13335:10010':{ name:'CF Peering',     desc:'Cloudflare — Learned via peering',            type:'carrier', asn:13335 },
  '13335:20050':{ name:'CF Blackhole',   desc:'Cloudflare — RTBH trigger',                   type:'carrier', asn:13335 },
  // Zayo 6461
  '6461:9000':{ name:'Zayo Blackhole',   desc:'Zayo — RTBH trigger',                         type:'carrier', asn:6461 },
  // DE-CIX 6695
  '6695:1000':{ name:'DE-CIX RS',        desc:'DE-CIX Frankfurt — Route server export',      type:'ixp', asn:6695 },
  '6695:1001':{ name:'DE-CIX RS peer',   desc:'DE-CIX — Received from route server peer',   type:'ixp', asn:6695 },
  // AMS-IX 1200
  '1200:100': { name:'AMS-IX RS',        desc:'AMS-IX — Route server export',                type:'ixp', asn:1200 },
  // LINX 5459
  '5459:1001':{ name:'LINX RS',          desc:'LINX — Route server export',                  type:'ixp', asn:5459 },
  // Seabone/TI 6762
  '6762:30':  { name:'Seabone Customer', desc:'Telecom Italia Seabone — Customer route',     type:'carrier', asn:6762 },
  // Turkcell 9121
  '9121:666': { name:'Turkcell BH',      desc:'Turkcell — RTBH trigger',                     type:'carrier', asn:9121 },
};

export function decodeCommunities(communityList: any[]): Array<{ raw: string; known: BGPCommunity | null }> {
  if (!Array.isArray(communityList)) return [];
  return communityList.map(c => {
    const key = Array.isArray(c) ? c.join(':') : String(c);
    const known = BGP_COMMUNITY_DB[key] || null;
    return { raw: key, known };
  });
}
