import { useMemo } from 'react';
import * as THREE from 'three';
import { buildLimbGeometry, normalizeStations } from './limbGeometry';
import EdgeLines from './EdgeLines';
import { useLabels } from './useViewportLabels';

/**
 * Conceptual semi-transparent cover shell around the socket and upper pylon.
 * Selectable, toggleable, responds to the transparency slider. No Drei Html;
 * the "conceptual" warning renders in the DOM overlay.
 */
export default function CoverShell({ stations, visible = true, opacity = 0.3, clearance = 6, thickness = 3, conceptual = true, selectedId, onSelect }) {
  const { geometry, totalHeight } = useMemo(() => {
    const norm = normalizeStations(stations);
    const expanded = norm.map(s => ({ ...s, width: s.width + clearance * 2, depth: s.depth + clearance * 2 }));
    const lowest = norm[0];
    expanded.unshift({
      height: (lowest?.height ?? 0) - 110,
      width: 40 + clearance * 2,
      depth: 36 + clearance * 2,
    });
    const g = buildLimbGeometry(expanded);
    const minH = expanded[0].height;
    const maxH = expanded[expanded.length - 1].height;
    return { geometry: g, midHeight: (minH + maxH) / 2, totalHeight: maxH - minH };
  }, [stations, clearance]);

  const skirtBottomY = -totalHeight / 2;
  const seamLen = totalHeight;
  const maxRadius = useMemo(() => {
    const norm = normalizeStations(stations);
    return Math.max(...norm.map(s => Math.max(s.width, s.depth) / 2 + clearance), 30);
  }, [stations, clearance]);

  const sel = selectedId === 'cover-preview';
  useLabels(visible ? [{ id: 'cover-warn', position: [0, skirtBottomY - 14, 0], title: 'Conceptual cover preview — not manufacturing geometry', tone: 'amber' }] : []);

  if (!visible) return null;

  const handleSelect = (e) => { e.stopPropagation(); onSelect?.('cover', 'preview', { partId: 'cover-preview', label: 'Conceptual Cover' }); };

  return (
    <group>
      <mesh geometry={geometry} onClick={handleSelect}>
        <meshPhysicalMaterial
          color={sel ? '#22d3ee' : '#28323d'}
          transparent
          opacity={opacity}
          metalness={0.06}
          roughness={0.34}
          clearcoat={0.35}
          clearcoatRoughness={0.35}
          side={THREE.DoubleSide}
          emissive={sel ? '#22d3ee' : '#0a1018'}
          emissiveIntensity={sel ? 0.12 : 0.03}
          depthWrite={false}
        />
        <EdgeLines geometry={geometry} color={sel ? '#22d3ee' : '#5a6675'} opacity={opacity * 1.6} />
      </mesh>

      {/* Front seam */}
      <mesh position={[0, 0, maxRadius]} raycast={() => null}>
        <cylinderGeometry args={[1.1, 1.1, seamLen, 8]} />
        <meshStandardMaterial color="#22d3ee" emissive="#22d3ee" emissiveIntensity={0.6} transparent opacity={0.85} />
      </mesh>
      {/* Rear seam */}
      <mesh position={[0, 0, -maxRadius]} raycast={() => null}>
        <cylinderGeometry args={[1.1, 1.1, seamLen, 8]} />
        <meshStandardMaterial color="#22d3ee" emissive="#22d3ee" emissiveIntensity={0.6} transparent opacity={0.85} />
      </mesh>
    </group>
  );
}