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

const PAGES: { path: string; heading: RegExp }[] = [
  { path: '/', heading: /arena/i },
  { path: '/how-it-works', heading: /./ },
  { path: '/for-venues', heading: /./ },
  { path: '/pricing', heading: /./ },
  { path: '/pilot', heading: /./ },
  { path: '/faq', heading: /./ },
  { path: '/legal/privacy', heading: /privacy/i },
  { path: '/legal/terms', heading: /terms/i },
];

test.describe('marketing site', () => {
  for (const { path, heading } of PAGES) {
    test(`${path} renders with one h1, a title and no console errors`, async ({ page }) => {
      const errors: string[] = [];
      page.on('pageerror', (e) => errors.push(e.message));
      page.on('console', (m) => {
        if (m.type() === 'error') errors.push(m.text());
      });
      const res = await page.goto(path);
      expect(res?.status()).toBe(200);
      await expect(page.locator('h1')).toHaveCount(1);
      await expect(page.locator('h1')).toHaveText(heading);
      await expect(page).toHaveTitle(/PintRush/);
      // No horizontal scrolling at any viewport.
      const overflow = await page.evaluate(() => document.documentElement.scrollWidth - window.innerWidth);
      expect(overflow).toBeLessThanOrEqual(1);
      expect(errors).toEqual([]);
    });
  }

  test('pricing shows the three pilot tiers and no checkout', async ({ page }) => {
    await page.goto('/pricing');
    for (const price of ['$79', '$149', '$249']) await expect(page.getByText(price, { exact: false }).first()).toBeVisible();
    await expect(page.getByText(/pilot pricing/i).first()).toBeVisible();
    await expect(page.getByRole('link', { name: /pilot/i }).first()).toBeVisible();
  });

  test('sitemap and robots are served', async ({ request }) => {
    const sitemap = await request.get('/sitemap.xml');
    expect(sitemap.ok()).toBe(true);
    expect(await sitemap.text()).toContain('/pricing');
    const robots = await request.get('/robots.txt');
    expect(robots.ok()).toBe(true);
    const body = await robots.text();
    expect(body).toMatch(/Disallow: \/host/);
    expect(body).toMatch(/Sitemap:/);
  });

  test('pilot form rejects an empty submission with field errors', async ({ page }) => {
    await page.goto('/pilot');
    await page.getByRole('button', { name: /submit|apply|join|send/i }).first().click();
    // Native validation or server errors — either way the page must not show a success state.
    await expect(page.getByText(/thank|received|you're in/i)).toHaveCount(0);
  });
});
