// ============================================================
// ENCOUNTER TAB — detailed workflow: transcript → generate HPI →
// refine → shorten. Uses mocked AI so tests don't burn API credits.
// ============================================================

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

async function openTab(page) {
  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="encounter"]');
  await page.waitForFunction(() => {
    const el = document.getElementById('encounter-tab');
    return el && el.classList.contains('active') && el.innerHTML.trim().length > 100;
  }, { timeout: 15000 });
}

test.describe('Encounter — HPI generation workflow', () => {

  test('fill transcript + generate → mocked HPI renders in output pane', async ({ authedPage: _, page }) => {
    await mockAI(page);
    await openTab(page);

    // Fill age + setting; transcript is a contenteditable div, not a textarea
    await page.fill('#enc-age', '8 years');
    await page.selectOption('#enc-gender', 'Male');
    await page.locator('#enc-transcript').click();
    await page.keyboard.type('Chief complaint: 3-day history of fever and cough.');

    // Click generate — wait for the mocked /api/generate-hpi-encounter response
    const [hpiResp] = await Promise.all([
      page.waitForResponse('**/api/generate-hpi-encounter'),
      page.click('#enc-generate-btn'),
    ]);
    expect(hpiResp.status()).toBe(200);

    // Output container un-hides and shows the mocked text
    await expect(page.locator('#enc-output')).toBeVisible();
    await expect(page.locator('#enc-hpi-text')).toContainText('MOCK HPI from encounter');
  });

  test('refine action → fetches /api/refine and updates the rendered HPI', async ({ authedPage: _, page }) => {
    // Override /api/refine to return a recognisable marker so we can tell the refined
    // content replaced the original.
    await mockAI(page, {
      '**/api/refine': { success: true, refined: 'REFINED-MARKER: now a longer narrative.', model: 'mock-gpt' },
    });
    await openTab(page);

    await page.fill('#enc-age', '8 years');
    await page.locator('#enc-transcript').click();
    await page.keyboard.type('Fever and cough 3 days.');
    await page.click('#enc-generate-btn');
    await expect(page.locator('#enc-hpi-text')).toContainText('MOCK HPI', { timeout: 10000 });

    // Type an instruction and hit refine
    await page.fill('#enc-refine-input', 'Make it longer.');
    const [refineResp] = await Promise.all([
      page.waitForResponse('**/api/refine'),
      page.click('#enc-refine-btn'),
    ]);
    expect(refineResp.status()).toBe(200);
    await expect(page.locator('#enc-hpi-text')).toContainText('REFINED-MARKER', { timeout: 10000 });
  });

  test('clear transcript button empties the contenteditable', async ({ authedPage: _, page }) => {
    await mockAI(page);
    await openTab(page);
    await page.locator('#enc-transcript').click();
    await page.keyboard.type('Some content.');
    await expect(page.locator('#enc-transcript')).toContainText('Some content.');
    await page.click('#enc-clear');
    const text = await page.locator('#enc-transcript').innerText();
    expect(text.trim()).toBe('');
  });
});
