import { test, expect } from '@playwright/test';

test.describe('Documents', () => {
  test.beforeEach(async ({ page }) => {
    // 设置已登录状态
    await page.goto('/dashboard');
    await page.evaluate(() => {
      localStorage.setItem('auth-storage', JSON.stringify({
        state: {
          user: { id: '1', username: 'test' },
          token: 'test-token',
          isAuthenticated: true,
        },
        version: 0,
      }));
      localStorage.setItem('auth_token', 'test-token');
    });

    // Mock API
    await page.route('**/api/documents**', (route) => {
      if (route.request().method() === 'GET') {
        route.fulfill({
          status: 200,
          contentType: 'application/json',
          body: JSON.stringify({
            success: true,
            data: [
              {
                id: '1',
                title: '测试文档',
                content: '这是测试内容',
                created_at: '2024-01-01T00:00:00Z',
                updated_at: '2024-01-01T00:00:00Z',
                user_id: '1',
                category_id: null,
              },
            ],
            count: 1,
          }),
        });
      }
    });
  });

  test('应该显示文档列表', async ({ page }) => {
    await page.goto('/documents');

    await expect(page.locator('h1')).toContainText('文档管理');
    await expect(page.locator('text=测试文档')).toBeVisible();
    await expect(page.locator('text=这是测试内容')).toBeVisible();
  });

  test('应该打开创建文档表单', async ({ page }) => {
    await page.goto('/documents');
    await page.click('button:has-text("新建文档")');

    await expect(page.locator('text=新建文档')).toBeVisible();
    await expect(page.locator('input[label="标题"]')).toBeVisible();
  });

  test('应该创建新文档', async ({ page }) => {
    await page.route('**/api/documents', (route) => {
      if (route.request().method() === 'POST') {
        route.fulfill({
          status: 201,
          contentType: 'application/json',
          body: JSON.stringify({
            success: true,
            data: {
              id: '2',
              title: '新文档',
              content: '新文档内容',
              created_at: '2024-01-01T00:00:00Z',
              updated_at: '2024-01-01T00:00:00Z',
              user_id: '1',
              category_id: null,
            },
          }),
        });
      }
    });

    await page.goto('/documents');
    await page.click('button:has-text("新建文档")');

    await page.fill('input[label="标题"]', '新文档');
    await page.fill('textarea[placeholder="文档内容"]', '新文档内容');
    await page.click('button:has-text("创建")');

    // 验证表单关闭
    await expect(page.locator('text=新建文档')).not.toBeVisible();
  });

  test('应该删除文档', async ({ page }) => {
    await page.route('**/api/documents/*', (route) => {
      if (route.request().method() === 'DELETE') {
        route.fulfill({
          status: 200,
          contentType: 'application/json',
          body: JSON.stringify({
            success: true,
            message: '文档已删除',
          }),
        });
      }
    });

    await page.goto('/documents');

    // 点击删除按钮
    const deleteButton = page.locator('button').filter({ hasText: '' }).first();
    await deleteButton.click();

    // 确认删除
    page.on('dialog', (dialog) => dialog.accept());
  });

  test('应该搜索文档', async ({ page }) => {
    await page.route('**/api/documents/search**', (route) => {
      route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({
          success: true,
          data: [
            {
              id: '1',
              title: '搜索结果',
              content: '包含关键词的内容',
              created_at: '2024-01-01T00:00:00Z',
              updated_at: '2024-01-01T00:00:00Z',
              user_id: '1',
              category_id: null,
            },
          ],
        }),
      });
    });

    await page.goto('/documents');
    await page.fill('input[placeholder="搜索文档..."]', '关键词');
    await page.click('button:has-text("搜索")');

    await expect(page.locator('text=搜索结果')).toBeVisible();
  });
});
