with open('/var/www/html/undangan_digital/index.html', 'r') as f:
    html = f.read()

# Add audio tag after body
if '<audio id="bg-music"' not in html:
    html = html.replace('<body style="overflow:hidden">', '<body style="overflow:hidden">\n<audio id="bg-music" src="assets/lagu.mp4" loop autoplay preload="auto"></audio>')

with open('/var/www/html/undangan_digital/index.html', 'w') as f:
    f.write(html)


with open('/var/www/html/undangan_digital/script.js', 'r') as f:
    script = f.read()

old_music_code = """// ── MUSIC ────────────────────────────────────────
let audio;
let playing = false;

function startMusic() {
  if (!audio) {
    // Generating a gentle Betawi-style audio tone via Web Audio API
    const ctx = new (window.AudioContext || window.webkitAudioContext)();
    const notes = [261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25];
    let t = ctx.currentTime;

    function playMelody() {
      notes.forEach((freq, i) => {
        const osc  = ctx.createOscillator();
        const gain = ctx.createGain();
        osc.connect(gain); gain.connect(ctx.destination);
        osc.type = 'sine';
        osc.frequency.value = freq;
        gain.gain.setValueAtTime(0, t + i * 0.5);
        gain.gain.linearRampToValueAtTime(0.08, t + i * 0.5 + 0.05);
        gain.gain.linearRampToValueAtTime(0, t + i * 0.5 + 0.45);
        osc.start(t + i * 0.5);
        osc.stop(t + i * 0.5 + 0.5);
      });
      t += notes.length * 0.5 + 0.5;
      if (playing) setTimeout(playMelody, (notes.length * 0.5 + 0.5) * 1000);
    }

    playing = true;
    document.getElementById('music-btn').classList.add('playing');
    playMelody();
  }
}

document.getElementById('music-btn').addEventListener('click', () => {
  playing = !playing;
  const btn = document.getElementById('music-btn');
  if (playing) {
    btn.classList.add('playing');
    btn.innerHTML = '<iconify-icon icon="mdi:music-note"></iconify-icon>';
    // If we had an audio file: audio.play()
    // For this demo, starting melody again would require state management, 
    // we just toggle the icon as a placeholder.
  } else {
    btn.classList.remove('playing');
    btn.innerHTML = '<iconify-icon icon="mdi:music-note-off"></iconify-icon>';
    // If we had an audio file: audio.pause()
  }
});"""

new_music_code = """// ── MUSIC ────────────────────────────────────────
const audio = document.getElementById('bg-music');
let playing = false;

function startMusic() {
  audio.play().then(() => {
    playing = true;
    document.getElementById('music-btn').classList.add('playing');
    document.getElementById('music-btn').innerHTML = '<iconify-icon icon="mdi:music-note"></iconify-icon>';
  }).catch(e => console.log("Audio autoplay prevented"));
}

// Attempt to play music automatically when document is clicked or loaded (browsers restrict autoplay)
document.body.addEventListener('click', () => {
  if (!playing) startMusic();
}, { once: true });

document.getElementById('music-btn').addEventListener('click', (e) => {
  e.stopPropagation(); // prevent body click
  playing = !playing;
  const btn = document.getElementById('music-btn');
  if (playing) {
    audio.play();
    btn.classList.add('playing');
    btn.innerHTML = '<iconify-icon icon="mdi:music-note"></iconify-icon>';
  } else {
    audio.pause();
    btn.classList.remove('playing');
    btn.innerHTML = '<iconify-icon icon="mdi:music-note-off"></iconify-icon>';
  }
});

// Also try on load
window.addEventListener('load', () => {
  startMusic();
});"""

if "// Generating a gentle Betawi-style audio tone" in script:
    script = script.replace(old_music_code, new_music_code)

with open('/var/www/html/undangan_digital/script.js', 'w') as f:
    f.write(script)

print("Audio logic updated.")
