// ============================================================
// VAX SCHEDULE + CATCH-UP — content renders into the stub panels.
// These tabs have no inputs; they just display the AAP/ACIP tables.
// Verify: real content (not just "Loading") + at least a few
// recognisable vaccine abbreviations show up.
// ============================================================

const { test, expect, E2E_BASE } = require('../fixtures');

async function openTab(page, name) {
  await page.goto(E2E_BASE + '/');
  await page.waitForSelector('button.tab-btn', { timeout: 15000 });
  const vp = page.viewportSize();
  if (vp && vp.width <= 768) {
    await page.click('#btn-menu-toggle').catch(() => {});
  }
  await page.click(`button.tab-btn[data-tab="${name}"]`);
  await page.waitForFunction((t) => {
    const el = document.getElementById(t + '-tab');
    return el && el.classList.contains('active') && el.innerHTML.trim().length > 100;
  }, name, { timeout: 15000 });
}

test.describe('Vaccine schedules — content loaded', () => {

  test('vaxschedule: panel populates beyond the loading placeholder', async ({ authedPage: _, page }) => {
    await openTab(page, 'vaxschedule');
    await expect.poll(async () => {
      const text = await page.locator('#wv-panel-schedule').innerText();
      return text;
    }, { timeout: 10000 }).not.toMatch(/^Loading schedule/i);

    // Must mention at least a couple of the core childhood vaccines
    const text = await page.locator('#wv-panel-schedule').innerText();
    expect(text).toMatch(/HepB|Hep B/i);
    expect(text).toMatch(/MMR/i);
    expect(text).toMatch(/DTaP|Tdap/i);
  });

  test('catchup: panel populates with the catch-up schedule', async ({ authedPage: _, page }) => {
    await openTab(page, 'catchup');
    await expect.poll(async () => {
      const text = await page.locator('#wv-panel-catchup').innerText();
      return text;
    }, { timeout: 10000 }).not.toMatch(/^Loading catch-up/i);

    const text = await page.locator('#wv-panel-catchup').innerText();
    expect(text.trim().length).toBeGreaterThan(200);
    // Should mention interval guidance in some form
    expect(text).toMatch(/interval|month|week/i);
  });
});
