import { MeshBuilder, StandardMaterial, Color3, Mesh } from '@babylonjs/core';
import { createAnatomicalSectionMesh } from './AnatomicalMesh';

/**
 * Optional, hidden-by-default translucent neutral-grey human lower-leg
 * silhouette for proportion comparison. Conceptual only — not customer anatomy.
 */
const REF_RINGS = [
  { y: 0.10, rx: 0.50, rz: 0.32 },
  { y: 0.60, rx: 0.28, rz: 0.24 },
  { y: 1.50, rx: 0.30, rz: 0.26 },
  { y: 2.50, rx: 0.34, rz: 0.30 },
  { y: 3.80, rx: 0.52, rz: 0.46, fullBack: 0.10 },
  { y: 4.60, rx: 0.48, rz: 0.42 },
  { y: 5.40, rx: 0.46, rz: 0.40 },
  { y: 6.00, rx: 0.42, rz: 0.38 },
];

export function buildHumanReference(scene) {
  const m = new StandardMaterial('humanRefMat', scene);
  m.diffuseColor = Color3.FromHexString('#6B7280');
  m.specularColor = new Color3(0.05, 0.05, 0.05);
  m.alpha = 0.16;
  m.transparencyMode = 2;
  m.backFaceCulling = false;
  m.sideOrientation = Mesh.DOUBLESIDE;

  let mesh;
  try {
    mesh = createAnatomicalSectionMesh(scene, 'human-ref', REF_RINGS, { closeBottom: true, closeTop: true });
  } catch (_) {
    mesh = MeshBuilder.CreateCylinder('human-ref', { diameterTop: 0.9, diameterBottom: 0.6, height: 6.0, tessellation: 24 }, scene);
  }
  mesh.material = m;
  mesh.isPickable = false;

  return {
    mesh,
    meshes: [mesh],
    setVisible: (v) => { try { mesh.setEnabled(v ? 1 : 0); } catch (_) {} },
    dispose: () => { try { mesh.dispose(); } catch (_) {} try { m.dispose(); } catch (_) {} },
  };
}