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

const RPC_URL = "https://forno.celo.org";
const PRIVATE_KEY = "0x4115b51650f293b9d736485297ab1526d86e886f8e03440efb3cc77683152ba7";
const AGENT_ID = 17;
const REGISTRY_ADDRESS = "0x8004A169FB4a3325136EB29fA0ceB6D2e539a432";
const REGISTRY_ABI = ["function setAgentURI(uint256 tokenId, string calldata uri) external"];
const CURRENT_URI = "ipfs://Qmf4H5KKx46CrmxQha8GXWmsF4heAgzdLM4M9Nup66zN2c";

async function triggerMomentum() {
  try {
    console.log(`[${new Date().toISOString()}] Running momentum pulse...`);
    const provider = new ethers.JsonRpcProvider(RPC_URL);
    const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
    console.log(`Signing from: ${wallet.address}`);
    const registry = new ethers.Contract(REGISTRY_ADDRESS, REGISTRY_ABI, wallet);
    const tx = await registry.setAgentURI(AGENT_ID, CURRENT_URI);
    await tx.wait();
    console.log(`[${new Date().toISOString()}] ✅ Momentum tx: ${tx.hash}`);
  } catch (err) {
    console.error(`[${new Date().toISOString()}] ❌ Error:`, err.message);
  }
}

triggerMomentum();
setInterval(triggerMomentum, 4 * 60 * 60 * 1000);
console.log("✅ Momentum cron started - fires every 4h");
