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

test.describe('Landing page', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('/');
  });

  test('Has correct title', async ({ page }) => {
    await expect(page).toHaveTitle(/Neuron/);
  });

  test('Has exactly one h1', async ({ page }) => {
    const h1s = page.locator('h1');
    await expect(h1s).toHaveCount(1);
  });

  test('Has meta description with sufficient length', async ({ page }) => {
    const meta = page.locator('meta[name="description"]');
    await expect(meta).toHaveCount(1);
    const content = await meta.getAttribute('content');
    expect(content?.length).toBeGreaterThan(50);
  });

  test('Has og:title and og:description', async ({ page }) => {
    await expect(page.locator('meta[property="og:title"]')).toHaveCount(1);
    await expect(page.locator('meta[property="og:description"]')).toHaveCount(1);
  });

  test('Has canonical URL pointing to production domain', async ({ page }) => {
    const canonical = page.locator('link[rel="canonical"]');
    await expect(canonical).toHaveCount(1);
    const href = await canonical.getAttribute('href');
    expect(href).toContain('neurontechnologies.ai');
    expect(href).not.toContain('stage');
    expect(href).not.toContain('run.app');
  });

  test('Nav is rendered and visible', async ({ page }) => {
    // Use the specific nav ID — the footer also contains a <nav> element
    await expect(page.locator('#nav')).toBeVisible();
  });

  test('Hero section is visible', async ({ page }) => {
    await expect(page.locator('section').first()).toBeVisible();
  });

  test('Has structured data JSON-LD script that parses cleanly', async ({ page }) => {
    const schema = page.locator('script[type="application/ld+json"]');
    await expect(schema).toHaveCount(1);
    const content = await schema.textContent();
    expect(() => JSON.parse(content!)).not.toThrow();
  });

  test('Page loads without first-party JavaScript errors', async ({ page }) => {
    const errors: string[] = [];
    page.on('console', msg => {
      if (msg.type() === 'error') errors.push(msg.text());
    });
    await page.goto('/');
    await page.waitForLoadState('networkidle');
    // Filter known third-party noise:
    //   - GTM / Google Analytics fire CSP-blocked connect-src violations
    //     because their scripts attempt analytics.google.com, www.google.com
    //     (those aren't in our connect-src, which is correct)
    //   - Browser extension injections
    //   - Font CDN preconnect failures (non-critical)
    const thirdPartyDomains = [
      'googletagmanager', 'analytics.google', 'google.com', 'gstatic',
      'cloudflare', 'cdn.jsdelivr', 'fonts.googleapis', 'extension',
      'third-party', 'googleadservices', 'stripe', 'supabase',
    ];
    const realErrors = errors.filter(
      e => !thirdPartyDomains.some(domain => e.includes(domain)),
    );
    expect(realErrors).toHaveLength(0);
  });

  test('Demo panel is present in the DOM', async ({ page }) => {
    // The demo panel is rendered server-side and injected into the page.
    await expect(page.locator('#neuron-demo-panel')).toBeAttached();
  });

  test('Demo panel button (open trigger) is present', async ({ page }) => {
    await expect(page.locator('#neuron-demo-btn')).toBeAttached();
  });
});
