export type PostureGlossaryTermId =
  | 'x402'
  | 'iso20022'
  | 'forward-canonical'
  | 'reference-asset'
  | 'cw-public-network'
  | 'transport-active'
  | 'gru'

export interface PostureGlossaryTerm {
  id: PostureGlossaryTermId
  title: string
  shortLabel: string
  summary: string
  methodology: string
}

export const postureGlossaryTerms: PostureGlossaryTerm[] = [
  {
    id: 'gru',
    title: 'GRU instrument',
    shortLabel: 'GRU',
    summary: 'A Global Reserve Unit instrument issued under DBIS taxonomy on Chain 138 or represented on public networks.',
    methodology:
      'The explorer marks GRU when token metadata, registry tags, or curated catalog entries align with the GRU transport and compliance model documented in the GRU guide.',
  },
  {
    id: 'x402',
    title: 'x402 readiness',
    shortLabel: 'x402 ready',
    summary: 'The token surface exposes the typed-data and domain metadata commonly required for HTTP-native payment authorization flows.',
    methodology:
      'Readiness is inferred from detected signing surfaces (EIP-712 domain, ERC-5267, and ERC-2612 or ERC-3009). It describes technical capability — not a guarantee that a live x402 merchant endpoint exists.',
  },
  {
    id: 'iso20022',
    title: 'ISO-20022 alignment',
    shortLabel: 'ISO-20022',
    summary: 'The asset is modeled as part of the ISO-20022-aligned settlement and reporting posture for institutional messaging.',
    methodology:
      'This badge reflects GRU metadata and governance expectations around supervised disclosure — not a claim that every transfer is already formatted as an ISO-20022 message on-chain.',
  },
  {
    id: 'forward-canonical',
    title: 'Forward-canonical posture',
    shortLabel: 'forward canonical',
    summary: 'The asset version is the forward-looking canonical representation operators should wire for new integrations.',
    methodology:
      'Used when a token family has legacy or parallel deployments. Prefer forward-canonical addresses for routers, wallets, and explorer deep links unless a migration note says otherwise.',
  },
  {
    id: 'reference-asset',
    title: 'Reference asset',
    shortLabel: 'reference asset',
    summary: 'A non-GRU mirrored or externally issued asset shown for routing, liquidity, or price context.',
    methodology:
      'Reference assets help explain cross-chain routes and pool composition. They are not implied to be DBIS-issued compliant instruments unless separately tagged.',
  },
  {
    id: 'cw-public-network',
    title: 'cW public-network representation',
    shortLabel: 'cW public-network',
    summary: 'A wrapped GRU instrument activated on a public network (for example mainnet cWUSDC) while Chain 138 remains the program ledger.',
    methodology:
      'Public-network overlays use cW* naming. Liquidity and bridge lanes may reference these addresses even when the canonical compliant token lives on Chain 138.',
  },
  {
    id: 'transport-active',
    title: 'transportActive (config compatibility)',
    shortLabel: 'transportActive',
    summary: 'Legacy JSON key indicating whether a public-network transport overlay is active in published `/config` manifests.',
    methodology:
      'Machine consumers should treat this as a v1 compatibility field. A future v2 schema will expose `publicNetworkActive` aliases before old keys are removed.',
  },
]

const labelToTermId: Record<string, PostureGlossaryTermId> = {
  gru: 'gru',
  'x402 ready': 'x402',
  'x402 not ready': 'x402',
  'iso-20022': 'iso20022',
  'iso-20022 aligned': 'iso20022',
  'iso-20022 unclear': 'iso20022',
  'forward canonical': 'forward-canonical',
  'reference asset': 'reference-asset',
  wrapped: 'cw-public-network',
  transportactive: 'transport-active',
}

export function resolvePostureTermId(label: string): PostureGlossaryTermId | null {
  const normalized = label.trim().toLowerCase()
  if (labelToTermId[normalized]) return labelToTermId[normalized]
  if (normalized.startsWith('cw public-network')) return 'cw-public-network'
  if (normalized.startsWith('forward ')) return 'forward-canonical'
  if (normalized.includes('transportactive') || normalized.includes('transport active')) return 'transport-active'
  return null
}

export function getPostureGlossaryTerm(id: PostureGlossaryTermId): PostureGlossaryTerm | undefined {
  return postureGlossaryTerms.find((term) => term.id === id)
}
