import { test, expect } from '@playwright/test';
import { login, navigateTo } from './helpers';
import path from 'path';

test.describe('Document Management', () => {
  test.beforeEach(async ({ page }) => {
    await login(page, 'super_admin');
  });

  test('navigate to documents page', async ({ page }) => {
    await navigateTo(page, '/documents');
    await page.waitForLoadState('networkidle');

    const heading = page.getByText(/documents|files/i).first();
    await expect(heading).toBeVisible({ timeout: 10_000 });
  });

  test('upload a test file via files page', async ({ page }) => {
    // Try the files sub-page which may have a file browser
    await navigateTo(page, '/documents');
    await page.waitForLoadState('networkidle');
    await page.waitForTimeout(2000);

    // Look for an upload button
    const uploadBtn = page.getByRole('button', { name: /upload|add/i }).first();
    if (await uploadBtn.isVisible({ timeout: 5_000 }).catch(() => false)) {
      // Check for a file input (may be hidden)
      const fileInput = page.locator('input[type="file"]').first();
      if ((await fileInput.count()) > 0) {
        const testFilePath = path.resolve('tests/e2e/fixtures/test-document.txt');
        await fileInput.setInputFiles(testFilePath);
        await page.waitForTimeout(5000);
      } else {
        // Click the upload button and then look for the input
        await uploadBtn.click();
        await page.waitForTimeout(1000);
        const fileInput2 = page.locator('input[type="file"]').first();
        if ((await fileInput2.count()) > 0) {
          const testFilePath = path.resolve('tests/e2e/fixtures/test-document.txt');
          await fileInput2.setInputFiles(testFilePath);
          await page.waitForTimeout(5000);
        }
      }
    }

    // Verify page didn't crash
    const pageContent = page.getByText(/documents|files/i).first();
    await expect(pageContent).toBeVisible({ timeout: 5_000 });
  });

  test('documents hub shows the EOI queue tab', async ({ page }) => {
    await navigateTo(page, '/documents');
    await page.waitForLoadState('networkidle');

    const tab = page.getByRole('tab', { name: /eoi queue/i });
    await expect(tab).toBeVisible({ timeout: 10_000 });
    await tab.click();
    await expect(tab).toHaveAttribute('data-state', 'active');
  });

  test('admin can create a folder and the breadcrumb updates', async ({ page }) => {
    await navigateTo(page, '/documents');
    await page.waitForLoadState('networkidle');

    // Create a folder via the actions menu.
    await page.getByRole('button', { name: /folder actions/i }).click();
    await page.getByRole('menuitem', { name: /new folder at root/i }).click();
    const folderName = `Smoke ${Date.now()}`;
    await page.getByLabel('Name').fill(folderName);
    await page.getByRole('button', { name: 'Create' }).click();
    // The new folder should appear in the FolderTreeSidebar as a button
    // whose accessible name matches its text content (node.name span).
    await expect(page.getByRole('button', { name: folderName })).toBeVisible({ timeout: 10_000 });

    // Click into the folder; breadcrumb should update to show the folder name.
    await page.getByRole('button', { name: folderName }).click();
    await expect(page.getByRole('navigation', { name: /breadcrumb/i })).toContainText(folderName);
  });
});
