import { expect, test } from '../../fixtures/multi-client';
import { openPluginStore } from '../../helpers/app-menu';
import { expectDashboardReady } from '../../helpers/dashboard';
import { addPluginSource } from '../../helpers/plugin-store';
import { RegisterPage } from '../../pages/register.page';
import { ServerSearchPage } from '../../pages/server-search.page';

test.describe('Plugin manager UI', () => {
  test.describe.configure({ timeout: 180_000 });

  test('installs, grants, activates, and logs an all-API test plugin', async ({ createClient }) => {
    const client = await createClient();
    const { page } = client;
    const suffix = Date.now();
    const register = new RegisterPage(page);
    const search = new ServerSearchPage(page);

    await test.step('Register user and create server context', async () => {
      await register.goto();
      await register.register(`plugin_${suffix}`, 'Plugin Tester', 'TestPass123!');
      await expectDashboardReady(page);
      await search.createServer(`Plugin API Server ${suffix}`, {
        description: 'Plugin manager UI E2E coverage'
      });

      await expect(page).toHaveURL(/\/room\//, { timeout: 30_000 });
    });

    await test.step('Open Plugin Store from the title-bar menu', async () => {
      await openPluginStore(page);
      await expect(page.getByTestId('plugin-store-page')).toBeVisible({ timeout: 10_000 });
    });

    await test.step('Install fixture plugin from source manifest', async () => {
      await addPluginSource(page);
      const pluginCard = page.locator('article', { hasText: 'E2E All API Plugin' });

      await pluginCard.getByRole('button', { name: 'Readme' }).click();
      await expect(page.getByText('Fixture plugin for Playwright coverage.')).toBeVisible({ timeout: 10_000 });
      await pluginCard.getByRole('button', { exact: true, name: /^(Install|Install to Server)$/ }).click();
      const installDialog = page.getByRole('dialog', { name: 'E2E All API Plugin' });

      await expect(installDialog).toBeVisible({ timeout: 10_000 });
      await expect(installDialog.getByText('Install to server', { exact: true })).toBeVisible();
      await page.getByRole('button', { name: 'Install and Activate' }).click();
      await expect(page.locator('article', { hasText: 'E2E All API Plugin' }).getByText('Installed')).toBeVisible({ timeout: 10_000 });
    });

    await test.step('Open plugin manager from the store page', async () => {
      await page.getByRole('button', { name: 'Manage Plugins' }).click();
      await expect(page.getByTestId('plugin-manager')).toBeVisible({ timeout: 10_000 });
      await expect(page.getByTestId('plugin-manager').getByRole('heading', { name: 'Server plugins' })).toBeVisible();
      await expect(page.getByText('E2E All API Plugin')).toBeVisible();
    });

    await test.step('Grant capabilities and activate runtime', async () => {
      const manager = page.getByTestId('plugin-manager');
      const pluginCard = manager.locator('article', { hasText: 'E2E All API Plugin' });

      await manager.getByRole('button', { name: 'Installed' }).click();
      await expect(pluginCard).toBeVisible({ timeout: 10_000 });
      await pluginCard.getByRole('button', { name: 'Select' }).click();

      await page.getByRole('button', { name: 'Grant all requested' }).click();
      await page.getByRole('button', { name: 'Activate ready plugins' }).click();
      await expect(page.locator('article', { hasText: 'E2E All API Plugin' }).getByText('ready', { exact: true })).toBeVisible({ timeout: 20_000 });
    });

    await test.step('Verify plugin exercised APIs through logs and extension points', async () => {
      const manager = page.getByTestId('plugin-manager');

      await manager.getByRole('button', { name: 'Logs' }).click();
      await expect(page.getByText('all-api plugin completed')).toBeVisible({ timeout: 20_000 });
      await expect(page.getByText('all-api plugin ready')).toBeVisible({ timeout: 10_000 });
      await manager.getByRole('button', { name: 'Extension points' }).click();
      await expect(page.getByTestId('plugin-extension-counts')).toContainText('Settings pages');
      await expect(page.getByTestId('plugin-extension-counts')).toContainText('Embed renderers');
      await expect(page.getByTestId('plugin-extension-counts')).toContainText('1');
      await expect(page.getByTestId('plugin-conflict-diagnostics')).toContainText(
        'No duplicate route, action, embed, channel, panel, or settings contribution ids detected.'
      );

      await manager.getByRole('button', { exact: true, name: 'Requirements' }).click();
      await expect(page.getByTestId('plugin-server-requirements')).toContainText('E2E All API Plugin');
      await expect(page.getByTestId('plugin-server-requirements')).toContainText('enabled');

      await manager.getByRole('button', { exact: true, name: 'Settings' }).click();
      await expect(page.getByTestId('plugin-generated-settings')).toContainText('E2E settings contribution');
      await expect(page.getByTestId('plugin-generated-settings')).toContainText('"enabled"');

      await manager.getByRole('button', { exact: true, name: 'Docs' }).click();
      await expect(page.getByTestId('plugin-installed-docs')).toContainText('Calls every public Toju plugin API surface');
    });
  });
});
