/**
 * OneHourAgo - Utility function to check if a date is within the last hour
 *
 * Determines whether a given date/timestamp is within the last 60 minutes
 * from the current time. Useful for time-sensitive operations like:
 * - Checking if a token or session is still valid
 * - Determining if cached data is recent enough
 * - Validating time-limited operations
 *
 * @param date - Date to check (can be Date object, timestamp, or ISO string)
 * @returns true if the date is within the last hour, false otherwise
 *
 * @example
 * // Check if a token was issued within the last hour
 * const tokenIssuedAt = new Date('2024-01-15T10:30:00.000Z');
 * const isRecent = OneHourAgo(tokenIssuedAt); // true if current time is before 11:30
 *
 * @example
 * // Check timestamp (milliseconds since epoch)
 * const isRecent = OneHourAgo(1705314600000); // timestamp for 2024-01-15T10:30:00.000Z
 *
 * @note Uses current system time (Date.now())
 * @note One hour = 3,600,000 milliseconds (1000 × 60 × 60)
 */
export const OneHourAgo = (date: Date | number | string): boolean => {
  const hour = 1000 * 60 * 60; // Milliseconds in one hour
  const hourago = Date.now() - hour; // Timestamp one hour ago

  // Convert input to timestamp for comparison
  const dateTimestamp =
    typeof date === 'string'
      ? new Date(date).getTime()
      : date instanceof Date
        ? date.getTime()
        : date;

  // Check if date is after (more recent than) one hour ago
  return dateTimestamp > hourago;
};

/**
 * Array of month names (1-indexed)
 *
 * Provides full English month names with index 0 as empty string
 * to allow 1-based month indexing (January = months[1]).
 *
 * @note Index 0 is empty string to make array 1-indexed
 * @example months[1] // 'January'
 * @example months[12] // 'December'
 */
export const months = [
  '', // Index 0 - placeholder for 1-based indexing
  'January', // Index 1
  'February', // Index 2
  'March', // Index 3
  'April', // Index 4
  'May', // Index 5
  'June', // Index 6
  'July', // Index 7
  'August', // Index 8
  'September', // Index 9
  'October', // Index 10
  'November', // Index 11
  'December', // Index 12
];

/**
 * Gets month name in full or short format
 *
 * Returns the English name of a month based on its numeric value (1-12).
 * Supports both full names and 3-letter abbreviations.
 *
 * @param month - Month number (1 = January, 12 = December)
 * @param short - Whether to return short (3-letter) form (default: false)
 * @returns Month name string, or empty string for invalid month numbers
 *
 * @throws No error thrown - returns empty string for invalid input
 *
 * @example
 * // Full month names
 * getMonthShortName(1);      // 'January'
 * getMonthShortName(12);     // 'December'
 *
 * @example
 * // Short month names
 * getMonthShortName(1, true);  // 'Jan'
 * getMonthShortName(12, true); // 'Dec'
 *
 * @example
 * // Invalid input
 * getMonthShortName(0);       // ''
 * getMonthShortName(13);      // ''
 * getMonthShortName(NaN);     // ''
 */
export function getMonthShortName(
  month: number,
  short: boolean = false,
): string {
  // Validate month is within valid range (1-12)
  if (month < 1 || month > 12 || !Number.isInteger(month)) {
    return '';
  }

  // Get full month name from array (1-indexed)
  const full = months[month];

  // Return short form (first 3 letters) if requested
  return short ? full.substring(0, 3) : full;
}
