import { expect, type Page, test } from '@playwright/test';
import { createBrowserWorkspace, pressAppShortcut } from './common';

const saveShortcut = process.platform === 'darwin' ? 'Meta+s' : 'Control+s';

async function createTextFile(page: Page, fileName: string, content: string) {
  const explorer = page.getByTestId('interactive-file-explorer');
  await explorer.getByRole('button', { name: 'New File' }).click();
  const dialog = page.getByRole('dialog', { name: 'Create Project File' });
  await dialog.getByLabel('File name').fill(fileName);
  await dialog.getByRole('button', { name: 'Create' }).click();
  const source = page.getByLabel('LaTeX source editor');
  await source.fill(content);
  await source.press(saveShortcut);
  await expect(page.getByText('Saved', { exact: true })).toBeVisible();
}

test('R14.24.0 provides project Text/Symbol workflows, navigator sections and central search/file shortcuts', async ({ page }) => {
  const workspaceName = `professional-r14-24-0-${Date.now()}`;
  await createBrowserWorkspace(page, { workspaceName });

  await createTextFile(
    page,
    'refs.bib',
    '@article{demo2026,title={A Demonstration Paper},author={Researcher, Ada},year={2026}}\n',
  );
  await page.getByRole('link', { name: /^main\.tex$/ }).click();
  const source = page.getByLabel('LaTeX source editor');
  await source.fill([
    '\\documentclass{article}',
    '\\begin{document}',
    '\\section{Alpha results}',
    '\\label{sec:alpha}',
    'Alpha evidence appears in this paragraph.',
    'See \\ref{sec:alpha} and \\cite{demo2026}.',
    '\\bibliography{refs}',
    '\\end{document}',
    '',
  ].join('\n'));
  await source.press(saveShortcut);
  await expect(page.getByText('Saved', { exact: true })).toBeVisible();

  // Cmd/Ctrl+Shift+F is the central project-search command, not browser Find.
  await pressAppShortcut(page, 'Shift+F');
  const tools = page.getByTestId('interactive-project-tools-dialog');
  await expect(tools).toBeVisible();
  await expect(tools.getByTestId('interactive-project-text-search')).toBeVisible();
  const search = tools.getByLabel('Search text across the project');
  await search.fill('Alpha');
  await tools.getByRole('button', { name: 'Search', exact: true }).click();
  await expect(tools.locator('[data-search-file="main.tex"]')).toContainText('2');
  await tools.getByLabel('Replace with').fill('Beta');
  await tools.getByRole('button', { name: /^Replace all/ }).click();
  await expect(page.getByText(/Replaced 2 matches in 1 file/i)).toBeVisible();
  await page.keyboard.press('Escape');
  await expect(source).toContainText('\\section{Beta results}');
  await expect(source).toContainText('Beta evidence appears in this paragraph.');

  // Current-file search is separately registered on Cmd/Ctrl+F.
  await pressAppShortcut(page, 'F');
  await expect(page.getByTestId('interactive-project-text-search')).toBeVisible();
  await expect(page.getByTestId('interactive-project-text-search').locator('select')).toHaveValue('file');
  await page.keyboard.press('Escape');

  // Navigator is one semantic index with the requested initial sections.
  await page.getByRole('button', { name: 'Outline', exact: true }).click();
  const navigator = page.getByTestId('interactive-project-outline');
  await expect(navigator).toBeVisible();
  for (const tab of ['Outline', 'Labels', 'References', 'Citations', 'Bibliography']) {
    await expect(navigator.getByRole('button', { name: tab, exact: true })).toBeVisible();
  }
  await navigator.getByRole('button', { name: 'Labels', exact: true }).click();
  await expect(navigator).toContainText('sec:alpha');
  await navigator.getByRole('button', { name: 'References', exact: true }).click();
  await expect(navigator).toContainText('sec:alpha');
  await navigator.getByRole('button', { name: 'Citations', exact: true }).click();
  await expect(navigator).toContainText('demo2026');
  await navigator.getByRole('button', { name: 'Bibliography', exact: true }).click();
  await expect(navigator).toContainText('A Demonstration Paper');

  // Cmd/Ctrl+P keeps quick-file navigation distinct from the command palette.
  await pressAppShortcut(page, 'P');
  await expect(page.getByRole('dialog').filter({ hasText: 'All Files' })).toBeVisible();
  await page.keyboard.press('Escape');
  await pressAppShortcut(page, 'Shift+P');
  const palette = page.getByRole('dialog', { name: 'omni command bar' });
  await expect(palette).toBeVisible();
  await expect(palette.getByPlaceholder('Type a command or search...')).toHaveValue('>');
});
