const { ethers } = require('hardhat');

const DAOAddress = {
  localhost: '0x76Dfe9F47f06112a1b78960bf37d87CfbB6D6133',
  sepolia: '0x39B7522Ee1A5B13aE5580C40114239D4cE0e7D29',
};

let dao;
let account;
let validationPools;
let reputation;

const fetchReputation = async () => {
  reputation = await dao.balanceOf(account);
  console.log(`reputation: ${reputation}`);
};

const fetchValidationPool = async (poolIndex) => {
  const pool = await dao.validationPools(poolIndex);
  validationPools[poolIndex] = pool;
  return pool;
};

const fetchValidationPools = async () => {
  const count = await dao.validationPoolCount();
  console.log(`validation pool count: ${count}`);
  const promises = [];
  validationPools = [];
  for (let i = 0; i < count; i += 1) {
    promises.push(fetchValidationPool(i));
  }
  await Promise.all(promises);
};

const initialize = async () => {
  const network = process.env.HARDHAT_NETWORK;
  if (!DAOAddress[network]) {
    throw new Error(`network '${network}' is unknown`);
  }
  dao = await ethers.getContractAt('DAO', DAOAddress[network]);
  [account] = await ethers.getSigners();
  const address = await account.getAddress();
  console.log(`account: ${address}`);
  await fetchReputation();
  await fetchValidationPools();
};

const poolIsActive = (pool) => {
  if (new Date() >= new Date(Number(pool.endTime) * 1000)) return false;
  if (pool.resolved) return false;
  return true;
};

const stake = async (pool, amount) => {
  console.log(`staking ${amount} in favor of pool ${pool.id.toString()}`);
  await dao.stake(pool.id, amount, true);
  await fetchReputation();
};

const stakeEach = async (pools, amountPerPool) => {
  const promises = [];
  pools.forEach((pool) => {
    promises.push(stake(pool, amountPerPool));
  });
  await Promise.all(promises);
};

async function main() {
  await initialize();

  validationPools.forEach((pool) => {
    let status;
    if (poolIsActive(pool)) status = 'Active';
    else if (!pool.resolved) status = 'Ready to Evaluate';
    else if (pool.outcome) status = 'Accepted';
    else status = 'Rejected';
    console.log(`pool ${pool.id.toString()}, status: ${status}`);
  });

  // Stake half of available reputation on any active validation pools
  const activePools = validationPools.filter(poolIsActive);
  if (activePools.length && reputation > 0) {
    const amountPerPool = reputation / BigInt(2) / BigInt(activePools.length);
    await stakeEach(activePools, amountPerPool);
  }

  // Listen for new validation pools
  dao.on('ValidationPoolInitiated', async (poolIndex) => {
    console.log(`pool ${poolIndex} started`);
    await fetchValidationPool(poolIndex);
    await fetchReputation();

    // Stake half of available reputation on this validation pool
    const amount = reputation / BigInt(2);
    await stake(poolIndex, amount, true);
  });

  dao.on('ValidationPoolResolved', async (poolIndex, votePasses) => {
    console.log(`pool ${poolIndex} resolved, status: ${votePasses ? 'accepted' : 'rejected'}`);
    fetchValidationPool(poolIndex);
    fetchReputation();
  });
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});