const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function checkBookOutline() {
  try {
    const book = await prisma.book.findUnique({
      where: { id: 1 },
      include: {
        chapters: {
          orderBy: [
            { level: 'asc' },
            { number: 'asc' }
          ]
        }
      }
    });

    if (!book) {
      console.log('❌ 书籍不存在 (id=1)');
      return;
    }

    console.log('📖 书籍信息:');
    console.log(`  ID: ${book.id}`);
    console.log(`  标题: ${book.title}`);
    console.log(`  状态: ${book.status}`);
    console.log(`  进度: ${book.progress}%`);
    console.log(`  章节数: ${book.totalChapters}`);
    console.log('');

    // 检查大纲
    if (book.outlineJson) {
      const outline = JSON.parse(book.outlineJson);
      console.log('✅ 大纲已生成:');
      console.log(`  主题: ${outline.mainTheme}`);
      console.log(`  结构逻辑: ${outline.structureLogic}`);
      console.log(`  章节数: ${outline.chapters?.length || 0}`);
      console.log('');
    } else {
      console.log('❌ 大纲未生成');
      console.log('');
    }

    // 统计各级章节
    const level1 = book.chapters.filter(c => c.level === 1);
    const level2 = book.chapters.filter(c => c.level === 2);
    const level3 = book.chapters.filter(c => c.level === 3);

    console.log('📊 章节统计:');
    console.log(`  章 (level=1): ${level1.length}`);
    console.log(`  节 (level=2): ${level2.length}`);
    console.log(`  小节 (level=3): ${level3.length}`);
    console.log('');

    // 检查大纲状态
    const outlineStatus = {
      pending: book.chapters.filter(c => c.status === 'pending').length,
      completed: book.chapters.filter(c => c.status === 'completed').length,
      failed: book.chapters.filter(c => c.status === 'failed').length,
    };

    console.log('📋 大纲状态 (status字段):');
    console.log(`  待生成: ${outlineStatus.pending}`);
    console.log(`  已完成: ${outlineStatus.completed}`);
    console.log(`  失败: ${outlineStatus.failed}`);
    console.log('');

    // 检查内容生成状态
    const contentStatus = {
      null: book.chapters.filter(c => c.contentStatus === null).length,
      pending: book.chapters.filter(c => c.contentStatus === 'pending').length,
      generating: book.chapters.filter(c => c.contentStatus === 'generating').length,
      completed: book.chapters.filter(c => c.contentStatus === 'completed').length,
      failed: book.chapters.filter(c => c.contentStatus === 'failed').length,
    };

    console.log('📝 内容生成状态 (contentStatus字段):');
    console.log(`  未开始: ${contentStatus.null}`);
    console.log(`  待生成: ${contentStatus.pending}`);
    console.log(`  生成中: ${contentStatus.generating}`);
    console.log(`  已完成: ${contentStatus.completed}`);
    console.log(`  失败: ${contentStatus.failed}`);
    console.log('');

    // 显示章列表
    if (level1.length > 0) {
      console.log('📚 章节列表:');
      level1.forEach(chapter => {
        console.log(`  ${chapter.number}. ${chapter.title} [${chapter.status}]`);
        
        // 显示该章下的节
        const sections = book.chapters.filter(c => c.parentId === chapter.id && c.level === 2);
        sections.forEach(section => {
          console.log(`    ${section.number}. ${section.title} [${section.status}]`);
          
          // 显示该节下的小节
          const subsections = book.chapters.filter(c => c.parentId === section.id && c.level === 3);
          subsections.forEach(sub => {
            const contentMark = sub.contentStatus === 'completed' ? '✅' : sub.contentStatus === 'failed' ? '❌' : '⏳';
            console.log(`      ${sub.number}. ${sub.title} [${sub.contentStatus || 'null'}] ${contentMark}`);
          });
        });
      });
    }

  } catch (error) {
    console.error('错误:', error);
  } finally {
    await prisma.$disconnect();
  }
}

checkBookOutline();
