// Procedural Web Audio SFX + ambience. No external assets (ELEVENLABS key MISSING).
// All sounds are synthesized at runtime. Initialized on first user gesture.

let ctx = null, master = null, ambGain = null, thrustGain = null, thrustOsc = null, mineGain = null, mineOsc = null;
let enabled = true;

export const audio = {
  init() {
    if (ctx) return;
    try {
      ctx = new (window.AudioContext || window.webkitAudioContext)();
    } catch (e) { enabled = false; return; }
    master = ctx.createGain(); master.gain.value = 0.5; master.connect(ctx.destination);

    // low ambient pad
    ambGain = ctx.createGain(); ambGain.gain.value = 0.0; ambGain.connect(master);
    const pad = ctx.createOscillator(); pad.type = 'sawtooth'; pad.frequency.value = 55;
    const pad2 = ctx.createOscillator(); pad2.type = 'sine'; pad2.frequency.value = 82.5;
    const lp = ctx.createBiquadFilter(); lp.type = 'lowpass'; lp.frequency.value = 320;
    pad.connect(lp); pad2.connect(lp); lp.connect(ambGain);
    pad.start(); pad2.start();
    ambGain.gain.linearRampToValueAtTime(0.12, ctx.currentTime + 3);

    // continuous thrust hum (gain modulated by speed)
    thrustGain = ctx.createGain(); thrustGain.gain.value = 0.0; thrustGain.connect(master);
    thrustOsc = ctx.createOscillator(); thrustOsc.type = 'sawtooth'; thrustOsc.frequency.value = 70;
    const tlp = ctx.createBiquadFilter(); tlp.type = 'lowpass'; tlp.frequency.value = 600;
    thrustOsc.connect(tlp); tlp.connect(thrustGain); thrustOsc.start();
  },
  resume() { if (ctx && ctx.state === 'suspended') ctx.resume(); },
  setThrust(v) { if (thrustGain) thrustGain.gain.value = Math.min(0.25, v * 0.25); if (thrustOsc) thrustOsc.frequency.value = 60 + v * 90; },
  setAmbient(v) { if (ambGain) ambGain.gain.linearRampToValueAtTime(v, ctx.currentTime + 1.5); },

  _tone({ type = 'sine', f = 440, f2 = null, dur = 0.2, vol = 0.3, delay = 0 }) {
    if (!ctx || !enabled) return;
    const t0 = ctx.currentTime + delay;
    const o = ctx.createOscillator(); o.type = type; o.frequency.setValueAtTime(f, t0);
    if (f2) o.frequency.exponentialRampToValueAtTime(f2, t0 + dur);
    const g = ctx.createGain(); g.gain.setValueAtTime(0.0001, t0);
    g.gain.exponentialRampToValueAtTime(vol, t0 + 0.012);
    g.gain.exponentialRampToValueAtTime(0.0001, t0 + dur);
    o.connect(g); g.connect(master); o.start(t0); o.stop(t0 + dur + 0.02);
  },
  _noise({ dur = 0.4, vol = 0.4, lp = 1200, delay = 0 }) {
    if (!ctx || !enabled) return;
    const t0 = ctx.currentTime + delay;
    const n = Math.floor(ctx.sampleRate * dur);
    const buf = ctx.createBuffer(1, n, ctx.sampleRate);
    const d = buf.getChannelData(0);
    for (let i = 0; i < n; i++) d[i] = (Math.random() * 2 - 1) * (1 - i / n);
    const src = ctx.createBufferSource(); src.buffer = buf;
    const f = ctx.createBiquadFilter(); f.type = 'lowpass'; f.frequency.value = lp;
    const g = ctx.createGain(); g.gain.value = vol;
    src.connect(f); f.connect(g); g.connect(master); src.start(t0);
  },

  ui() { this._tone({ type: 'triangle', f: 660, f2: 880, dur: 0.12, vol: 0.18 }); },
  scan() {
    this._tone({ type: 'sine', f: 880, f2: 1760, dur: 0.5, vol: 0.16 });
    this._tone({ type: 'sine', f: 1320, f2: 2200, dur: 0.4, vol: 0.08, delay: 0.05 });
  },
  discovery() {
    [523, 659, 784, 1046].forEach((f, i) => this._tone({ type: 'triangle', f, dur: 0.25, vol: 0.16, delay: i * 0.07 }));
  },
  mineStart() {
    if (!ctx || mineOsc) return;
    mineGain = ctx.createGain(); mineGain.gain.value = 0.0; mineGain.connect(master);
    mineOsc = ctx.createOscillator(); mineOsc.type = 'square'; mineOsc.frequency.value = 140;
    const f = ctx.createBiquadFilter(); f.type = 'bandpass'; f.frequency.value = 800; f.Q.value = 4;
    mineOsc.connect(f); f.connect(mineGain); mineOsc.start();
    mineGain.gain.linearRampToValueAtTime(0.12, ctx.currentTime + 0.05);
  },
  mineStop() {
    if (!ctx || !mineOsc) return;
    mineGain.gain.linearRampToValueAtTime(0.0001, ctx.currentTime + 0.06);
    mineOsc.stop(ctx.currentTime + 0.08); mineOsc = null;
  },
  shatter() { this._noise({ dur: 0.5, vol: 0.4, lp: 2600 }); this._tone({ type: 'sine', f: 320, f2: 60, dur: 0.4, vol: 0.18 }); },
  land() { this._noise({ dur: 1.2, vol: 0.5, lp: 700 }); this._tone({ type: 'sine', f: 90, f2: 40, dur: 1.0, vol: 0.3 }); },
  boost() { this._tone({ type: 'sawtooth', f: 200, f2: 600, dur: 0.5, vol: 0.12 }); },
  win() {
    [523, 659, 784, 1046, 1318].forEach((f, i) => this._tone({ type: 'triangle', f, dur: 0.5, vol: 0.18, delay: i * 0.12 }));
  },
};
