import { test, expect } from '@playwright/test';
import { LoginPage } from './pages/LoginPage';
import { DashboardPage } from './pages/DashboardPage';
import { FileManagementPage } from './pages/FileManagementPage';

test.describe('UAT文件管理流程测试', () => {
  test('UAT-FILE-001: 文件上传下载完整流程', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const fileManagementPage = new FileManagementPage(page);

    await test.step('1. 管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await page.waitForURL(/.*dashboard/);
    });

    await test.step('2. 导航到文件管理', async () => {
      await dashboardPage.navigateToFileManagement();
      await expect(fileManagementPage.table).toBeVisible();
    });

    await test.step('3. 上传文件', async () => {
      await fileManagementPage.clickUploadButton();
      
      const fileInput = page.locator('input[type="file"]');
      await fileInput.setInputFiles('./e2e/fixtures/test-file.txt');
      
      await fileManagementPage.submitUpload();
      await expect(fileManagementPage.successMessage).toBeVisible();
    });

    await test.step('4. 验证文件列表', async () => {
      const rowCount = await fileManagementPage.getTableRowCount();
      expect(rowCount).toBeGreaterThan(0);
    });

    await test.step('5. 下载文件', async () => {
      await fileManagementPage.clickDownloadButton(1);
      
      const downloadPromise = page.waitForEvent('download');
      const download = await downloadPromise;
      expect(download.suggestedFilename()).toBeTruthy();
    });
  });

  test('UAT-FILE-002: 文件删除流程', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const fileManagementPage = new FileManagementPage(page);

    await test.step('1. 管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await page.waitForURL(/.*dashboard/);
    });

    await test.step('2. 导航到文件管理', async () => {
      await dashboardPage.navigateToFileManagement();
      await expect(fileManagementPage.table).toBeVisible();
    });

    await test.step('3. 删除文件', async () => {
      await fileManagementPage.clickDeleteButton(1);
      await page.on('dialog', dialog => dialog.accept());
      await expect(fileManagementPage.successMessage).toBeVisible();
    });
  });

  test('UAT-FILE-003: 文件搜索和过滤', async ({ page }) => {
    const loginPage = new LoginPage(page);
    const dashboardPage = new DashboardPage(page);
    const fileManagementPage = new FileManagementPage(page);

    await test.step('1. 管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await page.waitForURL(/.*dashboard/);
    });

    await test.step('2. 导航到文件管理', async () => {
      await dashboardPage.navigateToFileManagement();
      await expect(fileManagementPage.table).toBeVisible();
    });

    await test.step('3. 搜索文件', async () => {
      await fileManagementPage.searchInput.fill('test');
      await fileManagementPage.searchButton.click();
      await page.waitForTimeout(2000);
      
      const rowCount = await fileManagementPage.getTableRowCount();
      expect(rowCount).toBeGreaterThanOrEqual(0);
    });
  });
});