/**
 * sat_audits.observed_at / start_time / end_time are TIMESTAMP. Clients often send time-only (HH:MM or HH:MM:SS).
 * Combine with the observation date; pass through values that already include a date.
 */
function combineObservedDateAndTime(observedAtRaw, timeRaw) {
  if (timeRaw == null || String(timeRaw).trim() === "") return null;
  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}`;
}

module.exports = { combineObservedDateAndTime };
