// ship.js — procedural kit-bashed explorer ship (primitives only).
import * as THREE from 'three';

const HULL = 0xdfe6ee;
const HULL_DARK = 0x6b7682;

export function makeShip() {
  const g = new THREE.Group();
  const hullMat = new THREE.MeshStandardMaterial({ color: HULL, metalness: 0.6, roughness: 0.45 });
  const darkMat = new THREE.MeshStandardMaterial({ color: HULL_DARK, metalness: 0.7, roughness: 0.4 });
  const canopyMat = new THREE.MeshStandardMaterial({
    color: 0x0a2b3a, metalness: 0.2, roughness: 0.1,
    emissive: new THREE.Color(0x39d0ff), emissiveIntensity: 0.35, transparent: true, opacity: 0.85,
  });
  const glowMat = new THREE.MeshStandardMaterial({
    color: 0xff7a2a, emissive: new THREE.Color(0xff7a2a), emissiveIntensity: 3.2, roughness: 0.4,
  });
  const trimMat = new THREE.MeshStandardMaterial({
    color: 0xff8a3a, emissive: new THREE.Color(0xff8a3a), emissiveIntensity: 1.6,
  });

  // fuselage
  const body = new THREE.Mesh(new THREE.CapsuleGeometry(0.55, 2.4, 8, 16), hullMat);
  body.rotation.x = Math.PI / 2;
  body.scale.set(1, 1, 0.82);
  g.add(body);

  // nose cone
  const nose = new THREE.Mesh(new THREE.ConeGeometry(0.55, 1.4, 16), hullMat);
  nose.rotation.x = -Math.PI / 2;
  nose.position.z = -2.1;
  g.add(nose);

  // canopy
  const canopy = new THREE.Mesh(new THREE.SphereGeometry(0.46, 20, 16, 0, Math.PI * 2, 0, Math.PI * 0.6), canopyMat);
  canopy.position.set(0, 0.34, -0.55);
  canopy.scale.set(1, 0.8, 1.5);
  g.add(canopy);

  // wings (swept)
  const wingGeo = new THREE.BoxGeometry(2.6, 0.12, 1.2);
  for (const s of [-1, 1]) {
    const wing = new THREE.Mesh(wingGeo, darkMat);
    wing.position.set(s * 1.5, -0.1, 0.5);
    wing.rotation.z = s * 0.12;
    wing.rotation.y = s * 0.35;
    g.add(wing);
    // wingtip trim light
    const tip = new THREE.Mesh(new THREE.BoxGeometry(0.18, 0.16, 0.5), trimMat);
    tip.position.set(s * 2.55, -0.05, 0.85);
    g.add(tip);
  }

  // tail fin
  const fin = new THREE.Mesh(new THREE.BoxGeometry(0.12, 0.9, 1.0), darkMat);
  fin.position.set(0, 0.5, 1.2);
  g.add(fin);

  // engines + glow
  const engines = [];
  const engineGeo = new THREE.CylinderGeometry(0.34, 0.42, 0.7, 16);
  const glowGeo = new THREE.CylinderGeometry(0.3, 0.18, 0.5, 16);
  for (const s of [-1, 1]) {
    const eng = new THREE.Mesh(engineGeo, darkMat);
    eng.rotation.x = Math.PI / 2;
    eng.position.set(s * 0.7, -0.05, 1.7);
    g.add(eng);
    const glow = new THREE.Mesh(glowGeo, glowMat);
    glow.rotation.x = -Math.PI / 2;
    glow.position.set(s * 0.7, -0.05, 2.05);
    g.add(glow);
    engines.push(glow);
  }

  // landing struts (only visible parked, fine to always have)
  const strutMat = darkMat;
  for (const [sx, sz] of [[-0.8, -0.6], [0.8, -0.6], [0, 1.4]]) {
    const strut = new THREE.Mesh(new THREE.CylinderGeometry(0.06, 0.06, 0.7, 8), strutMat);
    strut.position.set(sx, -0.7, sz);
    g.add(strut);
    const foot = new THREE.Mesh(new THREE.CylinderGeometry(0.16, 0.16, 0.08, 10), strutMat);
    foot.position.set(sx, -1.02, sz);
    g.add(foot);
  }

  g.traverse((o) => { if (o.isMesh) { o.castShadow = true; o.receiveShadow = true; } });

  return {
    group: g,
    engines,
    setBoost(t) {
      const i = 3.2 + t * 6.0;
      engines.forEach((e) => (e.material.emissiveIntensity = i));
      const sc = 1 + t * 0.5;
      engines.forEach((e) => e.scale.set(1, sc, 1));
    },
  };
}
