import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { expect, test } from '@playwright/test';
import { createBrowserWorkspace } from './common';
import { activateProFromAiLauncher, installProActivationRoute } from './pro-license-fixture';

const fixture = path.resolve(
  path.dirname(fileURLToPath(import.meta.url)),
  '../fixtures/interactive-r7-project.zip',
);

test('R8 searches, diagnoses and safely refactors a LaTeX project while keeping the PDF AI overlay floating', async ({ page }) => {
  await installProActivationRoute(page);
  const workspaceName = `latex-r8-${Date.now()}`;
  await createBrowserWorkspace(page, { workspaceName });
  await page.getByLabel('Import Project ZIP').setInputFiles(fixture);
  await page.getByRole('link', { name: /^main\.tex$/ }).click();

  await page.getByRole('button', { name: 'Project Tools' }).click();
  const tools = page.getByRole('dialog');
  await tools.getByRole('button', { name: 'Symbols', exact: true }).click();
  await tools.getByLabel('Search headings, labels, references, citations, bibliography, environments or files').fill('smith2026');
  await tools.getByRole('button', { name: 'Search' }).last().click();
  await expect(tools.getByText(/smith2026/).first()).toBeVisible();

  await tools.getByRole('button', { name: 'Doctor' }).click();
  await expect(tools.getByText('Project health')).toBeVisible();

  await tools.getByRole('button', { name: 'Refactor' }).click();
  await tools.getByRole('button', { name: 'Rename label' }).click();
  await tools.getByLabel('Current name').fill('fig:one');
  await tools.getByLabel('New name').fill('fig:result');
  await tools.getByRole('button', { name: 'Apply refactor' }).click();

  await tools.getByRole('button', { name: 'Rename citation key' }).click();
  await tools.getByLabel('Current name').fill('smith2026');
  await tools.getByLabel('New name').fill('smith2026a');
  await tools.getByRole('button', { name: 'Apply refactor' }).click();

  await tools.getByRole('button', { name: 'Move/rename file' }).click();
  await tools.getByLabel('Current project path').fill('figures/result.png');
  await tools.getByLabel('New project path').fill('figures/final.png');
  await tools.getByRole('button', { name: 'Apply refactor' }).click();
  await tools.getByRole('button', { name: 'Close' }).click();

  const source = page.getByLabel('LaTeX source editor');
  await expect(source).toContainText('\\ref{fig:result}');
  await expect(source).toContainText('\\cite{smith2026a}');
  await expect(source).toContainText('figures/final.png');

  await page.getByRole('link', { name: /^refs\.bib$/ }).click();
  await expect(page.getByLabel('LaTeX source editor')).toContainText('@article{smith2026a');

  await page.getByRole('link', { name: /^main\.tex$/ }).click();
  await page.getByRole('button', { name: 'Visual' }).click();
  const visual = page.locator('.interactive-latex-visual-editor');
  await expect(visual).toBeVisible();
  await visual.locator('.interactive-visual-paragraph, .interactive-visual-rich-paragraph').first().dblclick();
  await expect(page.getByLabel('LaTeX source editor')).toBeVisible();

  await activateProFromAiLauncher(page);
  await page.getByRole('button', { name: 'Open Learnitive AI' }).click();
  await expect(page.getByTestId('interactive-ai-overlay')).toBeVisible();
  await expect(page.getByTestId('interactive-pdf-preview')).toBeVisible();
});
