import dayjs from 'dayjs';
import init from '.';

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

const main = async () => {
  const engine = await init({
    mongoUrl: 'mongodb://127.0.0.1:27017/lucky-draw-pool-engine',
    debug: false,
    cleanUpPools: true,
  });

  await engine.createPool({
    poolKey: 'example',
    name: 'Example',
    initialAmount: 30,
    startTime: dayjs().toDate(),
    endTime: dayjs().add(3, 'second').toDate(),
    period: 'second',
    config: {
      minRate: 100,
      maxRate: 100,
    },
  });

  const pools = await engine.listPools();
  for (let i = 0; i < 15; i++) {
    const reward = await engine.draw({ poolKeys: ['example'] });
    console.log('reward: ', reward?.poolKey);
  }
  await sleep(1000);
  for (let i = 0; i < 15; i++) {
    const reward = await engine.draw({ poolKeys: ['example'] });
    console.log('reward: ', reward?.poolKey);
  }
  await sleep(1000);
  for (let i = 0; i < 15; i++) {
    const reward = await engine.draw({ poolKeys: ['example'] });
    console.log('reward: ', reward?.poolKey);
  }
};

main().catch(console.error);
