// Independent structural audit of UB. Groups every vertex by its dominant bone,
// then reports each anatomical group's vertex count, centroid, and bounding box in
// model space. Reveals off-midline parts (dorsal), mis-located parts (lip), and
// which primitive/material each region landed in after the split.
import { NodeIO } from '@gltf-transform/core';
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';

const io = new NodeIO().registerExtensions(ALL_EXTENSIONS);
const doc = await io.read(process.argv[2]);
const root = doc.getRoot();
const joints = root.listSkins()[0].listJoints();

const groupOf = (name) => {
  if (name.startsWith('Spine')) return 'body';
  if (name.startsWith('Dorsal')) return 'dorsal';
  if (name.startsWith('Pec_Fin_Left')) return 'pec_L';
  if (name.startsWith('Pec_Fin_Right')) return 'pec_R';
  if (name.startsWith('Left_Rear_Belly')) return 'belly_L';
  if (name.startsWith('Right_Rear_Belly')) return 'belly_R';
  if (name.startsWith('Rear_Center_Fin')) return 'anal';
  if (name.startsWith('Tail_Fork')) return 'tail';
  if (name.startsWith('Joint')) return 'joint(tail-ray?)';
  return `?:${name}`;
};
const jointGroup = joints.map((n) => groupOf(n.getName()));

const mesh = root.listMeshes()[0];
const prim = mesh.listPrimitives().reduce((a, b) => (b.getIndices().getCount() > a.getIndices().getCount() ? b : a));
const pos = prim.getAttribute('POSITION');
const jA = prim.getAttribute('JOINTS_0');
const wA = prim.getAttribute('WEIGHTS_0');
const n = pos.getCount();

const G = {};
const p = [0, 0, 0], j = [0, 0, 0, 0], w = [0, 0, 0, 0];
for (let i = 0; i < n; i++) {
  jA.getElement(i, j); wA.getElement(i, w);
  let best = 0, bestW = -1;
  for (let k = 0; k < 4; k++) if (w[k] > bestW) { bestW = w[k]; best = j[k]; }
  const g = jointGroup[best];
  pos.getElement(i, p);
  const e = G[g] || (G[g] = { c: 0, sum: [0, 0, 0], min: [1e9, 1e9, 1e9], max: [-1e9, -1e9, -1e9] });
  e.c++;
  for (let d = 0; d < 3; d++) { e.sum[d] += p[d]; e.min[d] = Math.min(e.min[d], p[d]); e.max[d] = Math.max(e.max[d], p[d]); }
}

const f = (x) => x.toFixed(3).padStart(8);
console.log('axes: X Y Z  (model space)\n');
for (const [g, e] of Object.entries(G).sort((a, b) => b[1].c - a[1].c)) {
  const ctr = e.sum.map((s) => s / e.c);
  const sz = e.max.map((m, d) => m - e.min[d]);
  console.log(`${g.padEnd(16)} n=${String(e.c).padStart(5)}  centroid[${ctr.map(f).join(',')}]  size[${sz.map(f).join(',')}]`);
}

// whole-model bbox to know which axis is length/height/depth
const bb = Object.values(G).reduce((acc, e) => {
  for (let d = 0; d < 3; d++) { acc.min[d] = Math.min(acc.min[d], e.min[d]); acc.max[d] = Math.max(acc.max[d], e.max[d]); }
  return acc;
}, { min: [1e9, 1e9, 1e9], max: [-1e9, -1e9, -1e9] });
console.log(`\nwhole bbox  min[${bb.min.map(f).join(',')}]  max[${bb.max.map(f).join(',')}]  size[${bb.max.map((m, d) => f(m - bb.min[d])).join(',')}]`);
