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

test.describe("Marketing & Public Pages", () => {
  test("TC-C-01: Homepage loads correctly", async ({ page }) => {
    await page.goto("/");

    // How it works section (kit: "Three steps. No waiting rooms.")
    await expect(
      page.getByRole("heading", { name: /Three steps/i }).first(),
    ).toBeVisible({ timeout: 10000 });

    // Who we help section (kit: "Mostly grown-ups figuring it out.")
    await expect(
      page.getByRole("heading", { name: /Mostly grown-ups/i }).first(),
    ).toBeVisible();

    // Footer has privacy link
    await expect(
      page.getByRole("contentinfo").getByRole("link", { name: /privacy/i }),
    ).toBeVisible();
  });

  test("TC-C-01: Homepage mobile responsiveness", async ({ page }) => {
    await page.setViewportSize({ width: 375, height: 812 });
    await page.goto("/");
    await expect(page.locator("body")).toBeVisible();
  });

  test("TC-C-02: Our Story page", async ({ page }) => {
    await page.goto("/our-story");

    await expect(
      page
        .getByRole("heading", { name: /How Pax Psychology started/i })
        .first(),
    ).toBeVisible({ timeout: 10000 });
    await expect(
      page.getByText(/one in finance, one in psychology/i).first(),
    ).toBeVisible();
  });

  test("TC-C-03: For Business page", async ({ page }) => {
    await page.goto("/for-business");

    await expect(
      page
        .getByRole("heading", {
          name: /Look after the people who look after the work/i,
        })
        .first(),
    ).toBeVisible({ timeout: 10000 });
  });

  test("TC-C-04: Contact page form loads and accepts input", async ({
    page,
  }) => {
    await page.goto("/contact");

    // Dismiss cookie banner if present
    const cookieAccept = page.getByRole("button", { name: /accept/i }).first();
    if (await cookieAccept.isVisible({ timeout: 2000 }).catch(() => false)) {
      await cookieAccept.click();
    }

    // Verify contact form is visible
    await expect(
      page.getByRole("heading", { name: /contact us/i }).first(),
    ).toBeVisible({ timeout: 10000 });

    // Fill in the form using placeholders
    await page.getByPlaceholder(/name/i).first().fill("Test User");
    await page.getByPlaceholder(/email/i).first().fill("testuser@example.com");
    await page.getByPlaceholder(/phone/i).first().fill("0412 345 678");
    await page
      .getByPlaceholder(/message/i)
      .first()
      .fill("Staging test.");

    // Submit button exists. It stays disabled until the Turnstile CAPTCHA is
    // solved (when configured), so only click when it's enabled.
    const submitBtn = page.getByRole("button", { name: /submit/i }).first();
    await expect(submitBtn).toBeVisible();
    if (await submitBtn.isEnabled()) {
      await submitBtn.click();
      // Verify success message or that the form is still present
      await expect(
        page
          .getByText(/thanks|thank you/i)
          .first()
          .or(submitBtn),
      ).toBeVisible({ timeout: 10000 });
    }
  });

  test("TC-C-05: Privacy Policy and Terms of Use pages", async ({ page }) => {
    await page.goto("/privacy-policy");
    await expect(
      page.getByRole("heading", { name: /privacy policy/i }).first(),
    ).toBeVisible({ timeout: 10000 });

    await page.goto("/terms-of-use");
    await expect(
      page.getByRole("heading", { name: /terms/i }).first(),
    ).toBeVisible({ timeout: 10000 });
  });

  test("TC-C-06: Newsletter signup from the journal strip", async ({
    page,
  }) => {
    // The newsletter form moved off the footer to the dark strip on /journal
    await page.goto("/journal");

    await expect(
      page
        .getByRole("heading", { name: /One good read, every fortnight/i })
        .first(),
    ).toBeVisible({ timeout: 15000 });

    const email = page.getByPlaceholder("you@email.com").first();
    await expect(email).toBeVisible();
    await email.fill(`newsletter-e2e-${Date.now()}@example.com`);
    const subscribe = page.getByRole("button", { name: /Subscribe/i }).first();
    await subscribe.click();

    // Success replaces the form with a confirmation message; a validation
    // error keeps the form. Accept either a confirmation or the form state.
    await expect(
      page
        .getByText(/welcome|subscribed|check your inbox|you.re in|thanks/i)
        .first()
        .or(subscribe),
    ).toBeVisible({ timeout: 15000 });
  });
});
