export function nowIso(): string {
  return new Date().toISOString()
}

export function addHours(baseIso: string | null | undefined, hours: number): string {
  const baseTime = baseIso ? new Date(baseIso).getTime() : Date.now()
  return new Date(baseTime + hours * 60 * 60 * 1000).toISOString()
}

export function normalizeTimestampIso(value: unknown, fallbackIso = nowIso()): string {
  if (value instanceof Date) {
    const timestamp = value.getTime()
    return Number.isFinite(timestamp) ? value.toISOString() : fallbackIso
  }

  const normalized = String(value || '').trim()
  if (!normalized) {
    return fallbackIso
  }

  const timestamp = Date.parse(normalized)
  return Number.isFinite(timestamp) ? new Date(timestamp).toISOString() : fallbackIso
}
