import type { Prisma } from '@prisma/client'

// A checklist item checked off more than this long ago is hidden from the active
// list and excluded from its item count. It stays in the DB (un-checking is still
// possible until it ages out) — this only governs visibility.
export const DONE_TTL_MS = 24 * 60 * 60 * 1000

/**
 * Prisma `where` for the checklist items an active list should show: everything
 * except items that have been done for more than 24h. An item shows when it is
 * not done, OR has no completion time (legacy rows), OR was completed within the
 * window. Null-safe by construction (uses gte rather than negating a `lt`).
 */
export function visibleChecklistItemsWhere(now: Date = new Date()): Prisma.ChecklistItemWhereInput {
  const cutoff = new Date(now.getTime() - DONE_TTL_MS)
  return {
    OR: [
      { done: false },
      { completedAt: null },
      { completedAt: { gte: cutoff } },
    ],
  }
}
