import ProstheticAssembly from './ProstheticAssembly';
import ComponentModel from './ComponentModel';
import StationRings from './StationRings';
import ZoneMeshes from './ZoneMeshes';
import DimensionLabels from './DimensionLabels';
import CoverShell from './CoverShell';
import MovementPath from './MovementPath';
import ShireLogo from './ShireLogo';
import SceneFeatureBoundary from './SceneFeatureBoundary';

export default function SceneComposer({
  mode = 'limb',
  component,
  dimensions,
  stations,
  accessPoints,
  keepOutZones,
  movementPositions,
  sensitiveZones,
  selectedId,
  onSelect,
  highlightedStation = -1,
  selectedAngle,
  show = {},
  transparency = 1,
  coverClearance = 6,
  coverThickness = 3,
  logoPosition = [0, 30, 72],
  onPaint,
  paintColor,
  compatibility = false,
}) {
  const opacity = compatibility ? 1 : transparency;
  const effectiveShow = compatibility
    ? { ...show, cover: false, movement: false, sensitive: false, logo: true }
    : show;

  return (
    <group>
      {/* Core prosthetic assembly — always rendered, never wrapped so it is the resilient fallback */}
      {mode === 'component' && component ? (
        <ComponentModel component={component} dimensions={dimensions} selected={selectedId === 'component'} onSelect={onSelect} opacity={opacity} onPaint={onPaint} />
      ) : (
        <ProstheticAssembly stations={stations} selectedId={selectedId} onSelect={onSelect} opacity={opacity} onPaint={onPaint} />
      )}

      <SceneFeatureBoundary name="Measurement rings">
        <StationRings stations={stations} selectedId={selectedId} onSelect={onSelect} visible={effectiveShow.measurements} highlightedIndex={highlightedStation} />
      </SceneFeatureBoundary>

      <SceneFeatureBoundary name="Dimension labels">
        <DimensionLabels dimensions={dimensions} stations={stations} visible={effectiveShow.measurements} />
      </SceneFeatureBoundary>

      <SceneFeatureBoundary name="Zones">
        <ZoneMeshes
          accessPoints={accessPoints}
          keepOutZones={keepOutZones}
          sensitiveZones={sensitiveZones}
          showAccess={effectiveShow.access}
          showKeepOut={effectiveShow.keepOut}
          showSensitive={effectiveShow.sensitive}
          selectedId={selectedId}
          onSelect={onSelect}
        />
      </SceneFeatureBoundary>

      <SceneFeatureBoundary name="Movement envelope" warningPosition={[0, 230, 0]}>
        <MovementPath positions={movementPositions} visible={effectiveShow.movement} selectedAngle={selectedAngle} onSelect={onSelect} />
      </SceneFeatureBoundary>

      <SceneFeatureBoundary name="Conceptual cover" warningPosition={[0, -200, 0]}>
        <CoverShell
          stations={stations}
          visible={effectiveShow.cover}
          opacity={0.3 * (compatibility ? 1 : transparency)}
          clearance={coverClearance}
          thickness={coverThickness}
          conceptual
          selectedId={selectedId}
          onSelect={onSelect}
        />
      </SceneFeatureBoundary>

      <SceneFeatureBoundary name="SHiRE logo" warningPosition={logoPosition}>
        <ShireLogo visible={effectiveShow.logo} position={logoPosition} selectedId={selectedId} onSelect={onSelect} />
      </SceneFeatureBoundary>
    </group>
  );
}