/**
 * 在guanghulab.com首页添加图片工作室入口卡片
 */

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 readFile(srv, path) {
  const r = await callGK(srv, 'POST', '/file/read', { path })
  if (r.status === 200) return r.data.content
  return null
}

async function writeFile(srv, path, content) {
  return (await callGK(srv, 'POST', '/file/write', { path, content })).status === 200
}

async function main() {
  // 读取首页HTML
  const html = await readFile(GZ, '/opt/guanghulab-repo/homepage/index.html')
  if (!html) {
    console.log('❌ 读取失败')
    return
  }
  
  console.log('当前首页大小:', html.length, '字符')

  // 在语料采集系统卡片和架构进度卡片之间插入图片工作室卡片
  // 找到语料采集卡片关闭后的位置
  
  // 插入标记：在 语料采集系统card 的 </div> 之后、架构进度的grid之前插入
  const marker = '<!-- 架构进度 + 工具链注册表 -->'
  const insertIdx = html.indexOf(marker)
  
  if (insertIdx === -1) {
    console.log('❌ 找不到插入位置')
    return
  }

  const imageStudioCard = `
    <!-- 铸渊图片工作室入口 -->
    <div class="card clickable breathe-amber" onclick="window.location.href='/images/'" style="cursor:pointer;border-color:rgba(212,160,48,.25)">
      <div class="card-header"><span class="hd amber"></span>铸渊图片工作室 <span class="tag" style="color:var(--amber)">神笔马良</span></div>
      <div class="server-entry">
        <div class="se-icon" style="border-color:rgba(212,160,48,.25);background:linear-gradient(135deg,rgba(212,160,48,.3),rgba(42,157,191,.25))">
          <span>🎨</span>
          <div class="pulse-ring" style="border-color:rgba(212,160,48,.15)"></div>
        </div>
        <div class="se-body">
          <div class="se-title">神笔马良 · 你说我画</div>
          <div class="se-desc">告诉铸渊内容和场景，自动生成小红书/即刻配图、海报、通知卡片</div>
          <div class="se-stats">
            <div class="se-stat"><span class="dot" style="background:var(--amber);box-shadow:0 0 8px var(--amber)"></span>小红书 · 即刻 · 海报</div>
            <div class="se-stat"><span class="dot" style="background:var(--amber);box-shadow:0 0 8px var(--amber)"></span>自动配色 · 内容自适应</div>
            <div class="se-stat"><span class="dot" style="background:var(--amber);box-shadow:0 0 8px var(--amber)"></span>新加坡渲染 · 广州分发</div>
          </div>
        </div>
        <div class="se-arrow" style="color:var(--amber)">→</div>
      </div>
    </div>

`

  const newHtml = html.slice(0, insertIdx) + imageStudioCard + html.slice(insertIdx)

  if (await writeFile(GZ, '/opt/guanghulab-repo/homepage/index.html', newHtml)) {
    console.log('✅ 首页更新成功')
    console.log('更新后大小:', newHtml.length, '字符')
    
    // 验证
    const verify = await readFile(GZ, '/opt/guanghulab-repo/homepage/index.html')
    if (verify && verify.includes('图片工作室')) {
      console.log('✅ 图片工作室入口已添加到首页')
    }
  } else {
    console.log('❌ 写入失败')
  }
}

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