import {db} from '@db/instance';
import {Events} from '@db/schema';
import {eq, asc, desc} from 'drizzle-orm';

export const getOldestNotProcessedEventTs = async () => {
  const [event] = await db.instance
    .select({ts: Events.ts})
    .from(Events)
    .where(eq(Events.isProcessed, 0))
    .orderBy(asc(Events.ts))
    .limit(1);
  return event?.ts || null;
};

export const getMostRecentProcessedEventTs = async () => {
  const [event] = await db.instance
    .select({ts: Events.ts})
    .from(Events)
    .where(eq(Events.isProcessed, 1))
    .orderBy(desc(Events.ts))
    .limit(1);
  return event?.ts || null;
};

export const removeAllEvents = () => {
  return db.instance.delete(Events);
};
