import { hatchet } from '../hatchet-client';
import { simple } from './workflow';

async function main() {
  // > Create a Scheduled Run

  const runAt = new Date(new Date().setHours(12, 0, 0, 0) + 24 * 60 * 60 * 1000);

  const scheduled = await simple.schedule(runAt, {
    Message: 'hello',
  });

  // 👀 Get the scheduled run ID of the workflow
  // it may be helpful to store the scheduled run ID of the workflow
  // in a database or other persistent storage for later use
  const scheduledRunId = scheduled.metadata.id;
  console.log(scheduledRunId);

  // > Reschedule a Scheduled Run
  await hatchet.scheduled.update(scheduledRunId, {
    triggerAt: new Date(Date.now() + 60 * 60 * 1000),
  });

  // > Delete a Scheduled Run
  await hatchet.scheduled.delete(scheduled);

  // > List Scheduled Runs
  const scheduledRuns = await hatchet.scheduled.list({
    workflow: simple,
  });
  console.log(scheduledRuns);

  // > Bulk Delete Scheduled Runs
  await hatchet.scheduled.bulkDelete({
    scheduledRuns: [scheduledRunId],
  });

  // > Bulk Reschedule Scheduled Runs
  await hatchet.scheduled.bulkUpdate([
    { scheduledRun: scheduledRunId, triggerAt: new Date(Date.now() + 2 * 60 * 60 * 1000) },
  ]);
}

if (require.main === module) {
  main();
}
