import db from '@/db/models';
import { cleanupExpiredRefreshTokens } from '@/services/refresh-token-maintenance';

/**
 * Operational maintenance command: delete refresh-token rows that expired before
 * the retention window (`AUTH_REFRESH_TOKEN_RETENTION_MS`, default 7 days). Run
 * on a schedule (cron / platform scheduler):
 *
 *   npm run db:cleanup-tokens          # dev (tsx)
 *   node dist/db/cleanup-refresh-tokens.js   # prod (built)
 */
async function run(): Promise<void> {
  const { deleted, cutoff } = await cleanupExpiredRefreshTokens();
  console.log(
    `Refresh-token cleanup complete: ${deleted} row(s) removed (cutoff ${cutoff.toISOString()}).`,
  );
  await db.sequelize.close();
}

run().catch((error) => {
  console.error('Refresh-token cleanup failed:', error);
  process.exit(1);
});
