import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {C,boards,chapters} from './design-lib.mjs';
import {foundations,responsiveAndStates} from './foundations.mjs';
import {intelligence} from './screens-intelligence.mjs';
import {platform} from './screens-platform.mjs';
import {operations} from './screens-operations.mjs';
import {admin} from './screens-admin.mjs';
import {details} from './details.mjs';
import {createCoverage} from './coverage.mjs';
import {frameSvg,boardSvg,darkPalette} from './render.mjs';
const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..');
for(const dir of ['screens','boards','assets','figma-importer','preview','qa'])fs.mkdirSync(path.join(root,dir),{recursive:true});
foundations();intelligence();platform();operations();admin();details();responsiveAndStates();
for(const b of boards){if(b.section){const nav=b.children.filter(n=>n.x<224&&n.x+n.w<=228);b.children=b.children.filter(n=>!nav.includes(n));b.children.unshift({type:'FRAME',id:'nav-'+b.id,name:'Navigation / '+b.section,x:0,y:0,w:224,h:1000,children:nav,componentKey:'sidebar/'+b.section});}}
for(const id of ['overview','ai-investigation','ai-plan','app-overview','cluster-overview','security','cost','logs']){const b=structuredClone(boards.find(b=>b.id===id));b.id=id+'-dark';b.title+=' / Dark';b.name=b.title;b.theme='dark';b.chapter='09 / Responsive & states';boards.push(b);}
const coverage=createCoverage(root,boards);
boards.sort((a,b)=>chapters.indexOf(a.chapter)-chapters.indexOf(b.chapter));
let y=80;const sections=[];for(const ch of chapters){const bs=boards.filter(b=>b.chapter===ch);if(!bs.length)continue;sections.push({title:ch,y,count:bs.length});bs.forEach((b,i)=>{b.x=80+(i%5)*1520;b.y=y+145+Math.floor(i/5)*1080;});y+=145+Math.ceil(bs.length/5)*1080+85;}
const layout={w:7680,h:y,sections};const model={version:1,name:'Ankra / Intelligent by design',created:'2026-09-08',palette:C,darkPalette,boards,layout};
for(const b of boards)fs.writeFileSync(path.join(root,'screens',b.id+'.svg'),frameSvg(b));
fs.writeFileSync(path.join(root,'ANKRA-COMPLETE-BOARD.svg'),boardSvg(boards,layout));
for(const section of sections){const bs=boards.filter(b=>b.chapter===section.title).map(b=>({...b,y:b.y-section.y+10}));const local={w:layout.w,h:145+Math.ceil(bs.length/5)*1080+85,sections:[{...section,y:10}]};fs.writeFileSync(path.join(root,'boards',section.title.split(' / ')[0]+'-'+section.title.split(' / ')[1].toLowerCase().replaceAll(/[^a-z0-9]+/g,'-')+'.svg'),boardSvg(bs,local));}
fs.writeFileSync(path.join(root,'design-model.json'),JSON.stringify(model));
if(fs.existsSync(path.join(root,'scripts/figma-runtime.js')))fs.writeFileSync(path.join(root,'figma-importer/code.js'),'const DESIGN = '+JSON.stringify(model)+';\n'+fs.readFileSync(path.join(root,'scripts/figma-runtime.js'),'utf8'));
fs.writeFileSync(path.join(root,'preview','model.js'),'window.ANKRA_DESIGN='+JSON.stringify({...model,boards:boards.map(b=>({...b,svg:frameSvg(b,{links:true}),children:undefined}))})+';');
const tokens={$schema:'https://www.designtokens.org/schemas/2025.10/format.json',name:'Ankra',color:Object.fromEntries(Object.entries(C).map(([k,v])=>[k,{$type:'color',$value:{colorSpace:'srgb',components:[1,3,5].map(i=>parseInt(v.slice(i,i+2),16)/255),alpha:1},$description:'Light-mode '+k}])),spacing:Object.fromEntries([4,8,12,16,24,32,48,64].map(v=>[v,{$type:'dimension',$value:{value:v,unit:'px'}}])),radius:{control:{$type:'dimension',$value:{value:7,unit:'px'}},card:{$type:'dimension',$value:{value:12,unit:'px'}}},typography:{family:{$type:'fontFamily',$value:'Inter'},mono:{$type:'fontFamily',$value:'Roboto Mono'}},themes:{light:C,dark:darkPalette}};
fs.writeFileSync(path.join(root,'design-tokens.json'),JSON.stringify(tokens,null,2));
fs.writeFileSync(path.join(root,'design-tokens.css'),':root {\n'+Object.entries(C).map(([k,v])=>'  --ankra-'+k+': '+v+';').join('\n')+'\n  --ankra-font: Inter, Arial, sans-serif;\n  --ankra-radius-control: 7px;\n  --ankra-radius-card: 12px;\n}\n[data-theme="dark"] {\n'+Object.entries(darkPalette).map(([k,v])=>'  --ankra-'+k+': '+v+';').join('\n')+'\n}\n');
if(fs.existsSync('/private/tmp/ankra-inter.woff2'))fs.copyFileSync('/private/tmp/ankra-inter.woff2',path.join(root,'assets/InterVariable.woff2'));
if(fs.existsSync('/private/tmp/ankra-inter-license.txt'))fs.copyFileSync('/private/tmp/ankra-inter-license.txt',path.join(root,'assets/Inter-LICENSE.txt'));
const ids=new Set(boards.map(x=>x.id)),missing=[],overflow=[],nodes=[];function walk(n,b,ox=0,oy=0,parent=null){nodes.push(n);if(n.target&&!ids.has(n.target))missing.push({screen:b.id,target:n.target});if(n.type!=='VECTOR'&&(ox+n.x+n.w>b.w+2||oy+n.y+n.h>b.h+3||ox+n.x<0||oy+n.y<0))overflow.push({screen:b.id,name:n.name,x:ox+n.x,y:oy+n.y,w:n.w,h:n.h});for(const c of n.children||[])walk(c,b,ox+(n.x||0),oy+(n.y||0),n);}for(const b of boards)for(const n of b.children)walk(n,b);
const qa={frames:boards.length,productAndStateFrames:boards.filter(b=>!['foundation','coverage','cover','report'].includes(b.kind)).length,chapters:sections.length,nodes:nodes.length,prototypeLinks:nodes.filter(n=>n.target).length,missingTargets:missing,screenOverflow:overflow,liveRoutes:coverage.filter(r=>r.live).length,coveredRoutes:coverage.length,boardSize:layout.w+' × '+layout.h};fs.writeFileSync(path.join(root,'qa/build-report.json'),JSON.stringify(qa,null,2));console.log(JSON.stringify(qa,null,2));
