import { db } from '@/lib/db'

// Cheap fingerprint of the visible thoughts set. Changes on any add (count),
// delete/archive (count), or edit/pin (updatedAt via @updatedAt). The client
// caches the full payload in IndexedDB and only re-downloads when this moves.
export async function thoughtsVersion(): Promise<string> {
  const agg = await db.thought.aggregate({
    where: { archived: false },
    _count: { _all: true },
    _max: { updatedAt: true },
  })
  return `${agg._count._all}:${agg._max.updatedAt?.toISOString() ?? '0'}`
}
