/**
 * 调试课程创建 - 检查发送的请求数据
 */
import { test, expect } from '@playwright/test';
import { loginAsAdmin } from './helpers';

test.describe('课程创建调试 - 检查请求数据', () => {
  test('检查发送到 API 的请求数据', async ({ page }) => {
    test.setTimeout(180000);

    const courseName = '调试测试课程包-' + Date.now();
    console.log('=== 测试开始 ===');
    console.log('课程名称:', courseName);

    // 步骤 1: 登录
    console.log('=== 步骤 1: 登录 ===');
    await loginAsAdmin(page);
    await page.waitForTimeout(2000);

    // 步骤 2: 进入课程创建页面
    console.log('=== 步骤 2: 进入课程创建页面 ===');
    await page.goto('/admin/courses/create', { timeout: 30000, waitUntil: 'networkidle' });
    await page.waitForTimeout(3000);

    // 步骤 3: 填写基本信息
    console.log('=== 步骤 3: 填写基本信息 ===');
    await page.getByPlaceholder('请输入课程包名称').fill(courseName);

    console.log('选择主题...');
    await page.locator('.ant-select-selector').first().click();
    await page.waitForTimeout(500);
    const themeOption = page.getByText('社会认知', { exact: true }).first();
    await themeOption.click();
    await page.waitForTimeout(300);

    console.log('选择年级...');
    await page.getByRole('checkbox', { name: '小班' }).check();
    await page.waitForTimeout(300);

    console.log('填写核心内容...');
    await page.getByPlaceholder('请输入课程包核心内容').fill('调试测试课程内容');
    await page.waitForTimeout(500);

    // 步骤 4: 点击下一步到最后
    console.log('=== 步骤 4: 点击下一步 ===');
    for (let i = 0; i < 6; i++) {
      const nextButton = page.getByRole('button', { name: '下一步' });
      if (await nextButton.isVisible().catch(() => false)) {
        await nextButton.click();
        await page.waitForTimeout(800);
      }
    }
    await page.waitForTimeout(1000);

    // 步骤 5: 监控请求
    console.log('=== 步骤 5: 点击创建并监控请求 ===');

    const [requestPromise] = await Promise.all([
      page.waitForRequest(
        req => req.url().includes('/api/v1/admin/courses') && req.method() === 'POST',
        { timeout: 15000 }
      ).catch(() => null),
      page.locator('.step-actions .ant-btn-primary').last().click()
    ]);

    if (requestPromise) {
      const postData = requestPromise.postData();
      console.log('=== 请求体 ===');
      console.log(postData);

      try {
        const json = JSON.parse(postData || '{}');
        console.log('=== 解析后的 JSON ===');
        console.log(JSON.stringify(json, null, 2));
      } catch {
        console.log('无法解析 JSON');
      }

      // 等待响应
      const [response] = await Promise.all([
        page.waitForResponse(
          res => res.url().includes('/api/v1/admin/courses') && res.request().method() === 'POST',
          { timeout: 15000 }
        ).catch(() => null)
      ]);

      if (response) {
        console.log('=== 响应 ===');
        console.log('状态:', response.status());
        const responseBody = await response.text();
        console.log('响应体:', responseBody);
      }
    } else {
      console.log('未捕获到请求');
    }

    // 截图
    await page.screenshot({ path: 'test-results/debug-request-data.png' });
  });
});
