const crypto = require('crypto');

function dedupeKey({ instrument_id, sample_id, result_time, results }) {
  const testCodes = (results || [])
    .map((r) => `${r.test_code}:${r.value}`)
    .sort()
    .join('|');
  const payload = `${instrument_id}|${sample_id}|${result_time}|${testCodes}`;
  return crypto.createHash('sha256').update(payload).digest('hex');
}

module.exports = { dedupeKey };
