<?php

namespace Booktics\Services;

use DateTime;
use DateTimeZone;

/**
 * Handles timezone conversions and validations for date/time operations
 */
class Time_Zone_Handler {
    /**
     * Converts a local time to UTC
     *
     * @param string $time Time in format H:i:s
     * @param string $date Date in format Y-m-d
     * @param string $timezone Source timezone identifier
     *
     * @return array Array containing converted 'time' and 'date'
     */
    public static function convert_to_utc( string $time, string $date, string $timezone ) {
        // If date already contains timezone information, use it directly
        if (strpos($date, 'T') !== false) { 
            $date = substr($date, 0, strpos($date, 'T')); // Extract just the date part (before the 'T')
        }
        
        $datetime = new DateTime( "$date $time", new DateTimeZone( $timezone ) );
        $datetime->setTimezone( new DateTimeZone( 'UTC' ) );

        return array(
            'time' => $datetime->format( 'H:i:s' ),
            'date' => $datetime->format( 'Y-m-d' ),
        );
    }

    /**
     * Converts a UTC time to local timezone
     *
     * @param string $time Time in format H:i:s
     * @param string $date Date in format Y-m-d
     * @param string $timezone Target timezone identifier
     *
     * @return array Array containing converted 'time' and 'date'
     */
    /**
     * @return array
     */
    public static function convertFromUTC( string $time, string $date, string $timezone ) {
        $datetime = new DateTime( "$date $time", new DateTimeZone( 'UTC' ) );
        $datetime->setTimezone( new DateTimeZone( $timezone ) );

        return array(
            'time' => $datetime->format( 'H:i:s' ),
            'date' => $datetime->format( 'Y-m-d' ),
        );
    }

    /**
     * Validates if the provided timezone identifier is valid
     *
     * @param string $timezone Timezone identifier to validate
     *
     * @return bool True if timezone is valid, false otherwise
     */
    /**
     * @return bool
     */
    public static function validate_time_zone( string $timezone ) {
        return in_array( $timezone, DateTimeZone::listIdentifiers() );
    }

    /**
     * Handles timezone string to DateTimeZone object conversion
     * In case the timezone string is invalid, returns default timezone
     * If timezone is already a DateTimeZone object, returns it as it is
     *
     * @param string|DateTimeZone $timezone Timezone string
     *
     * @return DateTimeZone
     */
    public static function get_timezone_from_string( $timezone ) {
        // If timezone is already a DateTimeZone object, return as it is
        if ( $timezone instanceof DateTimeZone ) {
            return $timezone;
        }
        // Incase of invalid timezone string, return default timezone
        if ( ! is_string( $timezone ) ) {
            return wp_timezone();
        }

        // Validate and return a DateTimeZone object
        return self::validate_time_zone( $timezone ) ? new DateTimeZone($timezone) : wp_timezone();
    }
}
