/* Yellow Stone Traders & General Supplies — Insights page hero background
 * A fanned stack of thin gold-edged panels — guides, company updates and
 * press, rendered as a spread of documents/plates. Same interaction model
 * as the other hero scenes: slow auto-rotation, mouse parallax, scroll-
 * driven fade/scale. 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.6, 9.5);

  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);

  scene.add(new THREE.AmbientLight(0x2a2116, 1.15));
  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);

  var plateMat = new THREE.MeshStandardMaterial({
    color: 0x201a10, metalness: 0.2, roughness: 0.55, flatShading: true
  });
  var edgeMat = new THREE.MeshStandardMaterial({
    color: 0xf3ab1c, emissive: 0x3a2200, emissiveIntensity: 0.32,
    metalness: 0.85, roughness: 0.3, flatShading: true
  });

  function makePlate(w, h) {
    // A thin panel with a slightly larger, thin gold "rim" frame behind it,
    // reading as a document with a gold-edged border.
    var plate = new THREE.Group();
    var rim = new THREE.Mesh(new THREE.BoxGeometry(w + 0.08, h + 0.08, 0.03), edgeMat);
    rim.position.z = -0.02;
    plate.add(rim);
    var face = new THREE.Mesh(new THREE.BoxGeometry(w, h, 0.04), plateMat);
    plate.add(face);
    // three thin gold "text" bars, suggesting content lines
    [0.28, 0.02, -0.24].forEach(function (yOff, i) {
      var lineW = i === 1 ? w * 0.55 : w * 0.7;
      var line = new THREE.Mesh(new THREE.BoxGeometry(lineW, 0.05, 0.05), edgeMat);
      line.position.set(-(w - lineW) / 2, yOff * h, 0.04);
      plate.add(line);
    });
    return plate;
  }

  var group = new THREE.Group();
  var plates = [];
  var layout = [
    { w: 1.7, h: 2.2, x: -0.9, y: -0.1, z: -0.6, rz: -0.28 },
    { w: 1.7, h: 2.2, x: -0.2, y: 0.05, z: -0.3, rz: -0.1 },
    { w: 1.7, h: 2.2, x: 0.5,  y: 0.0,  z: 0.0,  rz: 0.08 },
    { w: 1.7, h: 2.2, x: 1.2,  y: -0.15, z: 0.3, rz: 0.26 }
  ];
  layout.forEach(function (d) {
    var plate = makePlate(d.w, d.h);
    plate.position.set(d.x, d.y, d.z);
    plate.rotation.z = d.rz;
    plate.rotation.y = -0.15;
    group.add(plate);
    plates.push(plate);
  });

  group.position.x = 0.4;
  scene.add(group);

  var ringGeo = new THREE.TorusGeometry(4.3, 0.008, 8, 96);
  var ringMat = new THREE.MeshBasicMaterial({ color: 0xfdaf17, transparent: true, opacity: 0.22 });
  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.09;
    ring.rotation.z += delta * 0.045;

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

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

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