require('dotenv').config();
const { ethers } = require('ethers');

const PRIVATE_KEY = process.env.CELO_PRIVATE_KEY;
const AGENT_ID = 17;
const REGISTRY = '0x8004A169FB4a3325136EB29fA0ceB6D2e539a432';

const metadata = {
  "type": "https://eips.ethereum.org/EIPS/eip-8004#registration-v1",
  "name": "Loopuman",
  "description": "The Human Layer for AI — routes tasks to verified human workers worldwide via Telegram & WhatsApp. AI agents send requests, humans complete them, quality-checked results returned with 8-second cUSD payments on Celo.",
  "image": "https://api.loopuman.com/logo.png",
  "external_url": "https://loopuman.com",
  "version": "4.2.1",
  "active": true,
  "tags": ["microtasking","human-in-the-loop","data-labeling","verification","oracle","task-routing","crypto-payments","AI-agents"],
  "services": [
    {"name": "MCP", "version": "2025-06-18", "endpoint": "https://api.loopuman.com/.well-known/mcp.json"},
    {"name": "A2A", "version": "0.3.0", "endpoint": "https://api.loopuman.com/.well-known/agent-card.json"},
    {"name": "OASF", "version": "1.0.0", "endpoint": "https://api.loopuman.com/.well-known/oasf.json"},
    {"name": "web", "endpoint": "https://loopuman.com"},
    {"name": "api", "endpoint": "https://api.loopuman.com"}
  ],
  "skills": [
    {"name": "data_engineering/data_labeling", "id": 602},
    {"name": "data_engineering/data_collection", "id": 601},
    {"name": "data_engineering/data_quality_management", "id": 604},
    {"name": "agent_orchestration/agent_coordination", "id": 1004},
    {"name": "advanced_reasoning_planning/task_planning", "id": 1001}
  ],
  "domains": [
    {"name": "technology/data_science", "id": 1601},
    {"name": "technology/artificial_intelligence", "id": 1602},
    {"name": "finance_and_business/business_operations", "id": 1701}
  ],
  "publisher": {
    "name": "Loopuman",
    "website": "https://loopuman.com",
    "twitter": "https://x.com/loopuman",
    "github": "https://github.com/seesayearn-boop"
  },
  "supportedTrust": ["reputation"],
  "registrations": [
    {"agentId": 17, "agentRegistry": "eip155:42220:0x8004A169FB4a3325136EB29fA0ceB6D2e539a432"}
  ]
};

async function run() {
  if (!PRIVATE_KEY) {
    console.log('No private key found. Checking .env keys...');
    console.log(Object.keys(process.env).filter(k => k.includes('KEY') || k.includes('PRIVATE')));
    return;
  }
  const provider = new ethers.JsonRpcProvider('https://forno.celo.org');
  const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
  const contract = new ethers.Contract(REGISTRY, ['function setAgentURI(uint256,string)'], wallet);
  const dataURI = 'data:application/json;base64,' + Buffer.from(JSON.stringify(metadata)).toString('base64');
  console.log('Updating on-chain metadata...');
  const tx = await contract.setAgentURI(AGENT_ID, dataURI);
  console.log('TX:', tx.hash);
  await tx.wait();
  console.log('✅ Done! Version 4.2.1, cryptoEconomic removed, all endpoints updated');
}

run().catch(e => console.error('Error:', e.message));
