// This module provides functions to format date and time in various ways.
// It includes functions to transform date-time fields in records, get the current year and month, and format date-time strings.
const formatDateTime = (date) => {
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, "0");
  const day = date.getDate().toString().padStart(2, "0");
  const hours = date.getHours().toString().padStart(2, "0");
  const minutes = date.getMinutes().toString().padStart(2, "0");
  const seconds = date.getSeconds().toString().padStart(2, "0");
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};

const transformDateTimeField = (records) => {
  return records.map((record) => {
    switch (true) {
      case record.date_time !== undefined:
        if (record.date_time == undefined || record.date_time == null) break;
        record.date_time = formatDateTime(record.date_time);

      case record.created_at !== undefined:
        if (record.created_at == undefined || record.created_at == null) break;

        record.created_at = formatDateTime(record.created_at);

      case record.date_of_birth !== undefined:
        if (record.date_of_birth == undefined || record.date_of_birth == null)
          break;

        record.date_of_birth = formatDateTime(record.date_of_birth);

      case record.rx_time !== undefined:
        if (record.rx_time == undefined || record.rx_time == null) break;
        record.rx_time = formatDateTime(record.rx_time);
        break;
      default:
        // No specific action needed if none of the conditions are met
        break;
    }
    return record;
  });
};

const getCurrentYearMonth = () => {
  const now = new Date(); // Get the current date
  const year = now.getFullYear(); // Get the 4-digit year
  const month = String(now.getMonth() + 1).padStart(2, "0"); // Get the 2-digit month (0-based, so add 1)
  return `${year}-${month}`; // Format as YYYY-MM
};

const getFormattedDateTime = () => {
  const now = new Date(); // Get the current date and time

  // Extract date components
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, "0"); // Month is 0-based
  const date = String(now.getDate()).padStart(2, "0");

  return `${year}-${month}-${date}`;
};

function getFormattedDateTime12h() {
  const now = new Date();

  const day = String(now.getDate()).padStart(2, "0");
  const month = String(now.getMonth() + 1).padStart(2, "0"); // Month is 0-indexed
  const year = now.getFullYear();

  let hours = now.getHours() % 12; // Convert to 12-hour format
  hours = hours ? hours : 12; // Handle midnight (0 % 12 = 0)
  hours = String(hours).padStart(2, "0");

  const minutes = String(now.getMinutes()).padStart(2, "0");
  const seconds = String(now.getSeconds()).padStart(2, "0");

  const ampm = now.getHours() >= 12 ? "PM" : "AM";

  return `${day}/${month}/${year} ${hours}:${minutes}:${seconds} ${ampm}`;
}

// const formattedDateTime = getFormattedDateTime12h();
// console.log(formattedDateTime); // Output: e.g., 25/10/2023 03:30:45 PM

module.exports = {
  formatDateTime,
  transformDateTimeField,
  getCurrentYearMonth,
  getFormattedDateTime,
  getFormattedDateTime12h,
};
