import { eq, and, gt, asc, lt } from 'drizzle-orm';
import { db } from '../db';
import { chatSessionEvents } from '../schema';

/** Append one outbound event to a session's durable log; returns its global cursor id. */
export async function appendChatEvent(sessionId: string, event: unknown): Promise<number> {
  const [row] = await db
    .insert(chatSessionEvents)
    .values({ sessionId, event })
    .returning({ id: chatSessionEvents.id });
  return row!.id;
}

/** Replay everything a (re)connecting client missed: events for this session with id > cursor. */
export async function getChatEventsSince(
  sessionId: string,
  cursor: number,
): Promise<Array<{ id: number; event: unknown }>> {
  return db
    .select({ id: chatSessionEvents.id, event: chatSessionEvents.event })
    .from(chatSessionEvents)
    .where(and(eq(chatSessionEvents.sessionId, sessionId), gt(chatSessionEvents.id, cursor)))
    .orderBy(asc(chatSessionEvents.id));
}

/** Retention: drop events older than the cutoff (called periodically). */
export async function pruneChatEventsOlderThan(cutoff: Date): Promise<void> {
  await db.delete(chatSessionEvents).where(lt(chatSessionEvents.createdAt, cutoff));
}
