/**
 * 多章测试 - 直接调用策略3生成12000字书籍
 * 绕过队列，同步执行
 */
const BOOK_ID = 196;
const BOOK_SCALE = '12000';

async function main() {
  const { setCurrentStrategy } = await import('../strategies/selector');
  const { langGraphGenerator } = await import('../index');
  const { bookStore } = await import('../book-generator.store');
  const { resolveGenLevel } = await import('../index');

  setCurrentStrategy('per-chapter' as any);

  const genLevel = resolveGenLevel(BOOK_SCALE);
  console.log(`\n====== 策略3 - 多章测试 ======`);
  console.log(`bookId: ${BOOK_ID}, scale: ${BOOK_SCALE}, genLevel: ${genLevel}`);
  console.log(`==============================\n`);

  const startTime = Date.now();

  try {
    await langGraphGenerator.generate(
      BOOK_ID.toString(),
      '面向初中生的科普书，用通俗易懂的语言解释时间的概念、测量历史、相对论和时空观，内容系统和全面',
      BOOK_SCALE
    );

    const elapsed = Date.now() - startTime;
    console.log(`\n✅ 生成完成，耗时 ${(elapsed / 1000).toFixed(1)}s`);

    // 查看结果
    const result = await bookStore.getById(BOOK_ID.toString());
    if (result) {
      const cs = result.chapters || [];
      console.log(`\n📚 共 ${cs.length} 章`);
      for (const c of cs) {
        const contentLen = (c.content || '').length;
        const status = c.genStage || 'unknown';
        console.log(`  [${c.level}] 章${c.number}. ${c.title}`);
        console.log(`     状态: ${status} | 内容: ${contentLen}字`);
      }

      if (result.bookAnalysis) {
        const plan = JSON.parse(result.bookAnalysis);
        console.log(`\n📋 AI规划:`);
        console.log(`  层级: ${plan.genLevel}, 风格: ${plan.writingStyle}, 结构: ${plan.structureLogic}`);
        console.log(`  深度: ${plan.contentDepth}, 理由: ${(plan.reasoning || '').slice(0, 100)}`);
      }
    }
  } catch (err) {
    console.error('❌ 生成失败:', err);
  }
}

main().catch(console.error);
