/* Yellow Stone Traders & General Supplies — hero background
 * A cluster of low-poly gold ore fragments, lit like cut bullion,
 * turning slowly behind the headline. Built to read as "raw gold ore",
 * the company's core trade — not a generic gemstone.
 * Skipped on small screens and prefers-reduced-motion.
 */
(function () {
  var canvas = document.getElementById('heroCanvas');
  if (!canvas || typeof THREE === 'undefined') return;
  if (window.matchMedia('(max-width: 700px)').matches) return;

  var reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;

  var scene = new THREE.Scene();
  var camera = new THREE.PerspectiveCamera(42, window.innerWidth / window.innerHeight, 0.1, 100);
  camera.position.set(0, 0.4, 9);

  var renderer = new THREE.WebGLRenderer({ canvas: canvas, antialias: true, alpha: true });
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
  renderer.setClearColor(0x000000, 0);

  // Lighting rig — warm key light + cool rim light, like a studio shot of ore.
  scene.add(new THREE.AmbientLight(0x2a2116, 1.1));
  var key = new THREE.PointLight(0xffcf7a, 3.4, 40);
  key.position.set(6, 6, 8);
  scene.add(key);
  var rim = new THREE.PointLight(0x6d7dff, 1.1, 40);
  rim.position.set(-7, -3, 5);
  scene.add(rim);
  var fill = new THREE.PointLight(0xffb347, 1.6, 40);
  fill.position.set(-4, 5, -4);
  scene.add(fill);

  function makeNugget(radius) {
    var geo = new THREE.IcosahedronGeometry(radius, 1);
    var pos = geo.attributes.position;
    for (var i = 0; i < pos.count; i++) {
      var v = new THREE.Vector3(pos.getX(i), pos.getY(i), pos.getZ(i));
      var n = v.clone().normalize();
      var bump = (Math.sin(v.x * 3.1) + Math.cos(v.y * 2.7) + Math.sin(v.z * 3.6)) * 0.14;
      v.addScaledVector(n, bump);
      pos.setXYZ(i, v.x, v.y, v.z);
    }
    geo.computeVertexNormals();
    var mat = new THREE.MeshStandardMaterial({
      color: 0xf3ab1c,
      emissive: 0x3a2200,
      emissiveIntensity: 0.35,
      metalness: 0.85,
      roughness: 0.32,
      flatShading: true
    });
    return new THREE.Mesh(geo, mat);
  }

  var group = new THREE.Group();
  var nuggets = [];
  var layout = [
    { r: 1.55, x: 2.1, y: 0.2, z: 0 },
    { r: 0.95, x: 3.3, y: -1.1, z: -0.6 },
    { r: 0.7, x: 1.1, y: -1.5, z: 0.8 },
    { r: 0.55, x: 3.6, y: 1.4, z: 0.4 },
    { r: 0.42, x: 0.6, y: 1.4, z: -0.4 }
  ];
  layout.forEach(function (d) {
    var m = makeNugget(d.r);
    m.position.set(d.x, d.y, d.z);
    m.rotation.set(Math.random() * Math.PI, Math.random() * Math.PI, Math.random() * Math.PI);
    group.add(m);
    nuggets.push(m);
  });
  group.position.x = 0.6;
  scene.add(group);

  // Faint wireframe orbit ring — a nod to survey/engineering drawings.
  var ringGeo = new THREE.TorusGeometry(4.4, 0.008, 8, 96);
  var ringMat = new THREE.MeshBasicMaterial({ color: 0xfdaf17, transparent: true, opacity: 0.25 });
  var ring = new THREE.Mesh(ringGeo, ringMat);
  ring.rotation.x = Math.PI / 2.4;
  ring.position.copy(group.position);
  scene.add(ring);

  function handleResize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
  }
  window.addEventListener('resize', handleResize);

  var scrollProgress = 0;
  window.addEventListener('scroll', function () {
    var doc = document.documentElement;
    var max = doc.scrollHeight - doc.clientHeight;
    scrollProgress = max > 0 ? Math.min(window.scrollY / (window.innerHeight * 1.2), 1) : 0;
  }, { passive: true });

  var mouseX = 0, mouseY = 0;
  window.addEventListener('mousemove', function (e) {
    mouseX = (e.clientX / window.innerWidth - 0.5);
    mouseY = (e.clientY / window.innerHeight - 0.5);
  });

  if (reduceMotion) {
    renderer.render(scene, camera);
    return;
  }

  var clock = new THREE.Clock();
  function animate() {
    requestAnimationFrame(animate);
    var delta = clock.getDelta();

    group.rotation.y += delta * 0.12;
    group.rotation.x += delta * 0.02;
    ring.rotation.z += delta * 0.05;

    nuggets.forEach(function (m, i) {
      m.rotation.y += delta * (0.05 + i * 0.01);
    });

    camera.position.x += (mouseX * 1.2 - camera.position.x) * 0.03;
    camera.position.y += (0.4 - mouseY * 0.6 - camera.position.y) * 0.03;
    camera.lookAt(0.6, 0, 0);

    group.position.y = -scrollProgress * 1.4;
    group.scale.setScalar(1 - scrollProgress * 0.1);

    renderer.render(scene, camera);
  }
  animate();
})();
