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

// ── Config Passmark : Vercel AI Gateway (clé partagée AI_GATEWAY_API_KEY du VPS).
//    Exécution → Gemini Flash (rapide/éco) ; jugement → Claude Sonnet 4.6.
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 =
  process.env.TARGET_URL ||
  "https://covalba-next-wp-tom-integration-hubspot-client.paf-studio.dev/contact";
const TEST_EMAIL = "paf-studio-code@agentmail.to";

test("Formulaire Contact → soumission HubSpot (live)", async ({ page }) => {
  test.setTimeout(240_000); // l'exécution IA est lente

  // Preuve dure : on capture la réponse de l'API HubSpot Forms (doit être 200).
  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 message =
    `### TEST AUTOMATISE (Passmark — UI live), merci d'ignorer ce lead. ` +
    `Soumis via le formulaire /contact. Horodatage ${ts}. ###`;

  await runSteps({
    page,
    userFlow: "Remplir entierement et soumettre le formulaire de contact Covalba",
    steps: [
      { description: `Navigate to ${TARGET}` },
      { description: "Scroll to the contact form card titled 'Décrivez votre demande'" },
      { description: "Type the email address into the 'E-mail professionnel' input", data: { value: TEST_EMAIL } },
      { 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-Passmark" } },
      { description: "Type the company name into the \"Nom de l'entreprise\" input", data: { value: "Paf-Studio Code (TEST)" } },
      { description: "Type the phone number into the phone field (France +33 is preselected)", data: { value: "0612345678" } },
      { description: "Type the city into the 'Ville' input", data: { value: "Orléans" } },
      { description: "Type the message into the 'Message' textarea", data: { value: message } },
      { description: "Check the RGPD consent checkbox about Covalba processing your data" },
      {
        description: "Click the 'Envoyer ma demande' submit button",
        waitUntil: "A success confirmation with the heading 'Message envoyé !' is visible",
      },
    ],
    assertions: [
      {
        assertion:
          "The contact form shows a success confirmation: a green check icon with the heading 'Message envoyé !' and a thank-you message. The form fields are no longer visible.",
      },
    ],
    test,
    expect,
  });

  // ── Vérification réseau (preuve indépendante des e-mails).
  console.log("[hubspot] submit responses:", JSON.stringify(submitResponses));
  expect(submitResponses.length, "au moins une requête HubSpot submit attendue").toBeGreaterThan(0);
  expect(
    submitResponses.some((r) => r.status === 200),
    `réponse HubSpot submit 200 attendue, reçu: ${JSON.stringify(submitResponses)}`,
  ).toBeTruthy();
});
