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

function json(route: Route, status: number, body: unknown) {
  return route.fulfill({ status, contentType: 'application/json', body: JSON.stringify(body) });
}

async function mockPublicSession(page: Page) {
  await page.route('**/api/config/public', (route) => json(route, 200, {
    saasEnabled: true,
    googleAuthEnabled: true,
    contactEnabled: true,
    shopUrl: '',
    freePlanProjectLimit: 3,
    homeAiBuilder: {
      enabled: true,
      anonymousVisible: true,
      freeUserEnabled: false,
      maxPromptLength: 8000,
      quickStarts: ['research-paper','essay','study-notes','literature-review','latex','report','article','thesis','dissertation','book-chapter','proposal','lab-report','case-study','meeting-notes','summary','study-guide','flashcards','markdown'],
    },
  }));
  await page.route('**/api/auth/me', (route) => json(route, 200, { authenticated: false }));
  await page.route('**/api/user/settings', (route) => json(route, 200, { settings: {} }));
}

test('homepage hero demonstrates the LaTeX Source, Visual and PDF workflow with a working simulated Compile action', async ({ page }) => {
  await mockPublicSession(page);
  await page.goto('/');

  const workspace = page.getByRole('region', { name: 'Interactive simulated Learnitive LaTeX editor preview' });
  await expect(workspace).toBeVisible();
  await expect(workspace.getByText('main.tex', { exact: true }).first()).toBeVisible();
  await expect(workspace.getByText('Local First Scientific Writing', { exact: true }).first()).toBeVisible();
  await expect(workspace.getByText('TikZ', { exact: true })).toBeVisible();
  await expect(workspace.getByText('TexLab', { exact: true })).toBeVisible();
  await expect(workspace.getByRole('slider', { name: 'Compare Source and Visual editor' })).toBeVisible();

  const compile = workspace.getByRole('button', { name: /Compile/u });
  await compile.click();
  await expect(compile).toContainText(/Compiling|Compiled/u);
  await expect(workspace.locator('[data-pdf-revision="1"]')).toBeVisible();
});

test('homepage customer-review section shows source-backed Learnitive reviewers and real external source links', async ({ page }) => {
  await mockPublicSession(page);
  await page.goto('/');

  const reviews = page.locator('[data-home-section="customer-reviews"]');
  await expect(reviews.getByRole('heading', { name: 'What people are saying about Learnitive.' })).toBeVisible();
  await expect(reviews.getByText('Han Lee', { exact: true }).first()).toBeVisible();
  await expect(reviews.getByText('Amit Jaiswal', { exact: true }).first()).toBeVisible();
  await expect(reviews.getByRole('link', { name: /Han Lee.*Trustpilot/u }).first()).toHaveAttribute('href', 'https://www.trustpilot.com/review/learnitive.com');
  await expect(reviews.getByRole('link', { name: /Amit Jaiswal.*DealMirror/u }).first()).toHaveAttribute('href', 'https://dealmirror.com/product/learnitive/');
});

test('About carousel uses the supplied team and office WebP images', async ({ page }) => {
  await mockPublicSession(page);
  await page.goto('/about');

  const carousel = page.getByRole('region', { name: 'Learnitive highlights' });
  await expect(carousel.locator('img[src="/learnitive-team3.webp"]')).toHaveCount(1);
  await expect(carousel.locator('img[src="/learnitive-office3.webp"]')).toHaveCount(1);
  await expect(carousel.locator('img[src="/learnitive-team3.webp"]')).toHaveAttribute('alt', 'Learnitive team in the Learnitive workspace');
  await expect(carousel.locator('img[src="/learnitive-office3.webp"]')).toHaveAttribute('alt', 'Learnitive office workspace for learning, research and writing');
});
