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

async function fixBookStatus() {
  try {
    // 更新bookId=1的所有level=1的章，将status从pending改为completed
    const result = await prisma.bookChapter.updateMany({
      where: {
        bookId: 1,
        level: 1,
        status: 'pending'
      },
      data: {
        status: 'completed'
      }
    });

    console.log(`✅ 已修复 ${result.count} 个章的状态: pending → completed`);

    // 验证结果
    const book = await prisma.book.findUnique({
      where: { id: 1 },
      include: {
        chapters: true
      }
    });

    const level1Chapters = book.chapters.filter(c => c.level === 1);
    const statusCount = {
      pending: level1Chapters.filter(c => c.status === 'pending').length,
      completed: level1Chapters.filter(c => c.status === 'completed').length,
      failed: level1Chapters.filter(c => c.status === 'failed').length,
    };

    console.log('\n📊 验证结果:');
    console.log(`  章总数: ${level1Chapters.length}`);
    console.log(`  pending: ${statusCount.pending}`);
    console.log(`  completed: ${statusCount.completed}`);
    console.log(`  failed: ${statusCount.failed}`);

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

fixBookStatus();
