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

async function fixStatus() {
  console.log('\n📊 检查status字段：\n');
  
  const bookId = 5;
  
  // 检查status字段
  const statusCount = await prisma.bookChapter.groupBy({
    by: ['status'],
    where: {
      bookId,
      level: 3
    },
    _count: true
  });
  
  console.log('小节status统计：');
  statusCount.forEach(s => {
    console.log(`  ${s.status || 'null'}: ${s._count}个`);
  });
  
  // 检查contentStatus
  const contentStatusCount = await prisma.bookChapter.groupBy({
    by: ['contentStatus'],
    where: {
      bookId,
      level: 3
    },
    _count: true
  });
  
  console.log('\n小节contentStatus统计：');
  contentStatusCount.forEach(s => {
    console.log(`  ${s.contentStatus || 'null'}: ${s._count}个`);
  });
  
  // 如果status不是completed，修复它
  const notCompleted = await prisma.bookChapter.count({
    where: {
      bookId,
      level: 3,
      status: { not: 'completed' }
    }
  });
  
  if (notCompleted > 0) {
    console.log(`\n🔧 发现${notCompleted}个小节status不是completed，正在修复...\n`);
    
    const updated = await prisma.bookChapter.updateMany({
      where: {
        bookId,
        level: 3,
        status: { not: 'completed' }
      },
      data: {
        status: 'completed'
      }
    });
    
    console.log(`✅ 已更新${updated.count}个小节的status为completed\n`);
  } else {
    console.log('\n✅ 所有小节status都是completed\n');
  }
  
  await prisma.$disconnect();
}

fixStatus().catch(console.error);
