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

/**
 * 竞赛管理 API 测试
 * 测试竞赛、报名、作品相关的接口
 */

test.describe('竞赛管理 API 测试', () => {

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

  test.beforeAll(async ({ loginViaAPI }) => {
    // 登录获取 Token
    const result = await loginViaAPI('gdlib');
    authToken = result.token;
    tenantCode = result.tenantCode;
  });

  test.describe('竞赛管理接口', () => {

    test('TC-CONTEST-001: 获取竞赛列表', async ({ request }) => {
      const response = await request.get(`${API_BASE}/api/contests`, {
        headers: {
          Authorization: `Bearer ${authToken}`,
        },
      });

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

      console.log('✓ TC-CONTEST-001 通过：获取竞赛列表');
    });

    test('获取竞赛列表 - 分页参数', async ({ request }) => {
      const response = await request.get(`${API_BASE}/api/contests?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();
      expect(data.data.total).toBeDefined();

      console.log('✓ 竞赛列表分页测试通过');
    });

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

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

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

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

        console.log('✓ TC-CONTEST-002 通过：获取竞赛详情');
      } else {
        console.log('○ 跳过测试：没有竞赛数据');
      }
    });

    test('创建竞赛 - 必填字段验证', async ({ request }) => {
      const response = await request.post(`${API_BASE}/api/contests`, {
        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('TC-REGISTRATION-001: 获取报名列表', async ({ request }) => {
      const response = await request.get(`${API_BASE}/api/contest-registrations`, {
        headers: {
          Authorization: `Bearer ${authToken}`,
        },
      });

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

      console.log('✓ TC-REGISTRATION-001 通过：获取报名列表');
    });

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

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

        const response = await request.get(`${API_BASE}/api/contest-registrations/${registrationId}`, {
          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/contest-works`, {
        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/contest-works`, {
        headers: { Authorization: `Bearer ${authToken}` },
      });
      const listData = await listResponse.json();

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

        const response = await request.get(`${API_BASE}/api/contest-works/${workId}`, {
          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/contest-review-rules`, {
        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/contest-review-dimensions`, {
        headers: { Authorization: `Bearer ${authToken}` },
      });

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

      console.log('✓ 获取评审维度通过');
    });
  });

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

    test('获取不存在的竞赛详情', async ({ request }) => {
      const response = await request.get(`${API_BASE}/api/contests/999999999`, {
        headers: { Authorization: `Bearer ${authToken}` },
      });

      const data = await response.json();
      // 应该返回竞赛不存在
      expect(data.code).not.toBe(200);

      console.log('✓ 获取不存在的竞赛返回正确错误');
    });

    test('无 Token 访问竞赛接口', async ({ request }) => {
      const response = await request.get(`${API_BASE}/api/contests`);

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

      console.log('✓ 无 Token 访问竞赛接口返回 401');
    });
  });
});
