import { test, expect } from "./fixtures";
import { BASE_PATH } from "../../playwright.config";

const B = BASE_PATH;

test("contact form submits successfully and shows success state", async ({ page, smtpInbox }) => {
  await page.goto(`${B}/contact`);

  await page.fill("#name", "CI Test Runner");
  await page.fill("#email", "ci@clearfin-test.com");
  await page.fill("#company", "CI Corp");
  await page.fill("#phone", "+41 00 000 00 00");
  await page.selectOption("#subject", "General Inquiry");
  await page.fill("#message", "Automated CI test - please ignore.");

  await page.click('button[type="submit"]');

  await expect(
    page.locator("text=Message sent!"),
    "Success message not shown after form submission",
  ).toBeVisible({ timeout: 15000 });

  // The banner is only meaningful if a mail server actually took the message.
  await expect.poll(() => smtpInbox.messages.length).toBe(1);
  expect(smtpInbox.messages[0].header("subject")).toBe(
    "[ClearFin] General Inquiry: CI Test Runner (CI Corp)",
  );
});

test("every offered subject can be submitted", async ({ page, smtpInbox }) => {
  await page.goto(`${B}/contact`);

  // A subject present in the dropdown but rejected by the API would strand the
  // visitor on an error banner, so the two lists have to stay in step.
  const options = await page.locator("#subject option").evaluateAll((els) =>
    els.map((el) => (el as HTMLOptionElement).value).filter(Boolean),
  );
  expect(options.length).toBeGreaterThan(1);

  for (const subject of options) {
    const res = await page.request.post(`${B}/api/contact`, {
      data: {
        name: "CI Test Runner",
        email: "ci@clearfin-test.com",
        phone: "",
        company: "CI Corp",
        subject,
        message: "Automated CI test - please ignore.",
      },
    });
    expect(res.status(), `Subject rejected by the API: ${subject}`).toBe(200);
  }

  await expect.poll(() => smtpInbox.messages.length).toBe(options.length);
});

test("contact form shows an error banner when the send fails", async ({ page, smtpInbox }) => {
  smtpInbox.rejectAuth = true;

  await page.goto(`${B}/contact`);
  await page.fill("#name", "CI Test Runner");
  await page.fill("#email", "ci@clearfin-test.com");
  await page.fill("#company", "CI Corp");
  await page.selectOption("#subject", "General Inquiry");
  await page.fill("#message", "Automated CI test - please ignore.");

  await page.click('button[type="submit"]');

  await expect(page.locator("text=Failed to send message")).toBeVisible({ timeout: 15000 });
  await expect(page.locator("text=Message sent!")).toHaveCount(0);
});

test("contact form blocks submission when required fields are empty", async ({ page, smtpInbox }) => {
  await page.goto(`${B}/contact`);

  await page.click('button[type="submit"]');

  // Native validation keeps the form on screen and nothing is sent.
  await expect(page.locator("form")).toBeVisible();
  await expect(page.locator("text=Message sent!")).toHaveCount(0);
  expect(smtpInbox.messages).toHaveLength(0);
});
