import { expect, test } from "@playwright/test";
import {
  createCase,
  openCaseOnStep,
  sendChat,
  sendMany,
  STEP_SCRIPT,
} from "./helpers";

test("step 8 Hand off: agent briefing ack closes the chat process", async ({ request, page }) => {
  const created = await createCase(request);
  const c = await sendMany(request, created.caseId, [
    STEP_SCRIPT.name,
    STEP_SCRIPT.state,
    STEP_SCRIPT.intent,
    STEP_SCRIPT.age,
    STEP_SCRIPT.loan,
    STEP_SCRIPT.build,
    STEP_SCRIPT.diseases,
    STEP_SCRIPT.income,
  ]);
  expect(c.process.step).toBe(8);
  expect(c.profile.decision_result).toBeTruthy();
  expect((c.profile.carrier_shortlist as string[] | undefined)?.length).toBeGreaterThan(0);
  expect(c.process.missingRequired).toEqual(expect.arrayContaining(["agent_handoff_ack"]));

  await openCaseOnStep(page, c.caseId);
  await expect(page.getByTestId("process-current")).toHaveText("Hand off to licensed agent");
  await expect(page.getByTestId("process-exit-gate")).toContainText("Agent briefing acknowledged");
  await expect(page.getByText(/Licensed-agent handoff/i)).toBeVisible();

  const done = await sendChat(request, c.caseId, STEP_SCRIPT.handoff);
  expect(done.profile.agent_handoff_ack).toBe(true);
  expect(done.status).toBe("handoff");
  expect(done.process.step).toBe(8);
});
