/**
 * 教师端 E2E 测试 - API 接口测试
 */

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

test.describe('教师端 API 接口测试', () => {
  test.beforeEach(async ({ page }) => {
    await loginAsTeacher(page);
  });

  test('验证仪表盘 API 调用', async ({ page }) => {
    // 设置监听 API 请求
    const [response] = await Promise.all([
      page.waitForResponse(/.*\/teacher\/dashboard.*/, { timeout: 10000 }),
      page.goto('/teacher/dashboard'),
    ]);

    // 验证响应状态
    expect(response.status()).toBe(200);

    // 验证响应数据格式
    const jsonData = await response.json();
    expect(jsonData).toBeDefined();
    test.info().annotations.push({
      type: 'info',
      description: `仪表盘 API 响应：${JSON.stringify(jsonData).substring(0, 200)}`,
    });
  });

  test('验证课程列表 API 调用', async ({ page }) => {
    await page.goto('/teacher/courses');

    try {
      const [response] = await Promise.all([
        page.waitForResponse(/.*\/teacher\/courses.*/, { timeout: 10000 }),
      ]);

      expect(response.status()).toBe(200);

      const jsonData = await response.json();
      test.info().annotations.push({
        type: 'info',
        description: `课程列表 API 响应：${JSON.stringify(jsonData).substring(0, 200)}`,
      });
    } catch (e) {
      test.info().annotations.push({
        type: 'warning',
        description: '课程列表 API 请求未捕获到',
      });
    }
  });

  test('验证班级列表 API 调用', async ({ page }) => {
    await page.goto('/teacher/classes');

    try {
      const [response] = await Promise.all([
        page.waitForResponse(/.*\/teacher\/classes.*/, { timeout: 10000 }),
      ]);

      expect(response.status()).toBe(200);

      const jsonData = await response.json();
      test.info().annotations.push({
        type: 'info',
        description: `班级列表 API 响应：${JSON.stringify(jsonData).substring(0, 200)}`,
      });
    } catch (e) {
      test.info().annotations.push({
        type: 'warning',
        description: '班级列表 API 请求未捕获到',
      });
    }
  });

  test('验证授课记录列表 API 调用', async ({ page }) => {
    await page.goto('/teacher/lessons');

    try {
      const [response] = await Promise.all([
        page.waitForResponse(/.*\/teacher\/lessons.*/, { timeout: 10000 }),
      ]);

      expect(response.status()).toBe(200);

      const jsonData = await response.json();
      test.info().annotations.push({
        type: 'info',
        description: `授课记录 API 响应：${JSON.stringify(jsonData).substring(0, 200)}`,
      });
    } catch (e) {
      test.info().annotations.push({
        type: 'warning',
        description: '授课记录 API 请求未捕获到',
      });
    }
  });

  test('验证任务列表 API 调用', async ({ page }) => {
    await page.goto('/teacher/tasks');

    try {
      const [response] = await Promise.all([
        page.waitForResponse(/.*\/teacher\/tasks.*/, { timeout: 10000 }),
      ]);

      expect(response.status()).toBe(200);

      const jsonData = await response.json();
      test.info().annotations.push({
        type: 'info',
        description: `任务列表 API 响应：${JSON.stringify(jsonData).substring(0, 200)}`,
      });
    } catch (e) {
      test.info().annotations.push({
        type: 'warning',
        description: '任务列表 API 请求未捕获到',
      });
    }
  });

  test('验证课表 API 调用', async ({ page }) => {
    await page.goto('/teacher/schedule');

    try {
      const [response] = await Promise.all([
        page.waitForResponse(/.*\/teacher\/schedules.*/, { timeout: 10000 }),
      ]);

      expect(response.status()).toBe(200);

      const jsonData = await response.json();
      test.info().annotations.push({
        type: 'info',
        description: `课表 API 响应：${JSON.stringify(jsonData).substring(0, 200)}`,
      });
    } catch (e) {
      test.info().annotations.push({
        type: 'warning',
        description: '课表 API 请求未捕获到',
      });
    }
  });
});
