import fs from 'node:fs'
import path from 'node:path'
const root=process.cwd(); const failures=[]
const read=p=>fs.readFileSync(path.join(root,p),'utf8')
const must=(text,needles,scope)=>needles.forEach(n=>{if(!text.includes(n))failures.push(`${scope} missing ${n}`)})

const ui=read('packages/kbx-contracts/src/ui.ts')
must(ui,['export interface KbxSummaryItem','export interface KbxQuickFilterItem'],'shared template surface contracts')

const screenFrame=read('packages/kbx-ui/src/components/KbxScreenFrame.vue')
must(screenFrame,['data-kbx-surface="page-header"','data-kbx-surface="command-bar"'],'ScreenFrame surface markers')

const list=read('packages/kbx-ui/src/components/KbxListPage.vue')
must(list,['quickFilters?: KbxQuickFilterItem[]','summaryItems?: KbxSummaryItem[]','<KbxQuickFilterBar :items="quickFilters"','<KbxSummaryBar :items="summaryItems"','data-kbx-surface="search"','data-kbx-surface="content"','data-kbx-surface="summary"'],'T01 first-class surfaces')

const tx=read('packages/kbx-ui/src/components/KbxTransactionPage.vue')
must(tx,['summaryItems?:KbxSummaryItem[]','<KbxSummaryBar :items="summaryItems" align="end"','data-kbx-surface="header-form"','data-kbx-surface="detail-grid"','data-kbx-surface="summary"','data-kbx-surface="record-lifecycle"'],'T03 first-class surfaces')

const fastEntry=read('packages/kbx-ui/src/components/KbxFastEntryPage.vue')
must(fastEntry,['errors?:KbxValidationError[]','summaryItems?:KbxSummaryItem[]','<KbxValidationSummary :errors="errors"','<KbxSummaryBar :items="summaryItems" align="end"','data-kbx-surface="keyboard-guide"','data-kbx-surface="editable-grid"','data-kbx-surface="validation"'],'T04 first-class surfaces')

const queue=read('packages/kbx-ui/src/components/KbxQueuePage.vue')
must(queue,['summaryItems?:KbxSummaryItem[]','exceptionCounters?:KbxWorkQueueCounter[]','<KbxExceptionSummary :counters="exceptionCounters"','exceptionFilter:[string|null]','data-kbx-surface="work-summary"','data-kbx-surface="exception-summary"','data-kbx-surface="queue/content"'],'T06 exception-driven surfaces')

const otherTemplates={
  'T02': ['packages/kbx-ui/src/components/KbxMasterPage.vue',['data-kbx-surface="detail"','data-kbx-surface="record-lifecycle"']],
  'T05': ['packages/kbx-ui/src/components/KbxMasterDetailPage.vue',['summaryItems?:KbxSummaryItem[]','<KbxSummaryBar :items="summaryItems"','data-kbx-surface="master"','data-kbx-surface="detail"','data-kbx-surface="bottom/history"']],
  'T07': ['packages/kbx-ui/src/components/KbxReconcilePage.vue',['data-kbx-surface="criteria/search"','data-kbx-surface="summary"','data-kbx-surface="comparison-grid"','data-kbx-surface="resolution-action"']],
  'T08': ['packages/kbx-ui/src/components/KbxImportPage.vue',['data-kbx-surface="import-content"']],
  'T09': ['packages/kbx-ui/src/wms/KbxWmsMobilePage.vue',['data-kbx-surface="mobile-header"','data-kbx-surface="network-state"','data-kbx-surface="instruction/content"','data-kbx-surface="sticky-actions"']],
}
for(const [code,[file,needles]] of Object.entries(otherTemplates)) must(read(file),needles,`${code} surface markers`)
const excel=read('packages/kbx-ui/src/components/KbxExcelImport.vue')
must(excel,['data-kbx-surface="progress-steps"','data-kbx-surface="file"','data-kbx-surface="mapping"','data-kbx-surface="validation"','data-kbx-surface="result"'],'T08 Excel workflow surfaces')

const homeLogic=read('packages/kbx-ui/src/shell/homeNavigation.ts')
must(homeLogic,["pinned:'고정'","tab.pinned?'pinned':'open'",'Boolean(b.pinned)'],'Home pinned priority')
const home=read('packages/kbx-ui/src/shell/KbxHomePage.vue')
must(home,['toggleFavorite:[screenId:string]','favoriteIds','openCounts','aria-keyshortcuts="Control+K"','class="favorite"',':aria-pressed="isFavorite(entry.screenId)"','열림 {{openCount(entry.screenId)}}'],'Home actionable navigation')
const appFrame=read('apps/web/src/shell/KbxAppFrame.vue')
must(appFrame,['@toggle-favorite="store.toggleFavorite"'],'Home favorite persistence wiring')
const side=read('packages/kbx-ui/src/shell/KbxSideNavigation.vue')
must(side,['aria-keyshortcuts="Control+K"',':aria-pressed="isFavorite(entry.screenId)"'],'Side navigation shortcut/favorite state')
const globalHeader=read('packages/kbx-ui/src/shell/KbxGlobalHeader.vue')
must(globalHeader,['aria-keyshortcuts="Control+K"'],'Global header shortcut discoverability')

const itemPrice=read('apps/web/src/modules/erp/item-prices/ItemPriceFastEntryPage.vue')
must(itemPrice,[':errors="vm.errors.value"',':summary-items="summary"'],'T04 golden adoption')
const work=read('apps/web/src/modules/wms/work/WmsWorkPage.vue')
must(work,[':summary-items="summaryItems"',':exception-counters="exceptionCounters"','@exception-filter="filterException"'],'T06 golden adoption')
const order=read('apps/web/src/modules/oms/orders/register/OrderRegisterPage.vue')
must(order,[':summary-items="summaryItems"'],'T03 golden adoption')
const inventory=read('apps/web/src/modules/erp/inventory/InventoryPage.vue')
must(inventory,[':summary-items="summaryItems"'],'T05 golden adoption')

const validation=read('packages/kbx-ui/src/components/KbxValidationSummary.vue')
must(validation,['var(--kbx-color-danger-border)','var(--kbx-color-danger-surface)','var(--kbx-font-sm)'],'ValidationSummary token normalization')
const exceptions=read('packages/kbx-ui/src/components/KbxExceptionSummary.vue')
must(exceptions,['var(--kbx-space-2)','var(--kbx-control-lg)','var(--kbx-font-sm)'],'ExceptionSummary token normalization')

const manifest=read('packages/kbx-ui/src/registry/componentManifest.ts')
must(manifest,["KbxListPage', category: 'template'","KbxTransactionPage', category: 'template'","KbxFastEntryPage', category: 'template'","KbxQueuePage', category: 'template'","KbxHomePage', category: 'shell'"],'v32 component manifest coverage')

if(failures.length){console.error(failures.join('\n'));process.exit(1)}
console.log('v32 template/component/home navigation validation passed: first-class standard surfaces, T01/T03/T04/T06 composition contracts, T01-T09 surface observability, exception-driven queue adoption, pinned/favorite Home navigation, shortcut accessibility, and token normalization.')
