import { CronStore } from "./store.ts";
import { CronScheduler, type CronCallback } from "./scheduler.ts";
import type { CronJob } from "./types.ts";

export * from "./types.ts";
export * from "./store.ts";
export * from "./scheduler.ts";

export interface CronSystem {
  store: CronStore;
  scheduler: CronScheduler;
  start: () => void;
  stop: () => void;
  refresh: () => Promise<void>;
}

/**
 * Create a complete cron system with store and scheduler
 */
export function createCronSystem(onTrigger: CronCallback, pollMs?: number): CronSystem {
  const store = new CronStore();
  let jobs: CronJob[] = [];
  const scheduler = new CronScheduler(jobs, onTrigger, pollMs);

  return {
    store,
    scheduler,

    start() {
      scheduler.start();
    },

    stop() {
      scheduler.stop();
    },

    async refresh() {
      jobs = await store.listJobs({ enabled: true });
      scheduler.updateJobs(jobs);
    },
  };
}
