/**
 * L2 回归测试：AI书籍生成（核心模块）
 * 覆盖：书籍 CRUD、大纲生成、章节生成、音频生成、合并、发布
 */
import { test, expect } from '@playwright/test';
import { apiGet, apiPost, apiPut, apiDelete, initApiClient, disposeApiClient } from '../helpers/api-client';
import { trackResource } from '../helpers/test-data';

let testBookId: number;

test.describe('AI书籍生成 (BookGenerator)', () => {
  test.beforeAll(async () => { await initApiClient(); });
  test.afterAll(async () => { await disposeApiClient(); });

  // ===== 书籍 CRUD =====
  test('BG01: 获取书籍列表 GET /api/book-generator/langgraph/books', async () => {
    const res = await apiGet('/api/book-generator/langgraph/books');
    expect(res.code).toBe(0);
  });

  test('BG02: 创建书籍 POST /api/book-generator/langgraph/books', async () => {
    const res = await apiPost('/api/book-generator/langgraph/books', {
      title: `[自动化测试] 测试书籍 ${Date.now()}`,
      description: '由自动化回归测试创建',
    });
    expect(res.code).toBe(0);
    testBookId = res.data?.id || res.data?.book?.id;
    expect(testBookId).toBeDefined();

    if (testBookId) {
      trackResource('book', testBookId, async () => {
        await apiDelete(`/api/book-generator/langgraph/books/${testBookId}`);
      });
    }
  });

  test('BG03: 获取书籍详情 GET /api/book-generator/langgraph/books/:id', async () => {
    if (!testBookId) return test.skip();
    const res = await apiGet(`/api/book-generator/langgraph/books/${testBookId}`);
    expect(res.code).toBe(0);
  });

  test('BG04: 获取工作流状态 GET /api/book-generator/langgraph/workflow/:id', async () => {
    if (!testBookId) return test.skip();
    const res = await apiGet(`/api/book-generator/langgraph/workflow/${testBookId}`);
    expect([0, 404]).toContain(res.code);
  });

  test('BG05: 获取生成进度 GET /api/book-generator/langgraph/books/:id/progress', async () => {
    if (!testBookId) return test.skip();
    const res = await apiGet(`/api/book-generator/langgraph/books/${testBookId}/progress`);
    expect([0, 404]).toContain(res.code);
  });

  test('BG06: 获取完整内容 GET /api/book-generator/langgraph/books/:id/full-content', async () => {
    if (!testBookId) return test.skip();
    const res = await apiGet(`/api/book-generator/langgraph/books/${testBookId}/full-content`);
    expect([0, 404]).toContain(res.code);
  });

  // ===== 生成操作 =====
  test('BG07: 生成大纲 POST /api/book-generator/langgraph/books/:id/outline', async () => {
    if (!testBookId) return test.skip();
    const res = await apiPost(`/api/book-generator/langgraph/books/${testBookId}/outline`);
    expect([0, 400, 409]).toContain(res.code);
  });

  test('BG08: 切换发布状态 PUT /api/book-generator/langgraph/books/:id/publish', async () => {
    if (!testBookId) return test.skip();
    const res = await apiPut(`/api/book-generator/langgraph/books/${testBookId}/publish`);
    expect([0, 400, 404]).toContain(res.code);
  });

  // ===== AI生成接口 =====
  test('BG09: AI生成接口 - 仅 POST /api/book-generator/ai-generate，跳过 GET', async () => {
    // /api/book-generator/books 是 POST /ai-generate，不是 GET
    test.skip();
  });

  // ===== 清理测试数据 =====
  test('BG10: 删除测试书籍 DELETE /api/book-generator/langgraph/books/:id', async () => {
    if (!testBookId) return test.skip();
    const res = await apiDelete(`/api/book-generator/langgraph/books/${testBookId}`);
    expect([0, 404, 403]).toContain(res.code);
  });
});
