import fs from 'node:fs'
import { spawnSync } from 'node:child_process'

const result=spawnSync(process.execPath,['scripts/analyze-kbx-release.mjs'],{stdio:'inherit'})
if(result.status!==0)process.exit(result.status??1)
const impact=JSON.parse(fs.readFileSync('generated/release-impact.json','utf8'))
if(impact.requiredChangeLevel==='major' && !fs.existsSync('docs/release/migration-guide.md')){
  console.error('major KBX change requires docs/release/migration-guide.md')
  process.exit(1)
}
for(const change of impact.changes.filter(x=>x.kind==='component'&&x.change==='version')){
  if(!/^\d+\.\d+\.\d+$/.test(change.to)){console.error(`invalid component semver: ${change.key} ${change.to}`);process.exit(1)}
}
console.log(`release governance PASS: ${impact.requiredChangeLevel}`)
