/**
 * Ethiopian Time Zone Utility Functions
 * 
 * Ethiopian Time (EAT) is UTC+3 with no daylight saving time.
 * This module provides functions to convert between Ethiopian time and UTC.
 */

const ETHIOPIAN_TIMEZONE_OFFSET = 3; // UTC+3

/**
 * Convert Ethiopian time to UTC
 * 
 * Takes a Date object representing Ethiopian time and returns the equivalent UTC time.
 * For example: 08:00 EAT becomes 05:00 UTC
 * 
 * @param ethiopianDate - Date object in Ethiopian time zone
 * @returns Date object in UTC
 */
export function ethiopianToUTC(ethiopianDate: Date): Date {
  if (!ethiopianDate || !(ethiopianDate instanceof Date)) {
    return new Date(NaN);
  }

  // Create a new date and subtract the Ethiopian offset
  const utcDate = new Date(ethiopianDate);
  utcDate.setHours(utcDate.getHours() - ETHIOPIAN_TIMEZONE_OFFSET);
  return utcDate;
}

/**
 * Convert UTC time to Ethiopian time
 * 
 * Takes a Date object representing UTC time and returns the equivalent Ethiopian time.
 * For example: 05:00 UTC becomes 08:00 EAT
 * 
 * @param utcDate - Date object in UTC
 * @returns Date object in Ethiopian time zone
 */
export function utcToEthiopian(utcDate: Date): Date {
  if (!utcDate || !(utcDate instanceof Date)) {
    return new Date(NaN);
  }

  // Create a new date and add the Ethiopian offset
  const ethiopianDate = new Date(utcDate);
  ethiopianDate.setHours(ethiopianDate.getHours() + ETHIOPIAN_TIMEZONE_OFFSET);
  return ethiopianDate;
}

/**
 * Get current time in Ethiopian time zone
 * 
 * @returns Current time as a Date object in Ethiopian time zone
 */
export function getCurrentEthiopianTime(): Date {
  return utcToEthiopian(new Date());
}

/**
 * Format time for display in Ethiopian time zone
 * 
 * Converts a UTC Date to Ethiopian time and formats it as HH:mm
 * 
 * @param date - Date object in UTC
 * @param format - Optional format string (currently only supports 'HH:mm')
 * @returns Formatted time string in Ethiopian time zone
 */
export function formatEthiopianTime(date: Date, format: string = 'HH:mm'): string {
  if (!date || !(date instanceof Date) || isNaN(date.getTime())) {
    return '';
  }

  const ethiopianDate = utcToEthiopian(date);
  const hours = ethiopianDate.getHours().toString().padStart(2, '0');
  const minutes = ethiopianDate.getMinutes().toString().padStart(2, '0');
  const result = `${hours}:${minutes}`;

  if (format === 'HH:mm') {
    return result;
  }

  return result;
}

/**
 * Format a UTC Date as the traditional Ethiopian 12-hour clock time.
 *
 * The Ethiopian clock starts the day at 6:00 EAT, so e.g. 20:43 EAT (08:43 PM)
 * is "2:43" on the Ethiopian clock. This matches what the Amharic date picker's
 * native <input type="time"> renders for the am-ET locale, so the read-only
 * field display stays consistent with the picker dropdown.
 *
 * The EAT wall-clock is derived from the instant via getUTCHours()+3 — the same
 * basis the picker's time input uses (see toEthiopianTimeInput in
 * AmharicDatePicker) — so the two agree in any browser timezone.
 *
 * @param date - Date object (its UTC instant)
 * @returns Ethiopian clock time as "H:mm" (e.g. "2:43"), or '' when invalid
 */
export function formatEthiopianClockTime(date: Date): string {
  if (!date || !(date instanceof Date) || isNaN(date.getTime())) {
    return '';
  }

  // EAT (UTC+3) wall-clock minutes since midnight.
  const totalMinutesUtc = date.getUTCHours() * 60 + date.getUTCMinutes();
  const eatMinutes =
    (totalMinutesUtc + ETHIOPIAN_TIMEZONE_OFFSET * 60 + 1440) % 1440;

  const eatHour = Math.floor(eatMinutes / 60);
  const minutes = eatMinutes % 60;

  // EAT 24h clock -> Ethiopian 12h clock (day starts at 6:00 EAT).
  const ethiopianHour = ((eatHour + 6) % 12) || 12;

  return `${ethiopianHour}:${minutes.toString().padStart(2, '0')}`;
}

/**
 * Parse time string in Ethiopian time zone
 * 
 * Takes a time string in HH:mm format and returns a Date object representing
 * that time in Ethiopian time zone (converted to UTC internally).
 * 
 * @param timeString - Time string in HH:mm format
 * @returns Date object representing the time in UTC
 */
export function parseEthiopianTime(timeString: string): Date {
  if (!timeString || typeof timeString !== 'string') {
    return new Date(NaN);
  }

  const [hours, minutes] = timeString.split(':').map(Number);

  if (isNaN(hours) || isNaN(minutes) || hours < 0 || hours > 23 || minutes < 0 || minutes > 59) {
    return new Date(NaN);
  }

  // Create a date in Ethiopian time
  const ethiopianDate = new Date();
  ethiopianDate.setHours(hours, minutes, 0, 0);

  // Convert to UTC
  return ethiopianToUTC(ethiopianDate);
}

/**
 * Get the Ethiopian time zone offset in hours
 * 
 * @returns The offset in hours (3 for UTC+3)
 */
export function getEthiopianTimeZoneOffset(): number {
  return ETHIOPIAN_TIMEZONE_OFFSET;
}

/**
 * Check if a date is valid
 * 
 * @param date - Date object to validate
 * @returns true if the date is valid, false otherwise
 */
export function isValidDate(date: Date): boolean {
  return date instanceof Date && !isNaN(date.getTime());
}
