import cron, { type ScheduledTask } from "node-cron";
import { backfillAccountProfiles } from "./account-enrichment-service";
import { runScheduledCollections } from "./collection-service";
import { withDatabaseLock } from "./database";
import {
  resolveCollectionMcpConfig,
  type CollectionMcpBindings,
} from "./mcp-collection-client";
import { ensureSchema, getRawDb } from "./mvp-db";
import { getRuntimeEnv, isEnabled } from "./runtime-env";

declare global {
  var __kocLoopScheduler: ScheduledTask | undefined;
}

async function runDailyJob() {
  await withDatabaseLock("koc-loop-daily-collection", 0, async () => {
    await ensureSchema();
    const db = getRawDb();
    const config = resolveCollectionMcpConfig(
      getRuntimeEnv() as unknown as CollectionMcpBindings,
    );
    const collections = await runScheduledCollections(db, Date.now(), config);
    const accounts = await backfillAccountProfiles(db, config, 10);
    console.info("[KOC LOOP] daily scheduler completed", {
      collections,
      accounts,
    });
  });
}

export function startScheduler() {
  if (!isEnabled(getRuntimeEnv().ENABLE_SCHEDULER, true)) return;
  if (globalThis.__kocLoopScheduler) return;
  globalThis.__kocLoopScheduler = cron.schedule(
    "0 9 * * *",
    () => void runDailyJob().catch((error) => {
      console.error("[KOC LOOP] daily scheduler failed", error);
    }),
    { timezone: "Asia/Shanghai", noOverlap: true },
  );
  console.info("[KOC LOOP] scheduler enabled at 09:00 Asia/Shanghai");
}
