// KAR-905 / P3.1 — "VERIFIZIEREN per Test statt annehmen": every P2.x row/
// label parser (material/SBM/LOGISTICS/RMR/LC-CN/CO2e) was built on the
// canonical field model from day one, matching a header/label cell against
// BOTH a field's labelDe AND labelEn (see each module's "Canonical registry
// bridge" section — verified identical exact/alias tier logic across all six
// during the P3.1 inventory). canonical-fields.ts's own module header is
// explicit that these EN labels are "a working English translation ... NOT
// verified against a real shipped English-language QAF template" for every
// module except SUMMARY/MANUFACTURING. This file closes that verification
// gap: for every registered field of each module, its literal labelEn text
// is fed through that module's own public header/label matcher and must
// resolve at tier-1 (exact, confidence 1) — proving the EN half of the
// canonical registry is actually reachable through each parser's matching
// code path, not just present as unread data.
//
// Deliberately does NOT re-verify SUMMARY/MANUFACTURING here — those are
// already covered by the pre-existing "V9 SUMMARY layout"/EN-header describe
// blocks in summary-metrics.test.ts and qaf-parser.test.ts (both use real EN
// label text and were passing before this PR).

import { describe, it, expect } from 'vitest'
import { byModule } from '../canonical-model'
import { matchMaterialHeaderColumn } from '../material-parser'
import { matchSbmHeaderColumn } from '../sbm-parser'
import { matchLogisticsHeaderColumn } from '../logistics-parser'
import { matchRmrHeaderColumn } from '../rmr-parser'
import { matchLccnLabelCell } from '../lccn-parser'
import { matchCo2eSummaryLabelCell, matchCo2eMaterialHeaderColumn } from '../co2e-parser'
import type { QafModule } from '../canonical-fields.types'

describe('EN header/label matching — MATERIAL', () => {
  const fields = byModule('MATERIAL' as QafModule)
  it('registry is non-empty (sanity, would silently no-op the loop below)', () => {
    expect(fields.length).toBeGreaterThan(0)
  })
  for (const f of fields) {
    it(`resolves labelEn "${f.labelEn}" to canonical id ${f.id} at tier-1 (exact)`, async () => {
      const match = await matchMaterialHeaderColumn(f.labelEn)
      expect(match).not.toBeNull()
      expect(match?.confidence).toBe(1)
    })
  }
})

describe('EN header/label matching — SBM-DEVICES-FWZ', () => {
  const fields = byModule('SBM' as QafModule)
  it('registry is non-empty', () => {
    expect(fields.length).toBeGreaterThan(0)
  })
  for (const f of fields) {
    it(`resolves labelEn "${f.labelEn}" to canonical id ${f.id} at tier-1 (exact)`, async () => {
      const match = await matchSbmHeaderColumn(f.labelEn)
      expect(match).not.toBeNull()
      expect(match?.confidence).toBe(1)
    })
  }
})

describe('EN header/label matching — LOGISTICS&CUSTOM', () => {
  const fields = byModule('LOGISTICS' as QafModule)
  it('registry is non-empty', () => {
    expect(fields.length).toBeGreaterThan(0)
  })
  for (const f of fields) {
    it(`resolves labelEn "${f.labelEn}" to canonical id ${f.id} at tier-1 (exact)`, async () => {
      const match = await matchLogisticsHeaderColumn(f.labelEn)
      expect(match).not.toBeNull()
      expect(match?.confidence).toBe(1)
    })
  }
})

describe('EN header/label matching — RAW MATERIAL RISKS', () => {
  const fields = byModule('RMR' as QafModule)
  it('registry is non-empty', () => {
    expect(fields.length).toBeGreaterThan(0)
  })
  for (const f of fields) {
    it(`resolves labelEn "${f.labelEn}" to canonical id ${f.id} at tier-1 (exact)`, async () => {
      const match = await matchRmrHeaderColumn(f.labelEn)
      expect(match).not.toBeNull()
      expect(match?.confidence).toBe(1)
    })
  }
})

describe('EN header/label matching — LC-CN', () => {
  const fields = byModule('LC_CN' as QafModule).filter((f) => f.level === 'summary')
  it('registry is non-empty', () => {
    expect(fields.length).toBeGreaterThan(0)
  })
  for (const f of fields) {
    it(`resolves labelEn "${f.labelEn}" to canonical id ${f.id} at tier-1 (exact)`, async () => {
      const match = await matchLccnLabelCell(f.labelEn)
      expect(match).not.toBeNull()
      expect(match?.confidence).toBe(1)
    })
  }
})

describe('EN header/label matching — CO2e summary panel', () => {
  const fields = byModule('CO2E' as QafModule).filter((f) => f.level === 'summary')
  it('registry is non-empty', () => {
    expect(fields.length).toBeGreaterThan(0)
  })
  for (const f of fields) {
    it(`resolves labelEn "${f.labelEn}" to canonical id ${f.id} at tier-1 (exact)`, async () => {
      const match = await matchCo2eSummaryLabelCell(f.labelEn)
      expect(match).not.toBeNull()
      expect(match?.confidence).toBe(1)
    })
  }
})

describe('EN header/label matching — CO2e material rows', () => {
  const fields = byModule('CO2E' as QafModule).filter((f) => f.level === 'row')
  it('registry is non-empty', () => {
    expect(fields.length).toBeGreaterThan(0)
  })
  for (const f of fields) {
    it(`resolves labelEn "${f.labelEn}" to canonical id ${f.id} at tier-1 (exact)`, async () => {
      const match = await matchCo2eMaterialHeaderColumn(f.labelEn)
      expect(match).not.toBeNull()
      expect(match?.confidence).toBe(1)
    })
  }
})
