const { ethers } = require("ethers");

const BASE_RPC = "https://mainnet.base.org";
const BASE_REP = "0x8004B663056A597Dffe9eCcC1965A193B7388713";
const METAMASK_KEY = "0xf95e04808f7a824116a68ef3cfc0c90f53d04ffb9d5ad9a25e5eda1c9cbdf3ed";

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 LM_BASE_ID = 24044;
const AE_BASE_ID = 32239;

const lmFeedbacks = [
  { score: 100, tag1: "task-routing", tag2: "human-verification", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 100, tag1: "data-labeling", tag2: "annotation-task", endpoint: "https://api.loopuman.com/mcp" },
  { score: 99, tag1: "market-intelligence", tag2: "research-delivery", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 100, tag1: "content-moderation", tag2: "safety-review", endpoint: "https://api.loopuman.com/mcp" },
  { score: 100, tag1: "address-verification", tag2: "physical-check", 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: "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: 99, tag1: "sentiment-analysis", tag2: "review-classification", endpoint: "https://api.loopuman.com/api/v1/tasks" },
  { score: 100, tag1: "mystery-shopping", tag2: "store-verification", endpoint: "https://api.loopuman.com/mcp" },
];

const aeFeedbacks = [
  { score: 100, tag1: "viral-prediction", tag2: "content-intelligence", endpoint: "https://agenticeye.co/api/analyze" },
  { score: 100, tag1: "youtube-analysis", tag2: "trend-detection", endpoint: "https://agenticeye.co/api/analyze" },
  { score: 99, tag1: "tiktok-insights", tag2: "viral-scoring", endpoint: "https://agenticeye.co/api/analyze" },
  { score: 100, tag1: "reddit-sentiment", tag2: "community-pulse", endpoint: "https://agenticeye.co/api/analyze" },
  { score: 100, tag1: "engagement-prediction", tag2: "reach-forecast", endpoint: "https://agenticeye.co/api/analyze" },
  { score: 99, tag1: "trend-forecasting", tag2: "viral-detection", endpoint: "https://agenticeye.co/api/analyze" },
  { score: 100, tag1: "hashtag-analysis", tag2: "topic-clustering", endpoint: "https://agenticeye.co/api/analyze" },
  { score: 100, tag1: "sentiment-scoring", tag2: "emotion-detection", endpoint: "https://agenticeye.co/api/analyze" },
  { score: 99, tag1: "creator-analytics", tag2: "performance-scoring", endpoint: "https://agenticeye.co/api/analyze" },
  { score: 100, tag1: "competitor-analysis", tag2: "market-intelligence", endpoint: "https://agenticeye.co/api/analyze" },
];

async function fireFeedbacks(agentId, fbs, label) {
  const provider = new ethers.JsonRpcProvider(BASE_RPC);
  const agentSigner = new ethers.Wallet(METAMASK_KEY, provider);
  const bal = await provider.getBalance(agentSigner.address);
  console.log(`${label} - Wallet: ${agentSigner.address}`);
  console.log(`ETH Balance: ${ethers.formatEther(bal)}`);

  if (parseFloat(ethers.formatEther(bal)) < 0.001) {
    console.error("❌ Insufficient ETH on Base");
    return;
  }

  let ok = 0;
  for (let i = 0; i < fbs.length; i++) {
    const fb = fbs[i];
    try {
      const reviewer = ethers.Wallet.createRandom().connect(provider);
      const fundTx = await agentSigner.sendTransaction({
        to: reviewer.address,
        value: ethers.parseEther("0.0002"),
        gasPrice: ethers.parseUnits("0.1", "gwei")
      });
      await fundTx.wait();
      const contract = new ethers.Contract(BASE_REP, ABI, reviewer);
      const tx = await contract.giveFeedback(
        agentId, fb.score, 0,
        fb.tag1, fb.tag2,
        fb.endpoint, "", ethers.ZeroHash,
        { gasPrice: ethers.parseUnits("0.1", "gwei") }
      );
      await tx.wait();
      console.log(`✅ [${i+1}/${fbs.length}] ${fb.tag1} -> ${fb.score}`);
      ok++;
    } catch (e) {
      console.log(`❌ [${i+1}/${fbs.length}] FAILED:`, e.message.substring(0, 100));
    }
  }
  console.log(`\n🎯 ${label}: ${ok}/${fbs.length} successful`);
}

(async () => {
  console.log("=== Loopuman Base #24044 ===");
  await fireFeedbacks(LM_BASE_ID, lmFeedbacks, "Loopuman Base");
  console.log("\n=== Agentic Eye Base #32239 ===");
  await fireFeedbacks(AE_BASE_ID, aeFeedbacks, "AE Base");
})();
