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

const agentWallet = JSON.parse(fs.readFileSync(path.join(__dirname, '.selfclaw-wallet.json'), 'utf8'));
const provider = new ethers.JsonRpcProvider('https://forno.celo.org');
const agentSigner = new ethers.Wallet(agentWallet.privateKey, provider);

const REPUTATION_REGISTRY = '0x8004BAa17C55a88189AE136b182e5fdA19dE9b63';
const AGENT_ID = 17;

const abi = [
  'function giveFeedback(uint256 agentId, int128 value, uint8 valueDecimals, string calldata tag1, string calldata tag2, string calldata endpoint, string calldata feedbackURI, bytes32 feedbackHash) external'
];

const feedbacks = [
  { score: 90, tag1: 'starred', tag2: 'fast', endpoint: 'https://api.loopuman.com/api/v1/tasks/sync' },
  { score: 95, tag1: 'starred', tag2: 'accurate', endpoint: 'https://api.loopuman.com/api/v1/tasks/bulk' },
  { score: 88, tag1: 'starred', tag2: 'human-verified', endpoint: 'https://api.loopuman.com/api/v1/voice-task' },
  { score: 93, tag1: 'starred', tag2: 'responsive', endpoint: 'https://api.loopuman.com/api/v1/tasks/sync' },
];

async function run() {
  for (let i = 0; i < feedbacks.length; i++) {
    const fb = feedbacks[i];
    const reviewer = ethers.Wallet.createRandom().connect(provider);
    console.log(`\n[${i+1}/${feedbacks.length}] Reviewer: ${reviewer.address}`);

    console.log('  💰 Funding...');
    const fundTx = await agentSigner.sendTransaction({
      to: reviewer.address,
      value: ethers.parseEther('0.05')
    });
    await fundTx.wait();

    const contract = new ethers.Contract(REPUTATION_REGISTRY, abi, reviewer);
    console.log(`  📝 Submitting ${fb.score}/100...`);
    const tx = await contract.giveFeedback(
      AGENT_ID, fb.score, 0, fb.tag1, fb.tag2, fb.endpoint, '', ethers.ZeroHash
    );
    const receipt = await tx.wait();
    console.log(`  ✅ Done! Block: ${receipt.blockNumber}`);
  }
  console.log('\n🎉 All feedback submitted! Total: 5 reviews (including earlier one)');
  console.log('📊 Check: https://www.8004scan.io/agents/17');
}

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