import fs from 'node:fs'
const read=p=>fs.readFileSync(p,'utf8')
const failures=[]
const check=(ok,msg)=>{if(!ok)failures.push(msg)}
const componentVersion=(name)=>{const match=manifest.match(new RegExp(`${name}[^\\n]+version:\\s*['\"]([^'\"]+)['\"]`));return match?.[1]??''}
const atLeast=(actual,minimum)=>{const a=actual.split('.').map(Number),m=minimum.split('.').map(Number);for(let i=0;i<3;i++){if((a[i]??0)>(m[i]??0))return true;if((a[i]??0)<(m[i]??0))return false}return true}

const home=read('packages/kbx-ui/src/shell/KbxHomePage.vue')
for(const token of ['attentionExpanded=ref(false)','attentionCollapsedLimit=8','attentionExpandedLimit=24','attentionSourceSummary','aria-controls="kbx-home-attention-list"',':aria-expanded="attentionExpanded"','attentionToggleLabel'])check(home.includes(token),`Home attention disclosure missing: ${token}`)
const manifest=read('packages/kbx-ui/src/registry/componentManifest.ts')
check(atLeast(componentVersion('KbxHomePage'),'1.9.0'),'KbxHomePage must remain >= 1.9.0')

const ref=read('apps/web/fe-reference/kbx-fe.js')
for(const token of ['homeAttentionExpanded:false','function homeAttentionItems()','allAttention.slice(0,8)','homeAttentionToggle','aria-expanded="${state.homeAttentionExpanded}"'])check(ref.includes(token),`Reference Home attention parity missing: ${token}`)

const queue=read('packages/kbx-ui/src/components/KbxQueuePage.vue')
for(const token of ['emptyTitle?:string','emptyDetail?:string','emptyActionLabel?:string','errorTitle?:string','errorDetail?:string','retryLabel?:string',':error-title="errorTitle"',':empty-action-label="emptyActionLabel"'])check(queue.includes(token),`T06 state-recovery copy contract missing: ${token}`)
check(atLeast(componentVersion('KbxQueuePage'),'1.8.0'),'KbxQueuePage must remain >= 1.8.0')

const workApi=read('apps/web/src/modules/wms/work/workApi.ts')
for(const token of ["import.meta.env.VITE_KBX_DEMO_MODE==='true'",'demoRows:WmsWorkRow[]','WMS_WORK_QUERY_NOT_CONNECTED','throw problem'])check(workApi.includes(token),`WMS Work truthful query boundary missing: ${token}`)
check(!/searchWmsWork[^]*?return\s*\[\s*\]/.test(workApi),'WMS Work must not fake a successful empty production query')
const workPage=read('apps/web/src/modules/wms/work/WmsWorkPage.vue')
for(const token of ['queryProblem','empty-title="조건에 맞는 물류 작업이 없습니다."','empty-action-label="기본조건으로 조회"','resetSearch','retry-label="다시 조회"'])check(workPage.includes(token),`WMS Work recovery UX missing: ${token}`)

const guard=read('apps/web/src/modules/wms/shared/wmsFrontendCapability.ts')
for(const token of ['wmsFrontendScenarioMode','WMS_EXECUTION_API_NOT_CONNECTED','Server 검증·Idempotency·Audit'])check(guard.includes(token),`WMS execution guard missing: ${token}`)
for(const rel of ['receiving/WmsReceivingPage.vue','putaway/WmsPutawayPage.vue','counting/WmsCountingPage.vue']){
  const src=read(`apps/web/src/modules/wms/${rel}`)
  for(const token of ['defineProps<{taskId:string}>()','useKbxNetworkState','wmsFrontendScenarioMode','KbxProblemFeedback',' :dismissible="false"','playKbxWmsFeedback',':actions-visible="wmsFrontendScenarioMode"'])check(src.includes(token),`${rel} truthful T09 boundary missing: ${token}`)
  check(!src.includes('const online=ref(true)'),`${rel} must not hard-code network online=true`)
}
const wmsShell=read('packages/kbx-ui/src/wms/KbxWmsMobilePage.vue')
check(wmsShell.includes('actionsVisible?: boolean')&&wmsShell.includes('$slots.actions && actionsVisible'),'WMS Shell must suppress empty action footer when execution input is blocked')
check(atLeast(componentVersion('KbxWmsMobilePage'),'1.7.0'),'KbxWmsMobilePage must remain >= 1.7.0')
for(const rel of ['receiving/receiving.definition.ts','putaway/putaway.definition.ts','counting/counting.definition.ts','work/work.definition.ts']){const src=read(`apps/web/src/modules/wms/${rel}`);const version=src.match(/version:\s*['"](\d+)\.(\d+)\.(\d+)['"]/);check(Boolean(version)&&atLeast(version.slice(1).join('.'),'1.1.0'),`${rel} must remain >= 1.1.0`)}

if(failures.length){console.error('FE workbench/WMS v49 validation failed:');for(const f of failures)console.error(`- ${f}`);process.exit(1)}
console.log('FE workbench/WMS v49 PASS: Home attention disclosure + truthful T06/T09 demo/production boundaries.')
