/**
 * 最终全链路验证
 */

const SG = { ip: '43.156.237.110', port: 3911, key: 'zy_gtw_74d30f54fa8b5581514569ee7874def57861a31ccb2be8c9' }
const GZ = { ip: '43.139.217.141', port: 3910, key: 'zy_gtw_4a155446b7acc09fe46a2a29972c0ca5d9954da19c35dc23' }

async function callGK(srv, method, path, body = null) {
  const url = `http://${srv.ip}:${srv.port}${path}`
  const opts = { method, headers: { 'Authorization': `Bearer ${srv.key}`, 'Content-Type': 'application/json' } }
  if (body) opts.body = JSON.stringify(body)
  const res = await fetch(url, opts)
  const text = await res.text()
  try { return { status: res.status, data: JSON.parse(text) } }
  catch { return { status: res.status, data: { raw: text } } }
}

async function exec(srv, cmd) {
  const r = await callGK(srv, 'POST', '/exec', { cmd })
  return r.data
}

async function main() {
  console.log('═'.repeat(50))
  console.log('✅ 铸渊图片工作室 · 最终验收')
  console.log('═'.repeat(50))

  // 1. 新加坡本地服务检查
  console.log('\n1️⃣  新加坡image-studio:')
  const r1 = await exec(SG, 'curl -s -w "\nHTTP:%{http_code}" http://localhost:3912/ 2>&1 | tail -2')
  console.log('   localhost:3912:', (r1.stdout || '').trim())

  // 2. 新加坡Nginx代理
  const r2 = await exec(SG, 'curl -s -w "\nHTTP:%{http_code}" http://localhost/images/ 2>&1 | tail -2')
  console.log('   SG nginx /images/:', (r2.stdout || '').trim())

  // 3. 新加坡外网
  const r3 = await exec(SG, `curl -s -w "\nHTTP:%{http_code}" http://${SG.ip}/images/ 2>&1 | tail -2`)
  console.log('   SG public /images/:', (r3.stdout || '').trim())

  // 4. 广州→新加坡
  const r4 = await exec(GZ, `curl -s --connect-timeout 5 -w "\nHTTP:%{http_code}" http://${SG.ip}/images/ 2>&1 | tail -2`)
  console.log('   GZ→SG /images/:', (r4.stdout || '').trim())

  // 5. 广州Nginx代理
  const r5 = await exec(GZ, `curl -s -k --connect-timeout 5 -H "Host: guanghulab.com" -w "\nHTTP:%{http_code}" https://localhost/images/ 2>&1 | tail -2`)
  console.log('   GZ nginx /images/:', (r5.stdout || '').trim())

  // 6. 检查生成的测试图片
  console.log('\n2️⃣  验证生成的图片:')
  const r6 = await exec(SG, 'ls -la /data/image-studio/output/deploy_greeting.png 2>&1')
  console.log('   图片文件:', (r6.stdout || '').trim())
  
  // 7. 通过广州Nginx代理访问图片
  const imgName = 'deploy_greeting.png'
  const r7 = await exec(GZ, `curl -s -k --connect-timeout 5 -H "Host: guanghulab.com" -o /dev/null -w "HTTP:%{http_code} SIZE:%{size_download}" https://localhost/images/output/${imgName} 2>&1`)
  console.log('   广州Nginx→图片:', r7.stdout || r7.stderr)

  // 8. 生成更多测试
  console.log('\n3️⃣  生成不同风格的测试图:')
  const tests = [
    ['即刻风格', 'node generate.js --text "记录今日份思考\n种一棵树最好的时间是十年前\n其次是现在" --for "即刻" --output final_jike'],
    ['海报风格', 'node generate.js --text "公司放假通知\n时间：6月1日-6月5日\n请大家提前安排好工作\n祝大家节日快乐" --for "海报" --output final_poster'],
  ]
  for (const [name, cmd] of tests) {
    const r = await exec(SG, `cd /data/image-studio && ${cmd} 2>&1 | tail -5`)
    console.log(`   ${name}:`, (r.stdout || r.stderr || '').slice(0, 100))
  }

  // 9. 所有生成的文件
  console.log('\n4️⃣  所有图片列表:')
  const r8 = await exec(SG, 'ls -lhS /data/image-studio/output/*.png 2>&1')
  console.log(r8.stdout || r8.stderr)

  // 10. PM2状态
  console.log('\n5️⃣  PM2状态:')
  const r9 = await exec(SG, 'pm2 show image-studio 2>&1 | grep -E "status|uptime|restarts|port"')
  console.log(r9.stdout || r9.stderr)

  console.log('\n' + '═'.repeat(50))
  console.log('🎉 铸渊图片工作室部署完成！')
  console.log('═'.repeat(50))
  console.log()
  console.log('📎 访问入口:')
  console.log('   https://guanghulab.com/images/')
  console.log('   (广州Nginx → 新加坡Nginx → image-studio:3912)')
  console.log()
  console.log('🤖 如何使用:')
  console.log('   冰朔，你只需要在对话里告诉我内容和场景，')
  console.log('   我来理解、设计、生成配图。')
  console.log('   生成的图片在 https://guanghulab.com/images/ 可以直接查看下载。')
}

main().catch(err => console.error('❌', err))
