import { pgTable, bigserial, text, jsonb, timestamp, index } from 'drizzle-orm/pg-core';

// Durable per-session event log. Every outbound chat ServerMessage is appended here with a global
// monotonic `id` (the cursor). A (re)connecting client sends its last-seen cursor and the server
// replays everything since — so background task notifications and turn output that landed while the
// client was disconnected are never lost (the durability half of the turn/session decoupling).
export const chatSessionEvents = pgTable(
  'chat_session_events',
  {
    id: bigserial('id', { mode: 'number' }).primaryKey(),
    sessionId: text('session_id').notNull(),
    event: jsonb('event').notNull(),
    createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
  },
  (t) => [index('idx_chat_session_events_session_id').on(t.sessionId, t.id)],
);
