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

test("Formulaire Devenir Applicateur -> 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() });
    }
  });
  const ts = new Date().toISOString();
  const source = `Test automatise Passmark (lead de test, ignorer) ${ts}`;
  await runSteps({
    page,
    userFlow: "Remplir entierement et soumettre le formulaire de candidature applicateur Covalba",
    steps: [
      { description: `Navigate to ${TARGET}` },
      { description: "Scroll to the applicateur application form" },
      { 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-Applicateur" } },
      { description: "Type the email into the 'E-mail professionnel' input", data: { value: TEST_EMAIL } },
      { description: "Type the phone number into the phone field (France +33 is preselected)", data: { value: "0612345678" } },
      { description: "Type the company into the \"Nom de l'entreprise\" input", data: { value: "Paf-Studio Code (TEST)" } },
      { description: "Type the city into the 'Ville' input", data: { value: "Orléans" } },
      { description: "Type the company creation year into the 'Année de création de l'entreprise' number input", data: { value: "2015" } },
      { description: "Check the checkbox labelled 'J'ai déjà effectué au moins un chantier cool roof'" },
      { description: "Type into the 'Métier(s)' input", data: { value: "Étanchéité" } },
      { description: "In the 'Clientèle' section, click/check the 'Tertiaire' option card" },
      { description: "In the 'Clientèle' section, also click/check the 'Industries' option card" },
      { description: "Type into the 'Où avez-vous entendu parler de Covalba ?' input", data: { value: source } },
      { description: "Check the RGPD consent checkbox about Covalba processing your data" },
      { description: "Click the 'Envoyer ma candidature' submit button", waitUntil: "A success confirmation with the heading 'Candidature envoyée !' is visible" },
    ],
    assertions: [
      { assertion: "The form shows a success confirmation with a green check icon and the heading 'Candidature 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();
});
