/**
 * Journey: Onboarding Flow
 *
 * Tests the onboarding experience for new users who have just authenticated.
 * Full OAuth flows (GSC) require real external services and are not tested here.
 * Instead, this journey verifies the structural integrity of the onboarding UI:
 *
 * - Sites list / create page renders correctly
 * - Settings pages are accessible and render without crashing
 * - Onboarding page for a seeded site renders without JS errors
 * - Unauthenticated users are redirected to login
 *
 * What this catches:
 * - Broken Inertia prop contracts causing blank pages on /sites/create
 * - Missing route registrations for settings pages
 * - React crash due to undefined props in onboarding step components
 * - Auth middleware not protecting onboarding routes
 */

import { expect, test } from '../fixtures/auth';

test.describe('Journey: Onboarding Flow', () => {
  test.describe('Site creation', () => {
    test('sites create page renders the site creation form', async ({ page }) => {
      await page.goto('/sites/create', { waitUntil: 'domcontentloaded' });

      await expect(page).toHaveURL(/\/sites\/create/);

      // The form should have inputs for site name and domain
      const nameInput = page.locator('input[name="name"], input[id="name"]');
      const domainInput = page.locator('input[name="domain"], input[id="domain"]');

      await expect(nameInput.first()).toBeVisible();
      await expect(domainInput.first()).toBeVisible();
    });

    test('site creation page renders submit button', async ({ page }) => {
      await page.goto('/sites/create', { waitUntil: 'domcontentloaded' });

      const submitButton = page.locator('button[type="submit"]').first();
      await expect(submitButton).toBeVisible();
    });

    test('site creation page renders without JS errors', async ({ page }) => {
      const errors: string[] = [];
      page.on('pageerror', (err) => errors.push(err.message));
      page.on('console', (msg) => {
        if (msg.type() === 'error') errors.push(msg.text());
      });

      await page.goto('/sites/create', { waitUntil: 'domcontentloaded' });

      const appErrors = errors.filter(
        (e) => !e.includes('extension') && !e.includes('chrome-extension'),
      );
      expect(appErrors, `Console errors: ${appErrors.join(', ')}`).toHaveLength(0);
    });
  });

  test.describe('Settings accessibility', () => {
    test('AI settings page renders for authenticated user', async ({ page }) => {
      await page.goto('/settings/ai', { waitUntil: 'domcontentloaded' });

      await expect(page).toHaveURL(/\/settings\/ai/);
      // Page heading should contain AI-related content
      await expect(page.locator('body')).toContainText(/api key|openai|ai settings/i);
    });

    test('AI settings page renders without JS errors', async ({ page }) => {
      const errors: string[] = [];
      page.on('pageerror', (err) => errors.push(err.message));
      page.on('console', (msg) => {
        if (msg.type() === 'error') errors.push(msg.text());
      });

      await page.goto('/settings/ai', { waitUntil: 'domcontentloaded' });

      const appErrors = errors.filter(
        (e) => !e.includes('extension') && !e.includes('chrome-extension'),
      );
      expect(appErrors, `Console errors: ${appErrors.join(', ')}`).toHaveLength(0);
    });

    test('SERP key settings page renders for authenticated user', async ({ page }) => {
      await page.goto('/settings/serp-key', { waitUntil: 'domcontentloaded' });

      await expect(page).toHaveURL(/\/settings\/serp-key/);
    });
  });

  test.describe('Site onboarding page', () => {
    test('onboarding page renders for seeded site', async ({ page, seedData }) => {
      await page.goto(`/sites/${seedData.siteId}/onboarding`, { waitUntil: 'domcontentloaded' });

      // Either renders the onboarding page or redirects if already complete
      // The key assertion is that the app does NOT crash
      await expect(page.locator('body')).not.toBeEmpty();
    });
  });

  test.describe('Unauthenticated access', () => {
    test('blocks unauthenticated access to sites create', async ({ browser }) => {
      const context = await browser.newContext();
      const page = await context.newPage();

      await page.goto('/sites/create');
      await expect(page).toHaveURL(/\/login/);

      await context.close();
    });

    test('blocks unauthenticated access to AI settings', async ({ browser }) => {
      const context = await browser.newContext();
      const page = await context.newPage();

      await page.goto('/settings/ai');
      await expect(page).toHaveURL(/\/login/);

      await context.close();
    });
  });
});
