export const INVITE_REACTIVATION_WINDOW_DAYS = 7
export const INVITE_REACTIVATION_WINDOW_MS = INVITE_REACTIVATION_WINDOW_DAYS * 24 * 60 * 60 * 1000

function normalizeRevokedAt(value: Date | string | null | undefined) {
  if (!value) return null
  if (value instanceof Date) return value
  const parsed = new Date(value)
  return Number.isNaN(parsed.getTime()) ? null : parsed
}

export function isWithinInviteReactivationWindow(
  revokedAt: Date | string | null | undefined,
  reference: Date = new Date()
) {
  const timestamp = normalizeRevokedAt(revokedAt)
  if (!timestamp) return false
  return reference.getTime() - timestamp.getTime() <= INVITE_REACTIVATION_WINDOW_MS
}

export function canReactivateInvite(
  invite: { status: string | null | undefined; revokedAt: Date | string | null | undefined },
  reference: Date = new Date()
) {
  if ((invite.status ?? "").toLowerCase() !== "revoked") return false
  return isWithinInviteReactivationWindow(invite.revokedAt, reference)
}

