// apps/web/src/server/cron/maturation-sweep.ts
/**
 * Maturation sweep cron runner.
 *
 * Wired into the 30-min cron bucket via dispatchCron.
 * Also callable via POST /api/internal/maturation/sweep (Bearer CRON_SECRET).
 */

import { createDbService } from '@/server/services/db.js';
import { withSentry } from '@/server/observability/with-sentry';
import { captureCaught } from '@/server/observability/capture.server';
import { sweepMaturation } from '../referrals/maturation.js';
import type { CronEnv } from './deal-expiry.js';

export const runMaturationSweep = withSentry(
  async (env: CronEnv): Promise<void> => {
    try {
      const db = env.db ?? createDbService({ DATABASE_URL: env.DATABASE_URL });
      await sweepMaturation(db);
    } catch (err) {
      captureCaught(err, { scope: 'server.cron.maturation-sweep', severity: 'error' });
      throw err;
    }
  },
  { name: 'maturation-sweep', kind: 'cron' },
);
