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/estimation";
const TEST_EMAIL = "paf-studio-code+estimation@agentmail.to";

test("Formulaire Estimation (devis) -> 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 d'estimation / budget Covalba",
    steps: [
      { description: `Navigate to ${TARGET}` },
      { description: "Scroll to the lead form whose submit button says 'Recevoir mon budget indicatif'" },
      { description: "Type the first name into the 'Prénom' input", data: { value: "Claude" } },
      { description: "Type the last name into the 'Nom' input", data: { value: "TEST-Estimation" } },
      { 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 devis)" } },
      { description: "Check the optional communications opt-in checkbox" },
      { description: "Click the 'Recevoir mon budget indicatif' submit button", waitUntil: "A success confirmation with the heading 'Demande envoyée !' is visible" },
    ],
    assertions: [
      { assertion: "The form shows a success confirmation with a green check icon and the heading 'Demande envoyée !'. 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();
});
