/**
 * 书籍生成流程集成测试
 * 测试预估→检测类型→创建→进度完整流程
 */

import { describe, it, expect, beforeAll } from 'vitest';
import { apiPost, apiGet, apiPut, setAuthToken, initHttpClient, createTestUser } from '../../integration/http-client';

describe('书籍生成流程集成测试', () => {
  let authToken: string;
  let userId: string;

  beforeAll(async () => {
    // 创建测试用户
    try {
      const user = await createTestUser();
      authToken = user.token;
      userId = user.userId;
      setAuthToken(authToken);
    } catch (e) {
      console.log('创建测试用户失败，跳过部分测试');
    }
  });

  describe('Step 1: 预估 API', () => {
    it('预估字数和时长', async () => {
      if (!authToken) return;

      const res = await apiPost('/api/book-generator/langgraph/estimate', {
        topic: '人工智能发展史',
        wordCount: 50000
      });

      expect(res.code).toBe(0);
      expect(res.data?.estimatedCost).toBeGreaterThan(0);
      expect(res.data?.estimatedDuration).toBeGreaterThan(0);
    });

    it('无效字数返回错误', async () => {
      if (!authToken) return;

      const res = await apiPost('/api/book-generator/langgraph/estimate', {
        topic: '测试',
        wordCount: -100
      });

      expect(res.code).not.toBe(0);
    });
  });

  describe('Step 2: 检测书籍类型', () => {
    it('检测书籍类型成功', async () => {
      if (!authToken) return;

      const res = await apiPost('/api/book-generator/langgraph/detect-book-type', {
        topic: '人工智能简史'
      });

      expect(res.code).toBe(0);
      expect(['技术', '科普', '学术', '其他']).toContain(res.data?.bookType);
    });

    it('空主题返回错误', async () => {
      if (!authToken) return;

      const res = await apiPost('/api/book-generator/langgraph/detect-book-type', {
        topic: ''
      });

      expect(res.code).not.toBe(0);
    });
  });

  describe('Step 3: 创建书籍', () => {
    it('创建书籍成功', async () => {
      if (!authToken) return;

      const res = await apiPost('/api/book-generator/langgraph/create', {
        title: `测试书籍_${Date.now()}`,
        topic: '人工智能发展史',
        wordCount: 50000,
        strategy: 'deep-plan-parallel'
      });

      expect(res.code).toBe(0);
      expect(res.data?.bookId).toBeDefined();
    });

    it('配额不足时返回错误', async () => {
      if (!authToken) return;

      // 尝试超额创建
      const res = await apiPost('/api/book-generator/langgraph/create', {
        title: '超配额测试',
        topic: '测试',
        wordCount: 1000000 // 远超配额
      });

      // 应该返回配额不足错误
      expect(res.code).toBeGreaterThan(0);
    });
  });

  describe('Step 4: 查询书籍列表', () => {
    it('获取用户的书籍列表', async () => {
      if (!authToken) return;

      const res = await apiGet('/api/book-generator/langgraph/books');
      expect(res.code).toBe(0);
      expect(Array.isArray(res.data?.books)).toBe(true);
    });

    it('获取书籍详情', async () => {
      if (!authToken) return;

      // 先创建一本书
      const createRes = await apiPost('/api/book-generator/langgraph/create', {
        title: `详情测试书籍_${Date.now()}`,
        topic: '测试',
        wordCount: 30000
      });

      const bookId = createRes.data?.bookId;
      if (!bookId) return;

      const res = await apiGet(`/api/book-generator/langgraph/books/${bookId}`);
      expect(res.code).toBe(0);
      expect(res.data?.id).toBe(bookId);
    });
  });

  describe('Step 5: 章节生成进度', () => {
    it('查询章节生成状态', async () => {
      if (!authToken) return;

      // 先创建一本书
      const createRes = await apiPost('/api/book-generator/langgraph/create', {
        title: `章节测试书籍_${Date.now()}`,
        topic: '测试',
        wordCount: 20000
      });

      const bookId = createRes.data?.bookId;
      if (!bookId) return;

      const res = await apiGet(`/api/book-generator/langgraph/books/${bookId}/chapters`);
      expect(res.code).toBe(0);
    });
  });
});