import { test, expect } from "@playwright/test";
import { runSteps, configure } from "passmark";

configure({
  ai: {
    gateway: "vercel",
    models: {
      stepExecution: "google/gemini-3.5-flash",
      userFlowLow: "google/gemini-3-flash-preview",
      userFlowHigh: "google/gemini-3.5-flash",
      assertionPrimary: "anthropic/claude-sonnet-4.6",
      assertionSecondary: "google/gemini-3-flash",
      assertionArbiter: "anthropic/claude-sonnet-4.6",
    },
  },
});

const TARGET = "https://covalba-next-wp-tom-integration-hubspot-client.paf-studio.dev/guide-cool-roof";
const TEST_EMAIL = "paf-studio-code+livreblanc@agentmail.to";

test("Formulaire Livre Blanc (guide cool roof) -> HubSpot (live)", async ({ page }) => {
  test.setTimeout(600_000);
  const submitResponses: { url: string; status: number }[] = [];
  page.on("response", (res) => {
    if (res.url().includes("/submissions/v3/integration/submit/")) {
      submitResponses.push({ url: res.url(), status: res.status() });
    }
  });
  await runSteps({
    page,
    userFlow: "Remplir et soumettre le formulaire de telechargement du livre blanc Covalba",
    steps: [
      { description: `Navigate to ${TARGET} and wait for the page to finish loading` },
      { description: "Locate the first white-paper download form near the top of the page (the lead form inside the white card whose submit button says 'Télécharger')" },
      { description: "Type the first name into the 'Prénom' input of that first form", data: { value: "Claude" } },
      { description: "Type the last name into the 'Nom' input", data: { value: "TEST-LivreBlanc" } },
      { description: "Type the email into the 'E-mail professionnel' input", data: { value: TEST_EMAIL } },
      { description: "Type the company into the \"Nom de l'entreprise\" input", data: { value: "Paf-Studio Code (TEST livre blanc)" } },
      { description: "Check the optional communications opt-in checkbox" },
      { description: "Click the 'Télécharger' submit button", waitUntil: "A success confirmation with the heading 'Merci, c'est bien reçu !' is visible" },
    ],
    assertions: [
      { assertion: "The form shows a success confirmation with a green check icon and the heading 'Merci, c'est bien reçu !'. The form fields are no longer visible." },
    ],
    test,
    expect,
  });
  console.log("[hubspot] submit responses:", JSON.stringify(submitResponses));
  expect(submitResponses.length, "au moins une requête HubSpot submit").toBeGreaterThan(0);
  expect(submitResponses.some((r) => r.status === 200), `HubSpot 200 attendu, recu ${JSON.stringify(submitResponses)}`).toBeTruthy();
});
