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

defined( 'ABSPATH' ) || exit;

/**
 * Class Foodord_Pickup
 */
class Foodord_Pickup {
	/**
	 * Retrieve possible admin page slugs for Pickup settings.
	 *
	 * @return string[]
	 */
	private function get_page_slugs(): array {
		return function_exists( 'foodord_get_settings_page_slugs' )
			? foodord_get_settings_page_slugs( 'pickup' )
			: array( 'foodord-theme-settings-pickup', 'foodord-pickup' );
	}

	/**
	 * Build the admin URL for the Pickup settings page.
	 */
	private function get_page_url(): string {
		return function_exists( 'foodord_get_admin_page_url' )
			? foodord_get_admin_page_url( 'pickup' )
			: admin_url( 'admin.php?page=foodord-pickup' );
	}
	/**
	 * Check if license is active.
	 */
	private function is_license_active(): bool {
		return true;
	}

	/**
	 * Time To Pickup instance.
	 *
	 * @var Foodord_Time_To_Pickup
	 */
	private $time_to_pickup;

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

		// Initialize Time To Pickup module.
		require_once plugin_dir_path( __FILE__ ) . 'class-foodord-time-to-pickup.php';
		$this->time_to_pickup = new Foodord_Time_To_Pickup();
	}

	/**
	 * Register settings for pickup hours.
	 */
	public function register_settings() {
		if ( ! $this->is_license_active() ) {
			return;
		}
		register_setting(
			'foodord_pickup_hours',
			'foodord_pickup_opening_hours',
			array(
				'type'              => 'array',
				'sanitize_callback' => array( $this, 'sanitize_pickup_opening_hours_option' ),
			)
		);
		register_setting(
			'foodord_pickup_hours',
			'foodord_pickup_custom_hours',
			array(
				'type'              => 'array',
				'sanitize_callback' => array( $this, 'sanitize_pickup_custom_hours_option' ),
			)
		);
		register_setting(
			'foodord_pickup_hours',
			'foodord_preorder_enabled_pickup',
			array(
				'type'              => 'string',
				'sanitize_callback' => array( $this, 'sanitize_yes_no_option' ),
			)
		);
		register_setting(
			'foodord_pickup_hours',
			'foodord_preorder_time_pickup',
			array(
				'type'              => 'string',
				'sanitize_callback' => array( $this, 'sanitize_time_option' ),
			)
		);
		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' ),
			)
		);
		register_setting(
			'foodord_pickup_hours',
			'foodord_average_pickup_time',
			array(
				'type'              => 'integer',
				'sanitize_callback' => array( $this, 'sanitize_average_pickup_time_option' ),
			)
		);
		register_setting(
			'foodord_pickup_hours',
			'foodord_scheduled_date_enabled_pickup',
			array(
				'type'              => 'string',
				'sanitize_callback' => array( $this, 'sanitize_yes_no_option' ),
			)
		);
		register_setting(
			'foodord_pickup_hours',
			'foodord_scheduled_date_max_days_pickup',
			array(
				'type'              => 'integer',
				'sanitize_callback' => array( $this, 'sanitize_scheduled_date_max_days_option' ),
			)
		);
	}

	/**
	 * Enqueue admin styles.
	 *
	 * @param string $hook Current admin page hook.
	 */
	public function enqueue_admin_styles( $hook ) {
		if ( ! $this->is_license_active() ) {
			return;
		}

		$matches_page = false;
		foreach ( $this->get_page_slugs() as $slug ) {
			if ( '' !== $slug && false !== strpos( (string) $hook, $slug ) ) {
				$matches_page = true;
				break;
			}
		}

		if ( ! $matches_page ) {
			return;
		}

		wp_enqueue_style(
			'wrc-admin-styles',
			plugin_dir_url( __FILE__ ) . 'css/wrc-admin.css',
			array(),
			FOODORD_VERSION
		);
		wp_enqueue_script(
			'wrc-pickup-admin-js',
			plugin_dir_url( __FILE__ ) . 'js/foodord-pickup.js',
			array( 'jquery' ),
			FOODORD_VERSION,
			true
		);
	}


	/**
	 * Handle form submission.
	 */
	public function handle_form_submission() {
		$nonce = isset( $_POST['wrc_save_pickup_hours_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['wrc_save_pickup_hours_nonce'] ) ) : '';
		if ( '' === $nonce || ! wp_verify_nonce( $nonce, 'wrc_save_pickup_hours' ) ) {
			return;
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( ! $this->is_license_active() ) {
			wp_die( esc_html__( 'Pickup settings require an active FoodOrd license.', 'foodord' ) );
		}

		$hours = array();
		$days  = array( 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday' );

		foreach ( $days as $day ) {
			$closed = isset( $_POST[ $day . '_closed' ] );
			if ( $closed ) {
				$hours[ $day ] = array( 'closed' => true );
			} else {
				$morning_start = isset( $_POST[ $day . '_morning_start' ] ) ? sanitize_text_field( wp_unslash( $_POST[ $day . '_morning_start' ] ) ) : '';
				$morning_end   = isset( $_POST[ $day . '_morning_end' ] ) ? sanitize_text_field( wp_unslash( $_POST[ $day . '_morning_end' ] ) ) : '';
				$evening_start = isset( $_POST[ $day . '_evening_start' ] ) ? sanitize_text_field( wp_unslash( $_POST[ $day . '_evening_start' ] ) ) : '';
				$evening_end   = isset( $_POST[ $day . '_evening_end' ] ) ? sanitize_text_field( wp_unslash( $_POST[ $day . '_evening_end' ] ) ) : '';

				$hours[ $day ] = array(
					'closed'        => false,
					'morning_start' => $morning_start,
					'morning_end'   => $morning_end,
					'evening_start' => $evening_start,
					'evening_end'   => $evening_end,
				);
			}
		}

		update_option( 'foodord_pickup_opening_hours', $hours );

		// Handle custom hours.
		$custom_hours = array();
		if ( isset( $_POST['wrc_pickup_custom_hours'] ) && is_string( $_POST['wrc_pickup_custom_hours'] ) ) {
			$custom_hours_data = json_decode( sanitize_textarea_field( wp_unslash( $_POST['wrc_pickup_custom_hours'] ) ), true );
			if ( is_array( $custom_hours_data ) ) {
				foreach ( $custom_hours_data as $custom_hour ) {
					if ( is_array( $custom_hour ) ) {
						$date_range = $this->normalize_custom_hours_date_range( $custom_hour );
						if ( null === $date_range ) {
							continue;
						}
						$custom_hours[] = array(
							'date'          => $date_range['start_date'],
							'start_date'    => $date_range['start_date'],
							'end_date'      => $date_range['end_date'],
							'description'   => sanitize_text_field( $custom_hour['description'] ?? '' ),
							'closed'        => (bool) ( $custom_hour['closed'] ?? false ),
							'morning_start' => sanitize_text_field( $custom_hour['morningStart'] ?? '' ),
							'morning_end'   => sanitize_text_field( $custom_hour['morningEnd'] ?? '' ),
							'evening_start' => sanitize_text_field( $custom_hour['eveningStart'] ?? '' ),
							'evening_end'   => sanitize_text_field( $custom_hour['eveningEnd'] ?? '' ),
						);
					}
				}
			}
		}
		update_option( 'foodord_pickup_custom_hours', $custom_hours );

		// Handle pickup preorder settings.
		$preorder_enabled = isset( $_POST['wrc_preorder_enabled_pickup'] );
		$preorder_time    = '';
		if ( $preorder_enabled && isset( $_POST['wrc_preorder_time_pickup'] ) ) {
			$raw_preorder_time = sanitize_text_field( wp_unslash( $_POST['wrc_preorder_time_pickup'] ) );
			if ( preg_match( '/^\d{2}:\d{2}$/', $raw_preorder_time ) ) {
				$preorder_time = $raw_preorder_time;
			}
		}
		update_option( 'foodord_preorder_enabled_pickup', $preorder_enabled ? 'yes' : 'no' );
		update_option( 'foodord_preorder_time_pickup', $preorder_time );

		// Handle time to pickup settings.
		$time_to_pickup_enabled = isset( $_POST['wrc_time_to_pickup_enabled'] );
		update_option( 'foodord_time_to_pickup_enabled', $time_to_pickup_enabled ? 'yes' : 'no' );

		// Handle scheduled date settings.
		$scheduled_date_enabled = isset( $_POST['wrc_scheduled_date_enabled_pickup'] );
		update_option( 'foodord_scheduled_date_enabled_pickup', $scheduled_date_enabled ? 'yes' : 'no' );
		$max_days_ahead = 7;
		if ( isset( $_POST['wrc_scheduled_date_max_days_pickup'] ) ) {
			$max_days_ahead = absint( $_POST['wrc_scheduled_date_max_days_pickup'] );
		}
		if ( $max_days_ahead < 1 || $max_days_ahead > 365 ) {
			$max_days_ahead = 7;
		}
		update_option( 'foodord_scheduled_date_max_days_pickup', $max_days_ahead );

		// Handle time to pickup breakdown setting.
		$breakdown_value = isset( $_POST['wrc_time_to_pickup_breakdown'] ) ? sanitize_text_field( wp_unslash( $_POST['wrc_time_to_pickup_breakdown'] ) ) : '30';
		$allowed_values  = array( '5', '10', '15', '20', '30', '45', '60', '90' );
		if ( in_array( $breakdown_value, $allowed_values, true ) ) {
			update_option( 'foodord_time_to_pickup_breakdown', $breakdown_value );
		}

		// Handle average pickup time setting.
		$average_pickup_time = isset( $_POST['wrc_average_pickup_time'] ) ? absint( $_POST['wrc_average_pickup_time'] ) : 20;
		if ( $average_pickup_time > 240 ) {
			$average_pickup_time = 20; // Default fallback.
		}
		update_option( 'foodord_average_pickup_time', $average_pickup_time );

		// Set success message transient for display after redirect.
		set_transient( 'foodord_pickup_settings_saved', true, 45 );

		// Redirect to avoid resubmission.
		wp_safe_redirect( $this->get_page_url() );
		exit;
	}

	/**
	 * Get saved pickup opening hours.
	 *
	 * @return array Saved hours or default values.
	 */
	private function get_pickup_opening_hours() {
		$default_hours = array(
			'monday'    => array(
				'closed'        => false,
				'morning_start' => '10:00',
				'morning_end'   => '14:00',
				'evening_start' => '16:00',
				'evening_end'   => '22:00',
			),
			'tuesday'   => array(
				'closed'        => false,
				'morning_start' => '10:00',
				'morning_end'   => '14:00',
				'evening_start' => '16:00',
				'evening_end'   => '22:00',
			),
			'wednesday' => array(
				'closed'        => false,
				'morning_start' => '10:00',
				'morning_end'   => '14:00',
				'evening_start' => '16:00',
				'evening_end'   => '22:00',
			),
			'thursday'  => array(
				'closed'        => false,
				'morning_start' => '10:00',
				'morning_end'   => '14:00',
				'evening_start' => '16:00',
				'evening_end'   => '22:00',
			),
			'friday'    => array(
				'closed'        => false,
				'morning_start' => '10:00',
				'morning_end'   => '14:00',
				'evening_start' => '16:00',
				'evening_end'   => '22:00',
			),
			'saturday'  => array(
				'closed'        => false,
				'morning_start' => '10:00',
				'morning_end'   => '14:00',
				'evening_start' => '16:00',
				'evening_end'   => '22:00',
			),
			'sunday'    => array(
				'closed'        => true,
				'morning_start' => '',
				'morning_end'   => '',
				'evening_start' => '',
				'evening_end'   => '',
			),
		);

		return get_option( 'foodord_pickup_opening_hours', $default_hours );
	}

	/**
	 * Get saved pickup custom hours.
	 *
	 * @return array Saved custom hours or empty array.
	 */
	private function get_pickup_custom_hours() {
		return get_option( 'foodord_pickup_custom_hours', array() );
	}

	/**
	 * Get day names.
	 *
	 * @return array Day names.
	 */
	private function get_day_names() {
		return array(
			'monday'    => esc_html__( 'Monday', 'foodord' ),
			'tuesday'   => esc_html__( 'Tuesday', 'foodord' ),
			'wednesday' => esc_html__( 'Wednesday', 'foodord' ),
			'thursday'  => esc_html__( 'Thursday', 'foodord' ),
			'friday'    => esc_html__( 'Friday', 'foodord' ),
			'saturday'  => esc_html__( 'Saturday', 'foodord' ),
			'sunday'    => esc_html__( 'Sunday', 'foodord' ),
		);
	}

	/**
	 * Render the Pickup settings page.
	 */
	public function render_pickup_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'foodord' ) );
		}

		if ( ! $this->is_license_active() ) {
			$license_url = function_exists( 'foodord_get_admin_page_url' )
				? foodord_get_admin_page_url( 'license' )
				: admin_url( 'admin.php?page=foodord-license' );
			wp_safe_redirect( $license_url );
			exit;
		}

		// Handle form submission.
		$request_method = isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) : '';
		if ( 'POST' === $request_method ) {
			$nonce = isset( $_POST['wrc_save_pickup_hours_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['wrc_save_pickup_hours_nonce'] ) ) : '';
			if ( '' !== $nonce && wp_verify_nonce( $nonce, 'wrc_save_pickup_hours' ) ) {
				$this->handle_form_submission();
			}
		}

		$hours               = $this->get_pickup_opening_hours();
		$custom_hours        = $this->get_pickup_custom_hours();
		$day_names           = $this->get_day_names();
		$preorder_enabled    = 'yes' === get_option( 'foodord_preorder_enabled_pickup', 'no' );
		$preorder_time       = get_option( 'foodord_preorder_time_pickup', '' );
		$preorder_class      = $preorder_enabled ? '' : 'wrc-field--preorder-hidden';
		$average_pickup_time = get_option( 'foodord_average_pickup_time', '20' );

		?>
		<div class="wrap">
			<h1 class="wrc-admin-title"><?php esc_html_e( 'Pickup Settings', 'foodord' ); ?></h1>
			<?php
			if ( function_exists( 'foodord_admin_render_horizontal_menu' ) ) {
				foodord_admin_render_horizontal_menu();
			}
			?>
			<?php
			// Check for success message transient.
			if ( get_transient( 'foodord_pickup_settings_saved' ) ) {
				delete_transient( 'foodord_pickup_settings_saved' );
				?>
				<div class="notice notice-success is-dismissible">
					<p class="notice-message"><?php esc_html_e( 'Settings saved.', 'foodord' ); ?></p>
				</div>
				<?php
			}
			?>
			<div class="wrc-admin-content">
				<form class="wrc-settings-form" method="post" action="">
					<?php wp_nonce_field( 'wrc_save_pickup_hours', 'wrc_save_pickup_hours_nonce' ); ?>
					<input type="hidden" id="wrc_pickup_custom_hours" name="wrc_pickup_custom_hours" value="<?php echo esc_attr( wp_json_encode( $custom_hours ) ); ?>">
					
					<div class="wrc-pickup-hours-container">
						<div class="wrc-pickup-hours-header">
							<h2 class="wrc-section-title"><?php esc_html_e( 'Pickup Hours', 'foodord' ); ?></h2>
							<p class="wrc-admin-description"><?php esc_html_e( 'Manage your restaurant pickup hours and pickup settings from here.', 'foodord' ); ?></p>
						</div>
						
						<div class="wrc-pickup-hours-content">
							<?php foreach ( $day_names as $day => $day_name ) : ?>
								<div class="wrc-day-row">
									<div class="wrc-day-label"><?php echo esc_html( $day_name ); ?></div>
									
									<div class="wrc-time-inputs">
										<?php if ( ! $hours[ $day ]['closed'] ) : ?>
											<div class="wrc-time-group">
												<input type="time" 
													name="<?php echo esc_attr( $day . '_morning_start' ); ?>" 
													value="<?php echo esc_attr( $hours[ $day ]['morning_start'] ); ?>" 
													class="wrc-time-input">
												<span class="wrc-time-separator">-</span>
												<input type="time" 
													name="<?php echo esc_attr( $day . '_morning_end' ); ?>" 
													value="<?php echo esc_attr( $hours[ $day ]['morning_end'] ); ?>" 
													class="wrc-time-input">
											</div>
											
											<span class="wrc-period-separator">/</span>
											
											<div class="wrc-time-group">
												<input type="time" 
													name="<?php echo esc_attr( $day . '_evening_start' ); ?>" 
													value="<?php echo esc_attr( $hours[ $day ]['evening_start'] ); ?>" 
													class="wrc-time-input">
												<span class="wrc-time-separator">-</span>
												<input type="time" 
													name="<?php echo esc_attr( $day . '_evening_end' ); ?>" 
													value="<?php echo esc_attr( $hours[ $day ]['evening_end'] ); ?>" 
													class="wrc-time-input">
											</div>
										<?php else : ?>
											<div class="wrc-time-group">
												<input type="time" 
													name="<?php echo esc_attr( $day . '_morning_start' ); ?>" 
													value="" 
													class="wrc-time-input" 
													disabled>
												<span class="wrc-time-separator">-</span>
												<input type="time" 
													name="<?php echo esc_attr( $day . '_morning_end' ); ?>" 
													value="" 
													class="wrc-time-input" 
													disabled>
											</div>
											
											<span class="wrc-period-separator">/</span>
											
											<div class="wrc-time-group">
												<input type="time" 
													name="<?php echo esc_attr( $day . '_evening_start' ); ?>" 
													value="" 
													class="wrc-time-input" 
													disabled>
												<span class="wrc-time-separator">-</span>
												<input type="time" 
													name="<?php echo esc_attr( $day . '_evening_end' ); ?>" 
													value="" 
													class="wrc-time-input" 
													disabled>
											</div>
										<?php endif; ?>
									</div>
									
									<div class="wrc-closed-checkbox">
										<label class="wrc-closed-label">
											<input type="checkbox" class="wrc-closed-input"
												name="<?php echo esc_attr( $day . '_closed' ); ?>" 
												value="1" 
												<?php checked( $hours[ $day ]['closed'], true ); ?>
												onchange="this.closest('.wrc-day-row').querySelectorAll('.wrc-time-input').forEach(input => input.disabled = this.checked)">
											<?php esc_html_e( 'Closed', 'foodord' ); ?>
										</label>
									</div>
								</div>
							<?php endforeach; ?>
						</div>
					</div>
					
					<div class="wrc-pickup-preorder-container">
						<div class="wrc-pickup-preorder-header">
							<h2 class="wrc-section-title"><?php esc_html_e( 'Pre-order', 'foodord' ); ?></h2>
							<p class="wrc-card-sub"><?php esc_html_e( 'Let customers reserve a pickup time in advance.', 'foodord' ); ?></p>
						</div>
						
						<div class="wrc-pickup-preorder-content">
							<div class="wrc-pickup-preorder-grid">
								<div class="wrc-pickup-preorder-group">
									<div class="wrc-pickup-preorder-cell">
										<label class="wrc-field-label" for="wrc_preorder_enabled_pickup"><?php esc_html_e( 'Enable pre-orders', 'foodord' ); ?></label>
										<div class="wrc-control">
											<div class="foodord-toggle-row">
												<label class="foodord-toggle-switch" for="wrc_preorder_enabled_pickup">
													<input type="checkbox" class="wrc-input" id="wrc_preorder_enabled_pickup" name="wrc_preorder_enabled_pickup" value="1" <?php checked( $preorder_enabled, true ); ?> />
													<span class="foodord-toggle-slider"></span>
												</label>
												<span class="foodord-toggle-label"><?php esc_html_e( 'Customers can schedule pickup for later.', 'foodord' ); ?></span>
											</div>
										</div>
									</div>

									<div class="wrc-pickup-preorder-cell <?php echo esc_attr( $preorder_class ); ?>" id="wrc_preorder_time_wrapper_pickup">
										<label class="wrc-field-label" for="wrc_preorder_time_pickup"><?php esc_html_e( 'Earliest pickup time', 'foodord' ); ?></label>
										<div class="wrc-control">
											<input type="time" id="wrc_preorder_time_pickup" name="wrc_preorder_time_pickup" class="wrc-input" value="<?php echo esc_attr( $preorder_time ); ?>" />
											<p class="wrc-help"><?php esc_html_e( 'Visible to customers when pre-orders are enabled.', 'foodord' ); ?></p>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
					
					<div class="wrc-average-pickup-time-container">
						<div class="wrc-average-pickup-time-header">
							<h2 class="wrc-section-title"><?php esc_html_e( 'Average Pickup Time', 'foodord' ); ?></h2>
							<p class="wrc-card-sub"><?php esc_html_e( 'Configure estimated pickup time information for customers.', 'foodord' ); ?></p>
						</div>
						
						<div class="wrc-average-pickup-time-content">
							<div class="wrc-average-pickup-time-grid">
								<div class="wrc-average-pickup-time-group">
									<div class="wrc-average-pickup-time-cell">
										<label class="wrc-field-label" for="wrc_average_pickup_time"><?php esc_html_e( 'Average Pickup Time', 'foodord' ); ?></label>
										<div class="wrc-control">
											<input type="number" id="wrc_average_pickup_time" name="wrc_average_pickup_time" class="wrc-input" value="<?php echo esc_attr( $average_pickup_time ); ?>" min="0" max="240" step="1" />
											<p class="wrc-help"><?php esc_html_e( 'Enter the estimated pickup time in minutes.', 'foodord' ); ?></p>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>

					<div class="wrc-custom-hours-container">
						<div class="wrc-custom-hours-header">
							<h2 class="wrc-section-title"><?php esc_html_e( 'Custom Pickup Hours', 'foodord' ); ?></h2>
							<p class="wrc-custom-description"><?php esc_html_e( 'Configure special pickup hours for holidays or custom dates.', 'foodord' ); ?></p>
						</div>
						
						<div class="wrc-custom-hours-content">
							<button type="button" class="button button-secondary wrc-add-custom-hour" id="wrc-add-custom-pickup-hour-btn">
								<?php esc_html_e( 'Add Custom Pickup Hours', 'foodord' ); ?>
							</button>
							<div id="wrc-pickup-custom-hours-list" class="wrc-custom-hours-list wrc-empty-state">
								<p class="wrc-empty-message">No custom pickup hours added yet. Click the button above to add.</p>
							</div>
						</div>
					</div>
					
					<?php
					// Render Time To Pickup container.
					$this->time_to_pickup->render_time_to_pickup_container();
					?>
					
		<?php submit_button( esc_html__( 'Save Changes', 'foodord' ), 'primary', 'wrc_save_pickup_hours' ); ?>
				</form>
			</div>
		</div>
		
		<!-- Custom Pickup Hours Modal -->
		<div id="wrc-pickup-custom-hours-modal" class="foodord-modal">
			<div class="foodord-modal__content">
				<div class="foodord-modal__header">
					<h3 class="foodord-modal__title" data-add-title="<?php echo esc_attr__( 'Add Custom Pickup Hours', 'foodord' ); ?>" data-edit-title="<?php echo esc_attr__( 'Edit Custom Pickup Hours', 'foodord' ); ?>">
						<?php esc_html_e( 'Add Custom Pickup Hours', 'foodord' ); ?>
					</h3>
					<button type="button" class="foodord-modal__close" id="wrc-pickup-modal-close-btn">
						<span class="dashicons dashicons-no-alt"></span>
					</button>
				</div>
				
				<div class="foodord-modal__body">
					<form id="wrc-pickup-custom-hours-form" class="wrc-modal-form">
						<div class="wrc-field">
							<label class="wrc-field-label" for="pickup-custom-start-date"><?php esc_html_e( 'Start Date', 'foodord' ); ?></label>
							<input type="date" id="pickup-custom-start-date" name="pickup-custom-start-date" class="regular-text" required>
						</div>

						<div class="wrc-field">
							<label class="wrc-field-label" for="pickup-custom-end-date"><?php esc_html_e( 'End Date', 'foodord' ); ?></label>
							<input type="date" id="pickup-custom-end-date" name="pickup-custom-end-date" class="regular-text">
						</div>

						<div class="wrc-field">
							<label class="wrc-field-label" for="pickup-custom-description"><?php esc_html_e( 'Description', 'foodord' ); ?></label>
							<input type="text" id="pickup-custom-description" name="pickup-custom-description" class="regular-text" placeholder="<?php esc_attr_e( 'e.g., New Year, Holiday', 'foodord' ); ?>">
						</div>
						
						<div class="wrc-field wrc-field--toggle">
							<div class="wrc-toggle-container">
								<label class="wrc-toggle-switch">
									<span class="wrc-toggle-label"><?php esc_html_e( 'Closed', 'foodord' ); ?></span>
									<div class="wrc-toggle-switch-control">
								<input class="wrc-toggle-input" type="checkbox" id="pickup-custom-closed" name="pickup-custom-closed">
										<span class="wrc-toggle-slider"></span>
									</div>
								</label>
							</div>
						</div>
						
						<div id="pickup-custom-time-inputs" class="wrc-custom-time-inputs">
							<div class="wrc-time-row">
								<label class="wrc-time-label"><?php esc_html_e( 'Morning Hours', 'foodord' ); ?></label>
								<div class="wrc-time-group">
									<input type="time" id="pickup-custom-morning-start" name="pickup-custom-morning-start" class="wrc-time-input">
									<span class="wrc-time-separator">-</span>
									<input type="time" id="pickup-custom-morning-end" name="pickup-custom-morning-end" class="wrc-time-input">
								</div>
							</div>
							
							<div class="wrc-time-row">
								<label class="wrc-time-label"><?php esc_html_e( 'Evening Hours', 'foodord' ); ?></label>
								<div class="wrc-time-group">
									<input type="time" id="pickup-custom-evening-start" name="pickup-custom-evening-start" class="wrc-time-input">
									<span class="wrc-time-separator">-</span>
									<input type="time" id="pickup-custom-evening-end" name="pickup-custom-evening-end" class="wrc-time-input">
								</div>
							</div>
						</div>
					</form>
				</div>
				
				<div class="foodord-modal__actions">
					<button type="button" class="foodord-modal__button foodord-modal__button--cancel" id="wrc-pickup-modal-cancel-btn">
						<?php esc_html_e( 'Cancel', 'foodord' ); ?>
					</button>
					<button type="button" class="foodord-modal__button foodord-modal__button--confirm" id="wrc-pickup-modal-save-btn" data-add-label="<?php echo esc_attr__( 'Add Pickup Hours', 'foodord' ); ?>" data-update-label="<?php echo esc_attr__( 'Save changes', 'foodord' ); ?>">
						<?php esc_html_e( 'Add Pickup Hours', 'foodord' ); ?>
					</button>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Sanitize pickup opening hours option.
	 *
	 * @param mixed $value Value to sanitize.
	 */
	public function sanitize_pickup_opening_hours_option( $value ): array {
		$days      = array( 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday' );
		$sanitized = array();

		if ( ! is_array( $value ) ) {
			return $sanitized;
		}

		foreach ( $days as $day ) {
			$day_data          = isset( $value[ $day ] ) && is_array( $value[ $day ] ) ? $value[ $day ] : array();
			$closed            = ! empty( $day_data['closed'] );
			$sanitized[ $day ] = array(
				'closed'        => $closed,
				'morning_start' => $closed ? '' : $this->sanitize_time_string( $day_data['morning_start'] ?? '' ),
				'morning_end'   => $closed ? '' : $this->sanitize_time_string( $day_data['morning_end'] ?? '' ),
				'evening_start' => $closed ? '' : $this->sanitize_time_string( $day_data['evening_start'] ?? '' ),
				'evening_end'   => $closed ? '' : $this->sanitize_time_string( $day_data['evening_end'] ?? '' ),
			);
		}

		return $sanitized;
	}

	/**
	 * Sanitize pickup custom hours option.
	 *
	 * @param mixed $value Value to sanitize.
	 */
	public function sanitize_pickup_custom_hours_option( $value ): array {
		if ( ! is_array( $value ) ) {
			return array();
		}

		$sanitized = array();
		foreach ( $value as $entry ) {
			if ( ! is_array( $entry ) ) {
				continue;
			}
			$date_range = $this->normalize_custom_hours_date_range( $entry );
			if ( null === $date_range ) {
				continue;
			}
			$sanitized[] = array(
				'date'          => $date_range['start_date'],
				'start_date'    => $date_range['start_date'],
				'end_date'      => $date_range['end_date'],
				'description'   => isset( $entry['description'] ) ? sanitize_text_field( (string) $entry['description'] ) : '',
				'closed'        => ! empty( $entry['closed'] ),
				'morning_start' => $this->sanitize_time_string( $entry['morning_start'] ?? ( $entry['morningStart'] ?? '' ) ),
				'morning_end'   => $this->sanitize_time_string( $entry['morning_end'] ?? ( $entry['morningEnd'] ?? '' ) ),
				'evening_start' => $this->sanitize_time_string( $entry['evening_start'] ?? ( $entry['eveningStart'] ?? '' ) ),
				'evening_end'   => $this->sanitize_time_string( $entry['evening_end'] ?? ( $entry['eveningEnd'] ?? '' ) ),
			);
		}

		return $sanitized;
	}

	/**
	 * Sanitize HH:MM formatted option.
	 *
	 * @param mixed $value Value to sanitize.
	 */
	public function sanitize_time_option( $value ): string {
		return $this->sanitize_time_string( $value );
	}

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

	/**
	 * Sanitize time-to-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';
	}

	/**
	 * Sanitize average pickup time option.
	 *
	 * @param mixed $value Value to sanitize.
	 */
	public function sanitize_average_pickup_time_option( $value ): int {
		$time = absint( $value );
		if ( $time <= 0 || $time > 240 ) {
			$time = 20;
		}
		return $time;
	}

	/**
	 * Sanitize scheduled date max days option.
	 *
	 * @param mixed $value Value to sanitize.
	 */
	public function sanitize_scheduled_date_max_days_option( $value ): int {
		$days = absint( $value );
		if ( $days < 1 || $days > 365 ) {
			$days = 7;
		}
		return $days;
	}

	/**
	 * Sanitize time strings.
	 *
	 * @param mixed $value Value to sanitize.
	 */
	private function sanitize_time_string( $value ): string {
		if ( ! is_scalar( $value ) ) {
			return '';
		}
		$time = sanitize_text_field( (string) $value );
		return preg_match( '/^\d{2}:\d{2}$/', $time ) ? $time : '';
	}

	/**
	 * Normalize a custom hours date range.
	 *
	 * @param array $entry Custom hours entry.
	 * @return array|null Date range or null when invalid.
	 */
	private function normalize_custom_hours_date_range( array $entry ): ?array {
		$start_date = $this->sanitize_date_string( $entry['start_date'] ?? ( $entry['startDate'] ?? ( $entry['date'] ?? '' ) ) );
		if ( '' === $start_date ) {
			return null;
		}

		$end_date = $this->sanitize_date_string( $entry['end_date'] ?? ( $entry['endDate'] ?? ( $entry['date'] ?? $start_date ) ) );
		if ( '' === $end_date ) {
			$end_date = $start_date;
		}

		if ( $end_date < $start_date ) {
			$initial_start_date = $start_date;
			$start_date         = $end_date;
			$end_date           = $initial_start_date;
		}

		return array(
			'start_date' => $start_date,
			'end_date'   => $end_date,
		);
	}

	/**
	 * Sanitize date strings.
	 *
	 * @param mixed $value Value to sanitize.
	 */
	private function sanitize_date_string( $value ): string {
		if ( ! is_scalar( $value ) ) {
			return '';
		}

		$date = sanitize_text_field( (string) $value );
		if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}$/', $date ) ) {
			return '';
		}

		$parsed = date_create_from_format( '!Y-m-d', $date );
		return $parsed && $parsed->format( 'Y-m-d' ) === $date ? $date : '';
	}
}
