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

test.describe('文件管理 E2E 测试', () => {
  let loginPage: LoginPage;
  let dashboardPage: DashboardPage;
  let fileManagementPage: FileManagementPage;

  test.beforeEach(async ({ page }) => {
    loginPage = new LoginPage(page);
    dashboardPage = new DashboardPage(page);
    fileManagementPage = new FileManagementPage(page);
  });

  test('FILE-001: 管理员查看文件列表', async ({ page }) => {
    await test.step('管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('导航到文件管理页面', async () => {
      await page.goto('/files');
      await page.waitForLoadState('networkidle');
      await page.waitForTimeout(3000);
    });

    await test.step('验证文件列表页面加载', async () => {
      await expect(fileManagementPage.table).toBeVisible();
      const rowCount = await fileManagementPage.getTableRowCount();
      expect(rowCount).toBeGreaterThanOrEqual(0);
    });

    await test.step('验证文件表格包含必要列', async () => {
      await expect(fileManagementPage.table).toContainText('文件名');
      await expect(fileManagementPage.table).toContainText('文件大小');
      await expect(fileManagementPage.table).toContainText('上传时间');
      await expect(fileManagementPage.table).toContainText('上传人');
    });
  });

  test('FILE-002: 上传文件', async ({ page }) => {
    await test.step('管理员登录并导航到文件管理', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await fileManagementPage.goto();
    });

    await test.step('上传测试文件', async () => {
      const testFilePath = './e2e/fixtures/test-file.txt';

      const uploadButton = page.locator('.el-upload');
      await uploadButton.first().click();
      
      const fileInput = page.locator('input[type="file"]');
      await fileInput.setInputFiles(testFilePath);
      await page.waitForTimeout(3000);

      await expect(fileManagementPage.table).toBeVisible();
    });
  });

  test('FILE-003: 搜索文件', async ({ page }) => {
    await test.step('管理员登录并导航到文件管理', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await fileManagementPage.goto();
    });

    await test.step('搜索特定文件', async () => {
      await fileManagementPage.searchFile('test');
      await page.waitForTimeout(1000);
    });

    await test.step('清除搜索条件', async () => {
      await fileManagementPage.clearSearch();
      const rowCount = await fileManagementPage.getTableRowCount();
      expect(rowCount).toBeGreaterThanOrEqual(0);
    });
  });

  test('FILE-004: 下载文件', async ({ page, context }) => {
    await test.step('管理员登录并导航到文件管理', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await fileManagementPage.goto();
    });

    await test.step('下载文件', async () => {
      const rows = await fileManagementPage.table.locator('.el-table__row').count();
      if (rows > 0) {
        const pagePromise = context.waitForEvent('page');
        await fileManagementPage.downloadFile('test');
        const newPage = await pagePromise;
        expect(newPage).toBeDefined();
        await newPage.close();
      }
    });
  });

  test('FILE-005: 删除文件', async ({ page }) => {
    await test.step('管理员登录并导航到文件管理', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await fileManagementPage.goto();
    });

    await test.step('删除文件', async () => {
      const rows = await fileManagementPage.table.locator('.el-table__row').count();
      if (rows > 0) {
        const firstRow = fileManagementPage.table.locator('.el-table__row').first();
        const fileName = await firstRow.locator('td').nth(1).textContent();

        if (fileName) {
          await fileManagementPage.deleteFile(fileName);
          await page.waitForTimeout(1000);

          await expect(fileManagementPage.table).toBeVisible();
        }
      }
    });
  });

  test('FILE-006: 验证文件权限控制', async ({ page }) => {
    await test.step('普通用户登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('尝试访问文件管理页面', async () => {
      await page.goto('/files');
      await page.waitForTimeout(2000);

      const currentURL = page.url();
      if (currentURL.includes('/files')) {
        const rows = await fileManagementPage.table.locator('.el-table__row').count();
        if (rows > 0) {
          await expect(fileManagementPage.table).toBeVisible();
        }
      } else {
        await expect(page).toHaveURL(/.*dashboard/);
      }
    });
  });

  test('FILE-007: 验证文件列表排序', async ({ page }) => {
    await test.step('管理员登录并导航到文件管理', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await fileManagementPage.goto();
    });

    await test.step('验证文件按上传时间排序', async () => {
      const rows = await fileManagementPage.table.locator('.el-table__row').count();
      if (rows > 0) {
        const firstRow = fileManagementPage.table.locator('.el-table__row').first();
        await expect(firstRow).toBeVisible();
      }
    });
  });

  test('FILE-008: 验证文件大小显示', async ({ page }) => {
    await test.step('管理员登录并导航到文件管理', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await fileManagementPage.goto();
    });

    await test.step('验证文件大小列显示', async () => {
      await expect(fileManagementPage.table).toContainText('文件大小');
    });
  });

  test('FILE-009: 验证文件上传人信息', async ({ page }) => {
    await test.step('管理员登录并导航到文件管理', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await fileManagementPage.goto();
    });

    await test.step('验证上传人列显示', async () => {
      await expect(fileManagementPage.table).toContainText('上传人');
    });
  });

  test('FILE-010: 验证文件操作按钮可见性', async ({ page }) => {
    await test.step('管理员登录并导航到文件管理', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await fileManagementPage.goto();
    });

    await test.step('验证表格可见', async () => {
      await expect(fileManagementPage.table).toBeVisible();
    });

    await test.step('验证搜索功能可用', async () => {
      const searchInput = page.locator('.search-bar input');
      await expect(searchInput).toBeVisible();
    });
  });
});