/**
 * Cron: Email Verifications Sweep — runs daily at 00:00 UTC.
 *
 * Deletes email_verifications rows whose expires_at is more than 7 days in
 * the past. The 7-day grace period preserves a short audit trail before GC.
 * Without this sweep the table grows unboundedly as unverified signup and
 * change-email tokens accumulate. Bundled into the 0 0 * * * midnight bucket
 * alongside other low-frequency daily cleanup jobs.
 */

import { createDbService } from '@/server/services/db.js';
import { withSentry } from '@/server/observability/with-sentry';
import { purgeExpired } from '../db/queries/email_verifications.js';
import type { CronEnv } from './deal-expiry.js';

export const runEmailVerificationsSweep = withSentry(
  async function runEmailVerificationsSweep(env: CronEnv): Promise<{ deleted: number }> {
    const db = env.db ?? createDbService({ DATABASE_URL: env.DATABASE_URL });
    const deleted = await purgeExpired(db);
    return { deleted };
  },
  { name: 'cron.email-verifications-sweep', kind: 'cron' },
);
