import { format, formatISO, parseISO } from 'date-fns';

/** Display local time as dd/MM/yyyy HH:mm; RFC3339 in tooltips. */
export function formatAuditLocal(isoUtc: string): string {
  try {
    const d = parseISO(isoUtc);
    return format(d, 'dd/MM/yyyy HH:mm');
  } catch {
    return isoUtc;
  }
}

/** Build RFC3339 in UTC from `datetime-local` value (YYYY-MM-DDTHH:mm interpreted as local). */
export function localDateTimeInputToUtcRfc3339(localValue: string): string | undefined {
  if (!localValue.trim()) return undefined;
  const d = new Date(localValue);
  if (Number.isNaN(d.getTime())) return undefined;
  return formatISO(d, { representation: 'complete' });
}
