/**
 * 全链路验证
 */

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️⃣  新加坡本地:')
  const r1 = await exec(SG, 'curl -s -w "\nHTTP:%{http_code}" http://localhost/images/ 2>&1 | tail -5')
  console.log('   localhost/images/:', (r1.stdout || '').slice(0, 100))

  // 2. 新加坡外网（通过80端口）
  console.log('\n2️⃣  新加坡外网:')
  const r2 = await exec(SG, `curl -s -w "\nHTTP:%{http_code}" http://${SG.ip}/images/ 2>&1 | tail -5`)
  console.log('   SG外网/images/:', (r2.stdout || '').slice(0, 100))

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

  // 4. 广州Nginx本地测试
  console.log('\n4️⃣  广州Nginx:')
  const r4 = await exec(GZ, `curl -s -k --connect-timeout 5 -w "\nHTTP:%{http_code}" https://localhost/images/ 2>&1 | tail -5`)
  console.log('   localhost/images/:', (r4.stdout || '').slice(0, 100))

  // 5. 新加坡生成一张测试图
  console.log('\n5️⃣  生成测试图片:')
  const r5 = await exec(SG, 'cd /data/image-studio && node generate.js --text "你好冰朔\n这是铸渊在新加坡为你生成的图片\n图片工作室已部署完成" --for "小红书" --output deploy_test 2>&1 | tail -5')
  console.log('   生成结果:', r5.stdout || r5.stderr)
  
  // 6. 验证图片能访问
  const r6 = await exec(SG, 'ls -la /data/image-studio/output/deploy_test*.png 2>&1')
  console.log('   图片文件:', r6.stdout || r6.stderr)
  
  const r7 = await exec(SG, 'ls /data/image-studio/output/deploy_test*.png 2>&1 | head -1 | xargs -I{} basename {}')
  const imgName = (r7.stdout || '').trim()
  if (imgName) {
    const r8 = await exec(GZ, `curl -s --connect-timeout 5 -o /dev/null -w "HTTP:%{http_code} SIZE:%{size_download}" http://${SG.ip}/images/output/${imgName} 2>&1`)
    console.log(`   广州→新加坡 图片访问:`, r8.stdout || r8.stderr)
    
    const r9 = await exec(GZ, `curl -s -k --connect-timeout 5 -o /dev/null -w "HTTP:%{http_code} SIZE:%{size_download}" https://localhost/images/output/${imgName} 2>&1`)
    console.log(`   广州Nginx→图片:`, r9.stdout || r9.stderr)
  }

  // 7. 生成API测试
  console.log('\n6️⃣  API测试:')
  const r10 = await exec(SG, `curl -s -X POST http://localhost:3912/api/generate -H "Content-Type: application/json" -d '{"text":"测试API","for":"即刻"}' 2>&1 | python3 -m json.tool 2>&1 | head -10`)
  console.log('   API生成:', r10.stdout || r10.stderr)

  console.log('\n' + '═'.repeat(50))
  console.log('📋 链路验证完成')
  console.log('═'.repeat(50))
  console.log('\n📎 访问入口:')
  console.log('   新加坡直连: http://43.156.237.110/images/')
  console.log('   广州代理:   https://guanghulab.com/images/')
}

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