import fs from 'node:fs'

const read = (file) => fs.readFileSync(file, 'utf8')
const expect = (condition, message) => {
  if (!condition) {
    console.error(`FAIL: ${message}`)
    process.exit(1)
  }
  console.log(`PASS: ${message}`)
}

const tokenSource = JSON.parse(read('packages/kbx-ui/src/tokens/source/kbx.tokens.json'))
const tokenCss = read('packages/kbx-ui/src/tokens/kbx.css')
const tokenManifest = JSON.parse(read('generated/token-manifest.json'))
const figmaContract = JSON.parse(read('design/figma/variables.contract.json'))

expect(tokenSource.version === '1.10.0', 'v51 design token source version is explicit')
for (const module of ['OMS', 'ERP', 'WMS', 'COMMON']) {
  expect(Boolean(tokenSource.moduleThemes?.[module]?.light), `${module} light module identity theme exists`)
  expect(Boolean(tokenSource.moduleThemes?.[module]?.dark), `${module} dark module identity theme exists`)
  expect(tokenCss.includes(`[data-kbx-module="${module}"]`), `${module} module selector is generated into CSS`)
}
expect(tokenCss.includes('--kbx-color-workspace:'), 'workspace surface token is generated')
expect(tokenCss.includes('--kbx-color-module-accent:'), 'module accent token is generated')
expect(tokenCss.includes('--kbx-shadow-shell:'), 'shell elevation token is generated')
expect(tokenCss.includes('--kbx-color-module-accent: Highlight;'), 'forced-colors module accent fallback exists')
expect(Boolean(tokenManifest.moduleThemes?.OMS), 'generated token manifest retains module themes')
expect(Boolean(figmaContract.moduleThemes?.WMS), 'Figma variable contract retains module themes')

const shell = read('packages/kbx-ui/src/shell/KbxApplicationShell.vue')
const home = read('packages/kbx-ui/src/shell/KbxHomePage.vue')
const pageHeader = read('packages/kbx-ui/src/components/KbxPageHeader.vue')
const screenFrame = read('packages/kbx-ui/src/components/KbxScreenFrame.vue')
expect(shell.includes(':data-kbx-module="focusedModule || \'COMMON\'"'), 'application shell exposes focused module to the theme layer')
expect(home.includes('class="kbx-home__pulse"'), 'home exposes compact operational pulse navigation')
expect(home.includes(':data-kbx-module="item.module"'), 'home module sections carry module identity')
expect(pageHeader.includes('kbx-page-header__module'), 'page header exposes module identity without turning it into status color')
expect(screenFrame.includes(':data-kbx-module="screen.module"'), 'screen frame carries module identity into every standard screen')

const templateAssertions = [
  ['packages/kbx-ui/src/components/KbxListPage.vue', '--kbx-color-module-accent', 'T01 list/search work surface'],
  ['packages/kbx-ui/src/components/KbxMasterPage.vue', '--kbx-shadow-shell', 'T02 master CRUD split surface'],
  ['packages/kbx-ui/src/components/KbxTransactionPage.vue', '--kbx-color-module-accent', 'T03 transaction header/detail surface'],
  ['packages/kbx-ui/src/components/KbxFastEntryPage.vue', 'kbx-fast-entry__guide', 'T04 fast-entry keyboard guide'],
  ['packages/kbx-ui/src/components/KbxMasterDetailPage.vue', 'kbx-master-detail__master', 'T05 master/detail pane hierarchy'],
  ['packages/kbx-ui/src/components/KbxQueuePage.vue', '--kbx-color-warning', 'T06 queue exception hierarchy'],
  ['packages/kbx-ui/src/components/KbxReconcilePage.vue', 'kbx-reconcile-summary', 'T07 reconciliation mismatch summary'],
  ['packages/kbx-ui/src/components/KbxImportPage.vue', 'kbx-import-page__steps', 'T08 import process hierarchy'],
]
for (const [file, marker, label] of templateAssertions) {
  expect(read(file).includes(marker), `${label} is present`)
}

const referenceHtml = read('apps/web/fe-reference/index.html')
const referenceJs = read('apps/web/fe-reference/kbx-fe.js')
const referenceCss = read('apps/web/fe-reference/kbx-fe.css')
expect(referenceHtml.includes('data-module="COMMON"'), 'static HTML reference starts with explicit module identity')
expect(referenceJs.includes('class="home-pulse"'), 'static JavaScript reference mirrors home operational pulse')
expect(referenceJs.includes('data-module="${esc(s.module)}"'), 'static reference screen exposes module identity')
expect(referenceCss.includes('.home-pulse'), 'static reference styles home operational pulse')
expect(referenceCss.includes('[data-module="OMS"]'), 'static reference contains module theme selectors')
expect(referenceCss.includes('[data-template="T01"]'), 'static reference differentiates standard template presentation')

console.log('PASS: FE presentation/workbench hardening v51 contract')
