import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import crypto from 'node:crypto';
import {spawnSync} from 'node:child_process';
import {fileURLToPath} from 'node:url';
const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..');
const selected=['START-HERE.md','DESIGN-SYSTEM.md','AUDIT.md','HANDOFF.md','index.html','ANKRA-COMPLETE-BOARD.svg','design-model.json','design-tokens.json','design-tokens.css','route-coverage.csv','boards','screens','assets','figma-importer','preview','scripts','qa','audit/route-coverage.json','audit/live-routes.json'];
const walk=relative=>fs.statSync(path.join(root,relative)).isDirectory()?fs.readdirSync(path.join(root,relative)).filter(n=>!n.startsWith('.')).flatMap(n=>walk(path.join(relative,n))):[relative];
const files=selected.flatMap(walk).sort();
if(files.some(p=>p.includes('live-structure-')||p.includes('source-inventory')))throw new Error('Private audit evidence must not be shared');
const manifest={name:'Ankra — Intelligent by design',version:'2026-09-08',fileCount:files.length,privateEvidenceExcluded:true,files:files.map(file=>{const data=fs.readFileSync(path.join(root,file));return {path:file,bytes:data.length,sha256:crypto.createHash('sha256').update(data).digest('hex')};})};
fs.writeFileSync(path.join(root,'PACKAGE-MANIFEST.json'),JSON.stringify(manifest,null,2));
const tmp=fs.mkdtempSync(path.join(os.tmpdir(),'ankra-design-package-')),staged=path.join(tmp,'Ankra-Redesign-2026-09-08.zip');
const result=spawnSync('zip',['-q','-X',staged,...[...files,'PACKAGE-MANIFEST.json'].map(file=>path.join(path.basename(root),file))],{cwd:path.dirname(root),encoding:'utf8'});if(result.status!==0)throw new Error(result.stderr||'ZIP packaging failed');
const target=path.join(path.dirname(root),'Ankra-Redesign-2026-09-08.zip');fs.copyFileSync(staged,target);console.log(JSON.stringify({zip:target,files:files.length+1,bytes:fs.statSync(target).size,sha256:crypto.createHash('sha256').update(fs.readFileSync(target)).digest('hex'),privateEvidenceExcluded:true},null,2));
