const crypto = require('crypto');
const fs = require('fs');
const path = require('path');

const SELFCLAW_API = 'https://selfclaw.ai/api/selfclaw/v1';
const keys = JSON.parse(fs.readFileSync(path.join(__dirname, '.selfclaw-keys.json'), 'utf8'));
const privKey = crypto.createPrivateKey({ key: Buffer.from(keys.privateKey, 'base64'), format: 'der', type: 'pkcs8' });

const timestamp = Date.now();
const nonce = crypto.randomBytes(16).toString('hex');
const message = JSON.stringify({ agentPublicKey: keys.publicKey, timestamp, nonce });
const signature = crypto.sign(null, Buffer.from(message), privKey).toString('hex');

async function run() {
  // Update agent profile with image
  const res = await fetch(SELFCLAW_API + '/update-agent', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      agentPublicKey: keys.publicKey,
      signature, timestamp, nonce,
      image: 'https://api.loopuman.com/logo.png',
      description: 'The Human Layer for AI — an ERC-8004 agent that coordinates real humans for real-world tasks via Telegram & WhatsApp with 8-second cUSD payments on Celo.',
      external_url: 'https://loopuman.com'
    })
  });
  const text = await res.text();
  console.log(res.ok ? '✅ Agent profile updated!' : '❌ Failed:', res.status, text.substring(0, 300));

  // Also check the registration.json to see if image is there
  const reg = await fetch(SELFCLAW_API + '/agent/loopuman/registration.json');
  if (reg.ok) {
    const data = await reg.json();
    console.log('Image in registration:', data.image || 'NOT SET');
  }
}
run().catch(e => console.error('Error:', e.message));
