/**
 * Align with SAT_backend sat_audits TIMESTAMP columns: send date+time, not time-only.
 */
export function combineObservedDateAndTime(observedAtRaw, timeRaw) {
  if (timeRaw == null || String(timeRaw).trim() === '') return undefined;
  let t = String(timeRaw).trim();
  if (/^\d{4}-\d{2}-\d{2}/.test(t)) {
    t = t.replace('T', ' ');
    if (t.length === 10) return `${t} 00:00:00`;
    if (/^\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}$/.test(t)) return `${t}:00`;
    return t.slice(0, 19);
  }
  let datePart = null;
  if (observedAtRaw != null && String(observedAtRaw).trim() !== '') {
    const o = String(observedAtRaw).trim();
    if (o.length >= 10) datePart = o.slice(0, 10);
  }
  if (!datePart) datePart = new Date().toISOString().slice(0, 10);
  const timePart = t.length === 5 ? `${t}:00` : t;
  return `${datePart} ${timePart}`;
}
