// ===== TERMINAL TYPEWRITER =====
const lines = [
  { type: 'prompt', text: '$ whoami' },
  { type: 'out',    text: 'dr-bradley-davy' },
  { type: 'prompt', text: '$ cat role.txt' },
  { type: 'out',    text: 'Research Software Engineer' },
  { type: 'prompt', text: '$ squeue -u bdavy' },
  { type: 'out',    text: 'JOBID  PARTITION  NAME      ST  TIME     NODES' },
  { type: 'out',    text: '10842  gpu        train.sh  R   2:14:03  2' },
  { type: 'out',    text: '10843  compute    preproc   PD  0:00     4' },
  { type: 'prompt', text: '$ sinfo --summarize' },
  { type: 'out',    text: 'PARTITION  AVAIL  NODES(A/I/O/T)' },
  { type: 'out',    text: 'compute*   up     8/4/0/12' },
  { type: 'out',    text: 'gpu        up     2/1/0/3' },
  { type: 'prompt', text: '$ ansible-playbook site.yml --check' },
  { type: 'out',    text: 'PLAY [all] *********************************************' },
  { type: 'out',    text: 'ok=14  changed=3  unreachable=0  failed=0' },
  { type: 'prompt', text: '$ kubectl get nodes' },
  { type: 'out',    text: 'NAME       STATUS   ROLES    AGE' },
  { type: 'out',    text: 'node-01    Ready    master   42d' },
  { type: 'out',    text: 'node-02    Ready    worker   42d' },
  { type: 'prompt', text: '$ terraform plan | tail -1' },
  { type: 'out',    text: 'Plan: 12 to add, 0 to change, 0 to destroy.' },
  { type: 'prompt', text: '$ _' },
];

function typeTerminal() {
  const body = document.getElementById('terminal-body');
  if (!body) return;

  let lineIdx = 0;
  let charIdx = 0;
  let currentEl = null;

  function nextLine() {
    if (lineIdx >= lines.length) return;

    const line = lines[lineIdx];
    currentEl = document.createElement('div');
    currentEl.classList.add(line.type);

    if (line.type === 'prompt') {
      const promptSpan = document.createElement('span');
      promptSpan.classList.add('prompt');
      currentEl.appendChild(promptSpan);
    }

    body.appendChild(currentEl);
    charIdx = 0;
    typeChar();
  }

  function typeChar() {
    const line = lines[lineIdx];
    const target = line.type === 'prompt'
      ? currentEl.querySelector('.prompt')
      : currentEl;

    if (charIdx < line.text.length) {
      if (target) target.textContent += line.text[charIdx];
      charIdx++;
      setTimeout(typeChar, line.type === 'prompt' ? 50 : 20);
    } else {
      lineIdx++;
      const delay = lineIdx < lines.length ? (lines[lineIdx - 1].type === 'prompt' ? 400 : 150) : 0;
      if (lineIdx < lines.length) {
        setTimeout(nextLine, delay);
      }
    }
    body.scrollTop = body.scrollHeight;
  }

  setTimeout(nextLine, 600);
}

// ===== SCROLL FADE-IN =====
function initFadeIn() {
  const els = document.querySelectorAll(
    '.skill-card, .timeline-item, .cert-card, .edu-item, .contact-card, .stat-card, .about-text, .about-stats'
  );

  els.forEach(el => el.classList.add('fade-in'));

  const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        entry.target.classList.add('visible');
        observer.unobserve(entry.target);
      }
    });
  }, { threshold: 0.1 });

  els.forEach(el => observer.observe(el));
}

// ===== NAVBAR SCROLL HIGHLIGHT =====
function initNavHighlight() {
  const sections = document.querySelectorAll('section[id]');
  const navLinks = document.querySelectorAll('.nav-links a[href^="#"]');

  const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        const id = entry.target.getAttribute('id');
        navLinks.forEach(link => {
          link.classList.toggle('active', link.getAttribute('href') === `#${id}`);
        });
      }
    });
  }, { rootMargin: '-40% 0px -55% 0px' });

  sections.forEach(s => observer.observe(s));
}

// ===== MOBILE HAMBURGER =====
function initHamburger() {
  const btn = document.getElementById('hamburger');
  const links = document.querySelector('.nav-links');
  if (!btn || !links) return;

  btn.addEventListener('click', () => {
    links.classList.toggle('open');
  });

  links.querySelectorAll('a').forEach(a => {
    a.addEventListener('click', () => links.classList.remove('open'));
  });
}

// ===== INIT =====
document.addEventListener('DOMContentLoaded', () => {
  typeTerminal();
  initFadeIn();
  initNavHighlight();
  initHamburger();
});
