<?php
/**
 * Time To Pickup functionality for FoodOrd Pickup.
 *
 * @package FoodOrd
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Foodord_Time_To_Pickup
 */
class Foodord_Time_To_Pickup {

	/**
	 * Initialize the class.
	 */
	public function __construct() {
		add_action( 'admin_init', array( $this, 'register_settings' ) );
	}

	/**
	 * Register settings for time to pickup.
	 */
	public function register_settings() {
		register_setting(
			'foodord_pickup_hours',
			'foodord_time_to_pickup_enabled',
			array(
				'type'              => 'string',
				'sanitize_callback' => array( $this, 'sanitize_yes_no_option' ),
			)
		);
		register_setting(
			'foodord_pickup_hours',
			'foodord_time_to_pickup_breakdown',
			array(
				'type'              => 'string',
				'sanitize_callback' => array( $this, 'sanitize_time_to_pickup_breakdown_option' ),
			)
		);
	}

	/**
	 * Sanitize yes/no option.
	 *
	 * @param mixed $value Value to sanitize.
	 */
	public function sanitize_yes_no_option( $value ): string {
		return 'yes' === $value ? 'yes' : 'no';
	}

	/**
	 * Sanitize pickup breakdown option.
	 *
	 * @param mixed $value Value to sanitize.
	 */
	public function sanitize_time_to_pickup_breakdown_option( $value ): string {
		$allowed  = array( '5', '10', '15', '20', '30', '45', '60', '90' );
		$selected = sanitize_text_field( (string) $value );
		return in_array( $selected, $allowed, true ) ? $selected : '30';
	}

	/**
	 * Render the Time To Pickup container.
	 */
	public function render_time_to_pickup_container() {
		$time_to_pickup_enabled = 'yes' === get_option( 'foodord_time_to_pickup_enabled', 'no' );
		$breakdown_value        = get_option( 'foodord_time_to_pickup_breakdown', '30' );

		$scheduled_date_enabled = 'yes' === get_option( 'foodord_scheduled_date_enabled_pickup', 'no' );
		$max_days_ahead         = absint( get_option( 'foodord_scheduled_date_max_days_pickup', 7 ) );
		if ( $max_days_ahead < 1 || $max_days_ahead > 365 ) {
			$max_days_ahead = 7;
		}
		$scheduled_days_class = $scheduled_date_enabled ? '' : 'wrc-field--preorder-hidden';
		?>
		<div class="wrc-time-to-pickup-container">
			<div class="wrc-time-to-pickup-header">
				<h2 class="wrc-section-title"><?php esc_html_e( 'Time To Pickup', 'foodord' ); ?></h2>
			</div>
			
			<div class="wrc-time-to-pickup-content">
				<div class="wrc-time-pickup-grid">
					<div class="wrc-time-pickup-group">
						<div class="wrc-time-pickup-cell">
							<label class="wrc-field-label" for="wrc_time_to_pickup_enabled"><?php esc_html_e( 'Enable Time To Pickup', 'foodord' ); ?></label>
							<div class="wrc-control">
								<div class="foodord-toggle-row">
									<label class="foodord-toggle-switch" for="wrc_time_to_pickup_enabled">
										<input type="checkbox" class="wrc-input" id="wrc_time_to_pickup_enabled" name="wrc_time_to_pickup_enabled" value="1" <?php checked( $time_to_pickup_enabled, true ); ?> />
										<span class="foodord-toggle-slider"></span>
									</label>
									<span class="foodord-toggle-label"><?php esc_html_e( 'Show estimated pickup time to customers.', 'foodord' ); ?></span>
								</div>
							</div>
						</div>

						<div class="wrc-time-pickup-cell">
							<label class="wrc-field-label" for="wrc_time_to_pickup_breakdown"><?php esc_html_e( 'Break Down', 'foodord' ); ?></label>
							<div class="wrc-control">
								<select id="wrc_time_to_pickup_breakdown" name="wrc_time_to_pickup_breakdown" class="wrc-input">
									<option value="5" <?php selected( $breakdown_value, '5' ); ?>><?php esc_html_e( '5 minutes', 'foodord' ); ?></option>
									<option value="10" <?php selected( $breakdown_value, '10' ); ?>><?php esc_html_e( '10 minutes', 'foodord' ); ?></option>
									<option value="15" <?php selected( $breakdown_value, '15' ); ?>><?php esc_html_e( '15 minutes', 'foodord' ); ?></option>
									<option value="20" <?php selected( $breakdown_value, '20' ); ?>><?php esc_html_e( '20 minutes', 'foodord' ); ?></option>
									<option value="30" <?php selected( $breakdown_value, '30' ); ?>><?php esc_html_e( '30 minutes', 'foodord' ); ?></option>
									<option value="45" <?php selected( $breakdown_value, '45' ); ?>><?php esc_html_e( '45 minutes', 'foodord' ); ?></option>
									<option value="60" <?php selected( $breakdown_value, '60' ); ?>><?php esc_html_e( '60 minutes', 'foodord' ); ?></option>
									<option value="90" <?php selected( $breakdown_value, '90' ); ?>><?php esc_html_e( '90 minutes', 'foodord' ); ?></option>
								</select>
							</div>
						</div>
					</div>

					<div class="wrc-time-pickup-group">
						<div class="wrc-time-pickup-cell">
							<label class="wrc-field-label" for="wrc_scheduled_date_enabled_pickup"><?php esc_html_e( 'Enable scheduled dates', 'foodord' ); ?></label>
							<div class="wrc-control">
								<div class="foodord-toggle-row">
									<label class="foodord-toggle-switch" for="wrc_scheduled_date_enabled_pickup">
										<input type="checkbox" class="wrc-input" id="wrc_scheduled_date_enabled_pickup" name="wrc_scheduled_date_enabled_pickup" value="1" <?php checked( $scheduled_date_enabled, true ); ?> />
										<span class="foodord-toggle-slider"></span>
									</label>
									<span class="foodord-toggle-label"><?php esc_html_e( 'Allow customers to select a pickup date.', 'foodord' ); ?></span>
								</div>
							</div>
						</div>

						<div class="wrc-time-pickup-cell <?php echo esc_attr( $scheduled_days_class ); ?>" id="wrc_scheduled_date_max_days_wrapper_pickup">
							<label class="wrc-field-label" for="wrc_scheduled_date_max_days_pickup"><?php esc_html_e( 'Max days ahead', 'foodord' ); ?></label>
							<div class="wrc-control">
								<input type="number" id="wrc_scheduled_date_max_days_pickup" name="wrc_scheduled_date_max_days_pickup" class="wrc-input" value="<?php echo esc_attr( $max_days_ahead ); ?>" min="1" max="365" step="1" />
								<p class="wrc-help"><?php esc_html_e( 'Customers can schedule up to this many days ahead.', 'foodord' ); ?></p>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	}
}
