/**
 * Timezone Utility for Ethiopian Railway
 * 
 * All dates/times in the system are stored and handled in Ethiopian Time (EAT - UTC+3).
 * This utility ensures consistent date handling across the application.
 * 
 * IMPORTANT: The application timezone is set to 'Africa/Addis_Ababa' in main.ts
 */

/**
 * Parse a date string or Date object ensuring it's treated as Ethiopian time (EAT - UTC+3)
 * 
 * @param dateInput - ISO string, date string, or Date object
 * @returns Date object in Ethiopian time
 * 
 * @example
 * parseEthiopianTime('2026-06-15T08:00:00') // Treats as 08:00 EAT, not UTC
 * parseEthiopianTime('2026-06-15') // Treats as midnight EAT
 */
export function parseEthiopianTime(dateInput: string | Date): Date {
  if (dateInput instanceof Date) return dateInput;
  // Already has timezone info (Z or +HH:MM) — parse directly as UTC
  if (/Z$|[+-]\d{2}:\d{2}$/.test(dateInput)) return new Date(dateInput);
  // Bare local string (e.g. "2026-07-23T21:00") — treat explicitly as EAT (UTC+3)
  return new Date(dateInput + '+03:00');
}

/**
 * Get the start of day (00:00:00) in Ethiopian time
 * 
 * @param date - Date object or date string
 * @returns Date object set to midnight EAT
 */
export function startOfDayEAT(date: Date | string): Date {
  const d = typeof date === 'string' ? new Date(date) : new Date(date);
  d.setHours(0, 0, 0, 0);
  return d;
}

/**
 * Get the end of day (23:59:59.999) in Ethiopian time
 * 
 * @param date - Date object or date string
 * @returns Date object set to end of day EAT
 */
export function endOfDayEAT(date: Date | string): Date {
  const d = typeof date === 'string' ? new Date(date) : new Date(date);
  d.setHours(23, 59, 59, 999);
  return d;
}

/**
 * Get the start of the next day in Ethiopian time
 * 
 * @param date - Date object or date string
 * @returns Date object set to midnight of next day EAT
 */
export function startOfNextDayEAT(date: Date | string): Date {
  const d = startOfDayEAT(date);
  d.setDate(d.getDate() + 1);
  return d;
}

/**
 * Format a date for display in Ethiopian time
 * 
 * @param date - Date object
 * @param options - Intl.DateTimeFormatOptions
 * @returns Formatted date string
 */
export function formatEthiopianTime(
  date: Date,
  options: Intl.DateTimeFormatOptions = {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    hour12: false,
  }
): string {
  return new Intl.DateTimeFormat('en-ET', {
    ...options,
    timeZone: 'Africa/Addis_Ababa',
  }).format(date);
}

/**
 * Add minutes to a date
 * 
 * @param date - Date object
 * @param minutes - Number of minutes to add
 * @returns New Date object
 */
export function addMinutes(date: Date, minutes: number): Date {
  return new Date(date.getTime() + minutes * 60_000);
}

/**
 * Add hours to a date
 * 
 * @param date - Date object
 * @param hours - Number of hours to add
 * @returns New Date object
 */
export function addHours(date: Date, hours: number): Date {
  return new Date(date.getTime() + hours * 60 * 60_000);
}

/**
 * Add days to a date
 * 
 * @param date - Date object
 * @param days - Number of days to add
 * @returns New Date object
 */
export function addDays(date: Date, days: number): Date {
  const result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}

/**
 * Check if two dates are on the same day (Ethiopian time)
 * 
 * @param date1 - First date
 * @param date2 - Second date
 * @returns true if both dates are on the same calendar day in EAT
 */
export function isSameDayEAT(date1: Date, date2: Date): boolean {
  return (
    date1.getFullYear() === date2.getFullYear() &&
    date1.getMonth() === date2.getMonth() &&
    date1.getDate() === date2.getDate()
  );
}
