export function convertTo24Hour(timeStr?: string | null): string {
  if (!timeStr) return "";

  // If it doesn't have AM/PM, just pad it if needed and return
  const hasAmPm = /am|pm/i.test(timeStr);
  if (!hasAmPm) {
    const parts = timeStr.split(":");
    if (parts.length >= 2) {
      return `${String(Number(parts[0])).padStart(2, "0")}:${String(Number(parts[1])).padStart(2, "0")}`;
    }
    return timeStr.trim();
  }

  // Handle AM/PM
  const match = timeStr.match(/(\d+):?(\d+)?\s*(am|pm)/i);
  if (!match) return timeStr.trim();

  let hours = Number(match[1]);
  const minutes = Number(match[2] || 0);
  const period = match[3].toUpperCase();

  if (period === "PM" && hours < 12) hours += 12;
  if (period === "AM" && hours === 12) hours = 0;

  return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}`;
}
