const { ethers } = require('ethers');
const fs = require('fs');
const path = require('path');

// Use agent owner wallet, not treasury
const walletData = JSON.parse(fs.readFileSync(path.join(__dirname, '.selfclaw-wallet.json'), 'utf8'));
const AGENT_ID = 17;
const REGISTRY = '0x8004A169FB4a3325136EB29fA0ceB6D2e539a432';

const metadata = {
  "name": "Loopuman",
  "tags": ["microtasking","human-in-the-loop","data-labeling","verification","oracle","task-routing","crypto-payments","AI-agents","x402","celo"],
  "type": "https://eips.ethereum.org/EIPS/eip-8004#registration-v1",
  "image": "https://api.loopuman.com/logo.png",
  "active": true,
  "skills": [
    {"id": "task-routing", "name": "Task Routing", "description": "Routes microtasks to verified human workers via Telegram and WhatsApp"},
    {"id": "human-verification", "name": "Human Verification", "description": "Human-in-the-loop oracle for AI agent verification tasks"},
    {"id": "data-labeling", "name": "Data Labeling", "description": "Crowdsourced data labeling with quality checks and consensus"},
    {"id": "content-moderation", "name": "Content Moderation", "description": "Human content moderation with multi-reviewer consensus"},
    {"id": "survey-collection", "name": "Survey Collection", "description": "Distributed survey and feedback collection via messaging platforms"}
  ],
  "domains": [
    {"id": "ai-infrastructure", "name": "AI Infrastructure"},
    {"id": "gig-economy", "name": "Gig Economy"},
    {"id": "blockchain-payments", "name": "Blockchain Payments"}
  ],
  "version": "4.3.0",
  "license": "MIT",
  "sourceCode": "https://github.com/seesayearn-boop/humanoracle",
  "category": ["oracle,microtasking,human-in-the-loop"],
  "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.",
  "documentation": "https://github.com/seesayearn-boop/humanoracle/blob/main/docs/API.md",
  "website": "https://loopuman.com",
  "services": [
    {"name": "MCP", "endpoint": "https://api.loopuman.com/.well-known/mcp.json", "version": "2025-06-18",
     "skills": ["task-routing","human-verification","data-labeling","content-moderation","survey-collection"],
     "domains": ["ai-infrastructure","gig-economy","blockchain-payments"]},
    {"name": "A2A", "endpoint": "https://api.loopuman.com/.well-known/agent-card.json", "version": "0.3.0",
     "skills": ["task-routing","quality-control","workflow-orchestration"],
     "domains": ["ai-infrastructure","gig-economy"]},
    {"name": "OASF", "endpoint": "https://api.loopuman.com/.well-known/oasf.json", "version": "0.8.0"},
    {"name": "web", "endpoint": "https://loopuman.com"},
    {"name": "api", "endpoint": "https://api.loopuman.com"},
    {"name": "npm", "endpoint": "https://www.npmjs.com/package/loopuman"},
    {"name": "npm-mcp", "endpoint": "https://www.npmjs.com/package/loopuman-mcp"},
    {"name": "github", "endpoint": "https://github.com/seesayearn-boop/loopuman-backend"},
    {"name": "github-sdk", "endpoint": "https://github.com/seesayearn-boop/loopuman-mcp"},
    {"name": "list-workers", "endpoint": "https://api.loopuman.com/api/v1/workers"},
    {"name": "get-categories", "endpoint": "https://api.loopuman.com/api/v1/categories"},
    {"name": "email", "endpoint": "mailto:support@loopuman.com"},
    {"name": "twitter", "endpoint": "https://x.com/loopuman"},
    {"name": "openapi-spec", "endpoint": "https://api.loopuman.com/docs"},
    {"name": "telegram", "endpoint": "https://t.me/loopuman_bot"}
  ],
  "registrations": [{"agentId": 17, "agentRegistry": "eip155:42220:0x8004A169FB4a3325136EB29fA0ceB6D2e539a432"}],
  "supportedTrust": ["reputation","crypto-economic","tee-attestation"],
  "capabilities": {"streaming": false, "pushNotifications": true, "webhooks": true, "x402": true},
  "agentWallet": "0xeF4Acd50101C2afD14c2c213b30d8D8e9a91f0d2"
};

async function run() {
  const provider = new ethers.JsonRpcProvider('https://forno.celo.org');
  const wallet = new ethers.Wallet(walletData.privateKey, provider);
  console.log('Using wallet:', wallet.address);
  
  const contract = new ethers.Contract(REGISTRY, [
    'function setAgentURI(uint256 agentId, string calldata newURI) external',
    'function tokenURI(uint256 tokenId) view returns (string)'
  ], wallet);

  const json = JSON.stringify(metadata);
  const base64 = Buffer.from(json).toString('base64');
  const dataURI = 'data:application/json;base64,' + base64;

  console.log('JSON size:', json.length, 'bytes');
  console.log('Skills:', metadata.skills.length, '| Domains:', metadata.domains.length);
  console.log('sourceCode:', metadata.sourceCode);

  const tx = await contract.setAgentURI(AGENT_ID, dataURI);
  console.log('TX:', tx.hash);
  const receipt = await tx.wait();
  console.log('✅ Block:', receipt.blockNumber);
}

run().catch(e => console.error('❌', e.message.substring(0, 300)));
