<?php
/**
 * Time formatting utilities for converting between 24-hour and 12-hour formats
 */

class TimeFormatter {
    
    /**
     * Converts 24-hour time format to 12-hour AM/PM format
     * 
     * @param string $time24 Time in 24-hour format (e.g., "09:00", "13:30")
     * @return string Time in 12-hour format (e.g., "9:00 AM", "1:30 PM")
     */
    public static function convertTo12Hour($time24) {
        // Handle time range format (e.g., "09:00 - 10:00")
        if (strpos($time24, ' - ') !== false) {
            $parts = explode(' - ', $time24);
            $startTime = self::convertSingleTimeTo12Hour($parts[0]);
            $endTime = self::convertSingleTimeTo12Hour($parts[1]);
            return $startTime . ' - ' . $endTime;
        }
        
        // Handle single time format
        return self::convertSingleTimeTo12Hour($time24);
    }
    
    /**
     * Converts a single time string from 24-hour to 12-hour format
     * 
     * @param string $time24 Time in 24-hour format (e.g., "09:00")
     * @return string Time in 12-hour format (e.g., "9:00 AM")
     */
    private static function convertSingleTimeTo12Hour($time24) {
        try {
            $parts = explode(':', $time24);
            if (count($parts) != 2) {
                return $time24; // Return original if invalid format
            }
            
            $hour = (int)$parts[0];
            $minute = $parts[1];
            
            $period = 'AM';
            
            if ($hour == 0) {
                $hour = 12; // 00:xx -> 12:xx AM
            } elseif ($hour == 12) {
                $period = 'PM'; // 12:xx -> 12:xx PM
            } elseif ($hour > 12) {
                $hour = $hour - 12; // 13:xx -> 1:xx PM
                $period = 'PM';
            }
            
            return $hour . ':' . $minute . ' ' . $period;
        } catch (Exception $e) {
            return $time24; // Return original if parsing fails
        }
    }
    
    /**
     * Converts 12-hour time format back to 24-hour format
     * 
     * @param string $time12 Time in 12-hour format (e.g., "9:00 AM", "1:30 PM")
     * @return string Time in 24-hour format (e.g., "09:00", "13:30")
     */
    public static function convertTo24Hour($time12) {
        // Handle time range format (e.g., "9:00 AM - 10:00 AM")
        if (strpos($time12, ' - ') !== false) {
            $parts = explode(' - ', $time12);
            $startTime = self::convertSingleTimeTo24Hour($parts[0]);
            $endTime = self::convertSingleTimeTo24Hour($parts[1]);
            return $startTime . ' - ' . $endTime;
        }
        
        // Handle single time format
        return self::convertSingleTimeTo24Hour($time12);
    }
    
    /**
     * Converts a single time string from 12-hour to 24-hour format
     * 
     * @param string $time12 Time in 12-hour format (e.g., "9:00 AM")
     * @return string Time in 24-hour format (e.g., "09:00")
     */
    private static function convertSingleTimeTo24Hour($time12) {
        try {
            // Remove AM/PM and split time
            $timePart = preg_replace('/\s*(AM|PM)/i', '', $time12);
            $period = (stripos($time12, 'PM') !== false) ? 'PM' : 'AM';
            
            $parts = explode(':', $timePart);
            if (count($parts) != 2) {
                return $time12; // Return original if invalid format
            }
            
            $hour = (int)$parts[0];
            $minute = $parts[1];
            
            if ($period == 'AM') {
                if ($hour == 12) {
                    $hour = 0; // 12:xx AM -> 00:xx
                }
            } else { // PM
                if ($hour != 12) {
                    $hour = $hour + 12; // 1:xx PM -> 13:xx
                }
                // 12:xx PM stays 12:xx
            }
            
            return str_pad($hour, 2, '0', STR_PAD_LEFT) . ':' . $minute;
        } catch (Exception $e) {
            return $time12; // Return original if parsing fails
        }
    }
    
    /**
     * Converts an array of time slots to 12-hour format
     * 
     * @param array $timeSlots Array of time slots in 24-hour format
     * @return array Array of time slots in 12-hour format
     */
    public static function convertTimeSlotsTo12Hour($timeSlots) {
        return array_map([self::class, 'convertTo12Hour'], $timeSlots);
    }
}
?>
