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'
];

async function run() {
  const reviewer = ethers.Wallet.createRandom().connect(provider);
  console.log('🔑 Reviewer wallet:', reviewer.address);

  console.log('💰 Funding reviewer with 0.1 CELO...');
  const fundTx = await agentSigner.sendTransaction({
    to: reviewer.address,
    value: ethers.parseEther('0.1')
  });
  await fundTx.wait();
  console.log('✅ Funded!');

  const contract = new ethers.Contract(REPUTATION_REGISTRY, abi, reviewer);

  console.log('📝 Submitting feedback (92/100)...');
  const tx = await contract.giveFeedback(
    AGENT_ID,
    92,
    0,
    'starred',
    'reliable',
    'https://api.loopuman.com/api/v1/tasks/sync',
    '',
    ethers.ZeroHash
  );
  console.log('📋 TX:', tx.hash);
  const receipt = await tx.wait();
  console.log('✅ Feedback submitted! Block:', receipt.blockNumber);
  console.log('📊 Check: https://www.8004scan.io/agents/17');
}

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