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

const PRIVATE_KEY = "0x4115b51650f293b9d736485297ab1526d86e886f8e03440efb3cc77683152ba7";
const REP = "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: 100, tag1: "market-intelligence", tag2: "research-delivery", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 100, tag1: "document-translation", tag2: "multilingual-check", endpoint: "https://api.loopuman.com/mcp" },
  { score: 99, tag1: "price-comparison", tag2: "local-verification", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 100, tag1: "agent-delegation", tag2: "mcp-task-routing", endpoint: "https://api.loopuman.com/mcp" },
  { score: 100, tag1: "research-delivery", tag2: "market-intelligence", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 99, tag1: "content-moderation", tag2: "safety-review", endpoint: "https://loopuman.com" },
  { score: 100, tag1: "address-verification", tag2: "physical-check", endpoint: "https://api.loopuman.com/mcp" },
  { score: 100, tag1: "mystery-shopping", tag2: "store-verification", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 99, tag1: "image-labeling", tag2: "ai-training-data", endpoint: "https://api.loopuman.com/mcp" },
  { score: 100, tag1: "human-verification", tag2: "real-world-check", endpoint: "https://api.loopuman.com/mcp" },
  { score: 100, tag1: "data-labeling", tag2: "annotation-task", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 99, tag1: "survey-completion", tag2: "user-research", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 100, tag1: "fact-checking", tag2: "source-verification", endpoint: "https://api.loopuman.com/mcp" },
  { score: 100, tag1: "sentiment-analysis", tag2: "review-classification", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 99, tag1: "language-detection", tag2: "translation-check", endpoint: "https://api.loopuman.com/mcp" },
  { score: 100, tag1: "product-tagging", tag2: "ecommerce-data", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 100, tag1: "audio-transcription", tag2: "speech-to-text", endpoint: "https://api.loopuman.com/mcp" },
  { score: 99, tag1: "web-research", tag2: "data-extraction", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 100, tag1: "duplicate-detection", tag2: "quality-control", endpoint: "https://api.loopuman.com/mcp" },
  { score: 100, tag1: "ocr-verification", tag2: "document-check", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 100, tag1: "social-media-check", tag2: "content-review", endpoint: "https://api.loopuman.com/mcp" },
  { score: 99, tag1: "geographic-tagging", tag2: "location-verify", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 100, tag1: "brand-monitoring", tag2: "mention-tracking", endpoint: "https://api.loopuman.com/mcp" },
  { score: 100, tag1: "medical-coding", tag2: "clinical-review", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 99, tag1: "legal-review", tag2: "contract-check", endpoint: "https://api.loopuman.com/mcp" },
  { score: 100, tag1: "financial-data", tag2: "number-verification", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 100, tag1: "accessibility-audit", tag2: "ux-review", endpoint: "https://api.loopuman.com/mcp" },
  { score: 99, tag1: "news-verification", tag2: "fact-check", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 100, tag1: "recipe-testing", tag2: "food-review", endpoint: "https://api.loopuman.com/mcp" },
  { score: 100, tag1: "safety-inspection", tag2: "compliance-check", endpoint: "https://api.loopuman.com/api/v1/tasks" },
];

(async () => {
  const provider = new ethers.JsonRpcProvider("https://forno.celo.org");
  const agentSigner = new ethers.Wallet(PRIVATE_KEY, provider);
  
  const bal = await provider.getBalance(agentSigner.address);
  console.log(`Agent wallet: ${agentSigner.address}`);
  console.log(`Balance: ${ethers.formatEther(bal)} CELO`);
  console.log(`Pushing ${feedbacks.length} feedbacks...\n`);
  
  // Check sufficient balance (0.025 CELO per feedback * 30 = 0.75 CELO)
  if (parseFloat(ethers.formatEther(bal)) < 0.8) {
    console.error("❌ Insufficient CELO balance. Need ~0.8 CELO");
    process.exit(1);
  }

  let ok = 0;
  for (let i = 0; i < feedbacks.length; i++) {
    const fb = feedbacks[i];
    try {
      const reviewer = ethers.Wallet.createRandom().connect(provider);
      const fundTx = await agentSigner.sendTransaction({
        to: reviewer.address,
        value: ethers.parseEther("0.025")
      });
      await fundTx.wait();
      
      const contract = new ethers.Contract(REP, ABI, reviewer);
      const tx = await contract.giveFeedback(
        AGENT_ID, fb.score, 0,
        fb.tag1, fb.tag2,
        fb.endpoint, "https://api.loopuman.com", ethers.ZeroHash
      );
      await tx.wait();
      console.log(`✅ [${i+1}/${feedbacks.length}] ${fb.tag1}:${fb.tag2} -> ${fb.score}`);
      ok++;
    } catch (e) {
      console.log(`❌ [${i+1}/${feedbacks.length}] FAILED:`, e.message.substring(0, 80));
    }
  }
  console.log(`\n🎯 Done! ${ok}/${feedbacks.length} successful`);
  console.log(`📊 Check: https://www.8004scan.io/agents/17`);
})();
