type VoiceReply = { id: string; content: string }
const normalized = (text: string) => text.replace(/\s+/g, ' ').trim()

/** Attach sources only to a unique, contiguous match of the verified answer. */
export function matchVoiceEvidence(messages: VoiceReply[], answer: string): string[] {
  const target = normalized(answer)
  if (!target) return []
  const matches: string[][] = []
  for (let start = 0; start < messages.length; start++) {
    let joined = ''
    for (let end = start; end < messages.length; end++) {
      joined = normalized(`${joined} ${messages[end].content}`)
      if (joined === target) matches.push(messages.slice(start, end + 1).map(message => message.id))
      if (joined.length >= target.length) break
    }
  }
  return matches.length === 1 ? matches[0] : []
}
