import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { expect, test } from '@playwright/test';
import { createBrowserWorkspace } from './common';

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

test('R7 visually edits citations, figures, simple tables, theorem text and BibTeX fields losslessly', async ({ page }) => {
  const workspaceName = `latex-r7-${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: 'Visual' }).click();

  const visual = page.locator('.interactive-latex-visual-editor');
  await expect(visual).toBeVisible();
  await expect(visual.locator('.interactive-visual-inline-citation')).toContainText('smith2026');
  await expect(visual.locator('.interactive-visual-inline-reference')).toContainText('fig:one');
  await expect(visual.locator('.interactive-visual-inline-footnote')).toContainText('plain note');
  await expect(visual.locator('.interactive-visual-figure-image')).toBeVisible();
  await expect(visual.locator('.interactive-visual-figure')).toContainText('Result image');

  await visual.locator('.interactive-visual-figure').click();
  const figureDialog = page.getByRole('dialog');
  await figureDialog.getByLabel('Width (% of line width)').fill('60');
  await figureDialog.getByLabel('Replace image').setInputFiles({
    name: 'replacement.png',
    mimeType: 'image/png',
    buffer: Buffer.from(
      'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Wl2nCEAAAAASUVORK5CYII=',
      'base64',
    ),
  });
  await expect(figureDialog.getByText('figures/replacement.png')).toBeVisible();
  await figureDialog.getByRole('button', { name: 'Apply' }).click();

  const firstCell = visual.locator('.interactive-visual-table td').first();
  await firstCell.fill('Alpha');
  const theoremBody = visual.locator('.interactive-visual-theorem-body').first();
  await theoremBody.fill('Updated theorem body.');

  await visual.locator('.interactive-visual-inline-citation').click();
  const citationDialog = page.getByRole('dialog');
  await expect(citationDialog.getByText('Original Smith Title')).toBeVisible();
  await citationDialog.getByText('Original Smith Title').click();
  await citationDialog.getByText('Doe Book').click();
  await citationDialog.getByRole('button', { name: /Use 1 citation/ }).click();

  await page.getByRole('button', { name: 'Source' }).click();
  const source = page.getByLabel('LaTeX source editor');
  await expect(source).toContainText('Alpha & B');
  await expect(source).toContainText('Updated theorem body.');
  await expect(source).toContainText('\\cite{doe2025}');
  await expect(source).toContainText('\\label{fig:one}');
  await expect(source).toContainText(
    '\\includegraphics[width=0.6\\linewidth]{figures/replacement.png}',
  );

  await page.getByRole('link', { name: /^refs\.bib$/ }).click();
  await page.getByRole('button', { name: 'Visual' }).click();
  const titleInput = page.getByLabel('title').first();
  await expect(titleInput).toHaveValue('Original Smith Title');
  await titleInput.fill('Updated Smith Title');
  await titleInput.blur();
  await page.getByRole('button', { name: 'Source' }).click();
  const bibSource = page.getByLabel('LaTeX source editor');
  await expect(bibSource).toContainText('title = {Updated Smith Title}');
  await expect(bibSource).toContainText('author = {Smith, Jane}');
});
