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

async function verifyNewAudio() {
  console.log('\n✅ 验证新生成的音频\n');
  console.log('=' .repeat(60));
  
  const bookId = 5;
  const chapterId = 1214;
  
  const chapter = await prisma.bookChapter.findUnique({
    where: { id: chapterId },
    select: {
      id: true,
      title: true,
      audioUrl: true,
      audioDuration: true,
    }
  });
  
  console.log(`章节: ${chapter.title}`);
  console.log(`ID: ${chapter.id}`);
  console.log(`audioUrl: ${chapter.audioUrl || '无'}`);
  console.log(`audioDuration: ${chapter.audioDuration || 0}秒\n`);
  
  // 检查文件
  const fs = require('fs');
  const path = require('path');
  
  const uuid = chapter.audioUrl.match(/\/uploads\/([^\/]+)\//)?.[1];
  if (uuid) {
    const outputPath = path.join(__dirname, 'uploads', uuid, 'output.mp3');
    const segmentPath = path.join(__dirname, 'uploads', uuid, 'segment_0.mp3');
    
    console.log('📁 文件检查:\n');
    console.log(`output.mp3: ${fs.existsSync(outputPath) ? '✅ 存在' : '❌ 不存在'}`);
    console.log(`segment_0.mp3: ${fs.existsSync(segmentPath) ? '✅ 存在' : '❌ 不存在'}\n`);
    
    if (fs.existsSync(outputPath)) {
      const stats = fs.statSync(outputPath);
      console.log(`output.mp3 大小: ${(stats.size / 1024).toFixed(2)} KB\n`);
    }
    
    console.log('=' .repeat(60));
    console.log('\n📋 验证结论:\n');
    
    if (chapter.audioUrl?.includes('output.mp3') && fs.existsSync(outputPath)) {
      console.log('✅✅✅ 完美！\n');
      console.log('  1. 音频URL使用output.mp3 ✅');
      console.log('  2. 文件output.mp3存在 ✅');
      console.log('  3. 代码修复已生效 ✅\n');
      console.log('前端访问:');
      console.log(`  http://localhost:5173/#/pages/player/index?id=${chapterId}\n`);
    } else {
      console.log('❌ 验证失败\n');
    }
  }
  
  await prisma.$disconnect();
}

verifyNewAudio().catch(console.error);
