import fs from 'node:fs'
const read=p=>fs.readFileSync(p,'utf8');const failures=[]
const command=read('packages/kbx-contracts/src/command.ts');for(const t of ['allowedStatuses','requiresDirty','requiresClean','permissionByStatus','confirm'])if(!command.includes(t))failures.push(`command lifecycle missing ${t}`)
const tx=read('packages/kbx-contracts/src/transaction.ts');for(const t of ['KbxRecordStatePolicy','resolveKbxRecordStatePolicy','kbxFieldReadonly'])if(!tx.includes(t))failures.push(`record state policy missing ${t}`)
const commandBar=read('packages/kbx-ui/src/components/KbxCommandBar.vue');for(const t of ['KbxConfirm','effectivePermission','permissionByStatus'])if(!commandBar.includes(t))failures.push(`command safety missing ${t}`)
const lifecycle=read('packages/kbx-ui/src/components/KbxRecordLifecycle.vue');for(const t of ['KbxConflictResolver','KbxWorkflowBar','KbxAuditTrail','Version'])if(!lifecycle.includes(t))failures.push(`record lifecycle component missing ${t}`)
for(const f of ['packages/kbx-ui/src/components/KbxMasterPage.vue','packages/kbx-ui/src/components/KbxTransactionPage.vue'])if(!read(f).includes('KbxRecordLifecycle'))failures.push(`${f} does not integrate lifecycle`)
const item=read('apps/web/src/modules/erp/items/ItemMasterPage.vue');for(const t of ['itemWorkflow','itemStatePolicies','reloadConflict','auditEntries','itemApi.deactivate','useKbxDirtyState'])if(!item.includes(t))failures.push(`ERP-MST-ITEM-001 lifecycle missing ${t}`)
const itemBackend=read('backend/Modules/ERP/Items/Save/Handler.cs')+read('backend/Modules/ERP/Items/Deactivate/Endpoint.cs');for(const t of ['catalog.item_details','KbxConflictProblem.Version','audit.entries','integration.outbox','erp_item_search_projection'])if(!itemBackend.includes(t))failures.push(`item backend lifecycle missing ${t}`)
const order=read('apps/web/src/modules/oms/orders/register/OrderRegisterPage.vue')+read('apps/web/src/modules/oms/orders/register/useOrderRegistration.ts');for(const t of ['orderWorkflow','orderStatePolicies','vm.conflict','vm.auditEntries','vm.confirm','reloadConflict'])if(!order.includes(t))failures.push(`OMS-ORD-002 lifecycle missing ${t}`)
const orderBackend=read('backend/Modules/OMS/Orders/Confirm/Endpoint.cs')+read('backend/Modules/OMS/Orders/Get/Endpoint.cs')+read('backend/Modules/OMS/Orders/Audit/Endpoint.cs');for(const t of ['ORDER_CONFIRMED','Idempotency-Key','command_receipts','KbxConflictProblem.Version','OrderConfirmed','audit.entries','작성','확정'])if(!orderBackend.includes(t))failures.push(`order backend lifecycle missing ${t}`)
const migration=read('backend/Database/Migrations/20260808_016_record_lifecycle.sql');if(!migration.includes('catalog.item_details'))failures.push('normalized item detail write model missing')
const route=read('apps/web/src/router/appRoutes.ts');const nav=read('apps/web/src/shell/navigationCatalog.ts');if(!nav.includes('entry.permissions ?? screen.permissions'))failures.push('navigation permission override missing')
if(!route.includes("'/oms/orders/:orderId/edit'"))failures.push('order edit route missing')
if(failures.length){console.error('Record lifecycle validation failed:\n- '+failures.join('\n- '));process.exit(1)}
console.log('Record lifecycle PASS: state-aware commands, Dirty, workflow, concurrency recovery and audit integrated into T02/T03.')
