import { test, expect } from '../fixtures/auth.fixture';

/**
 * 系统配置 API 测试
 * 测试字典、系统配置、日志等接口
 */

test.describe('系统配置 API 测试', () => {

  const API_BASE = 'http://localhost:8580';
  let authToken: string;

  test.beforeAll(async ({ loginViaAPI }) => {
    // 使用超管账号登录
    const result = await loginViaAPI('platform');
    authToken = result.token;
  });

  test.describe('数据字典接口', () => {

    test('获取字典列表', async ({ request }) => {
      const response = await request.get(`${API_BASE}/api/dicts`, {
        headers: {
          Authorization: `Bearer ${authToken}`,
        },
      });

      const data = await response.json();
      expect(data.code).toBe(200);

      console.log('✓ 获取字典列表通过');
    });

    test('获取字典详情', async ({ request }) => {
      // 先获取字典列表
      const listResponse = await request.get(`${API_BASE}/api/dicts`, {
        headers: { Authorization: `Bearer ${authToken}` },
      });
      const listData = await listResponse.json();

      if (listData.data && listData.data.records && listData.data.records.length > 0) {
        const dictId = listData.data.records[0].id;

        const response = await request.get(`${API_BASE}/api/dicts/${dictId}`, {
          headers: { Authorization: `Bearer ${authToken}` },
        });

        const data = await response.json();
        expect(data.code).toBe(200);

        console.log('✓ 获取字典详情通过');
      } else {
        console.log('○ 跳过测试：没有字典数据');
      }
    });

    test('获取字典项列表', async ({ request }) => {
      const response = await request.get(`${API_BASE}/dict-items`, {
        headers: {
          Authorization: `Bearer ${authToken}`,
        },
      });

      const data = await response.json();
      expect(data.code).toBe(200);

      console.log('✓ 获取字典项列表通过');
    });

    test('创建字典 - 必填字段验证', async ({ request }) => {
      const response = await request.post(`${API_BASE}/api/dicts`, {
        headers: {
          Authorization: `Bearer ${authToken}`,
          'Content-Type': 'application/json',
        },
        data: {
          // 不提供任何字段
        },
      });

      const data = await response.json();
      // 应该返回验证失败
      expect(data.code).not.toBe(200);

      console.log('✓ 创建字典 - 必填字段验证通过');
    });
  });

  test.describe('系统配置接口', () => {

    test('获取系统配置列表', async ({ request }) => {
      const response = await request.get(`${API_BASE}/api/sys-configs`, {
        headers: {
          Authorization: `Bearer ${authToken}`,
        },
      });

      const data = await response.json();
      expect(data.code).toBe(200);

      console.log('✓ 获取系统配置列表通过');
    });

    test('获取系统配置详情', async ({ request }) => {
      // 先获取配置列表
      const listResponse = await request.get(`${API_BASE}/api/sys-configs`, {
        headers: { Authorization: `Bearer ${authToken}` },
      });
      const listData = await listResponse.json();

      if (listData.data && listData.data.records && listData.data.records.length > 0) {
        const configId = listData.data.records[0].id;

        const response = await request.get(`${API_BASE}/api/sys-configs/${configId}`, {
          headers: { Authorization: `Bearer ${authToken}` },
        });

        const data = await response.json();
        expect(data.code).toBe(200);

        console.log('✓ 获取系统配置详情通过');
      } else {
        console.log('○ 跳过测试：没有配置数据');
      }
    });

    test('获取系统配置 - 根据 Key', async ({ request }) => {
      const response = await request.get(`${API_BASE}/api/sys-configs/key/mail.server.host`, {
        headers: { Authorization: `Bearer ${authToken}` },
      });

      // 可能返回配置或空
      console.log('✓ 根据 Key 获取系统配置测试完成');
    });
  });

  test.describe('系统日志接口', () => {

    test('获取系统日志列表', async ({ request }) => {
      const response = await request.get(`${API_BASE}/api/sys-logs`, {
        headers: {
          Authorization: `Bearer ${authToken}`,
        },
      });

      const data = await response.json();
      expect(data.code).toBe(200);

      console.log('✓ 获取系统日志列表通过');
    });

    test('获取系统日志 - 分页参数', async ({ request }) => {
      const response = await request.get(`${API_BASE}/api/sys-logs?page=1&pageSize=10`, {
        headers: { Authorization: `Bearer ${authToken}` },
      });

      const data = await response.json();
      expect(data.code).toBe(200);

      expect(data.data).toBeTruthy();
      expect(data.data.records).toBeTruthy();

      console.log('✓ 系统日志分页测试通过');
    });

    test('获取系统日志详情', async ({ request }) => {
      // 先获取日志列表
      const listResponse = await request.get(`${API_BASE}/api/sys-logs`, {
        headers: { Authorization: `Bearer ${authToken}` },
      });
      const listData = await listResponse.json();

      if (listData.data && listData.data.records && listData.data.records.length > 0) {
        const logId = listData.data.records[0].id;

        const response = await request.get(`${API_BASE}/api/sys-logs/${logId}`, {
          headers: { Authorization: `Bearer ${authToken}` },
        });

        const data = await response.json();
        expect(data.code).toBe(200);

        console.log('✓ 获取系统日志详情通过');
      } else {
        console.log('○ 跳过测试：没有日志数据');
      }
    });
  });

  test.describe('统计接口', () => {

    test('获取日志统计信息', async ({ request }) => {
      const response = await request.get(`${API_BASE}/api/sys-logs/statistics`, {
        headers: {
          Authorization: `Bearer ${authToken}`,
        },
      });

      const data = await response.json();
      expect(data.code).toBe(200);

      console.log('✓ 获取日志统计信息通过');
    });

    test('获取用户统计信息', async ({ request }) => {
      const response = await request.get(`${API_BASE}/api/users/statistics`, {
        headers: {
          Authorization: `Bearer ${authToken}`,
        },
      });

      const data = await response.json();
      expect(data.code).toBe(200);

      console.log('✓ 获取用户统计信息通过');
    });
  });

  test.describe('边界情况', () => {

    test('无 Token 访问系统配置接口', async ({ request }) => {
      const response = await request.get(`${API_BASE}/api/dicts`);

      // 应该返回 401
      expect(response.status()).toBe(401);

      console.log('✓ 无 Token 访问系统配置接口返回 401');
    });
  });
});
