<?php
/**
 * FoodOrd: Delivery/Pickup toggle (frontend)
 *
 * Injects a mode toggle UI above the cart header (.wr-card__header)
 * for large screens (>=1025px). JS handles DOM insertion; this class
 * conditionally enqueues the script on pages where the card exists.
 *
 * @package FoodOrd
 */

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

/**
 * Class Foodord_Delivery_Pickup_Toggle
 */
class Foodord_Delivery_Pickup_Toggle {
	/**
	 * Bootstrap hooks.
	 */
	public function init(): void {
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
	}

	/**
	 * Determine if we should enable the toggle on this request.
	 * Looks for [foodord_card] shortcode or foodord/card block.
	 *
	 * @return bool
	 */
	protected function should_enable(): bool {
		if ( is_admin() || wp_doing_ajax() ) {
			return false;
		}

		global $post;
		if ( ! $post || ! is_object( $post ) ) {
			return false;
		}

		$content = (string) $post->post_content;

		if ( function_exists( 'has_shortcode' ) && has_shortcode( $content, 'foodord_card' ) ) {
			return true;
		}

		if ( function_exists( 'has_block' ) && has_block( 'foodord/card', $content ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Enqueue frontend script and pass translatable labels.
	 */
	public function enqueue_assets(): void {
		// Enqueue on all frontend views; JS checks presence of .wr-card and viewport >= 1025.
		wp_enqueue_script(
			'foodord-delivery-pickup-toggle',
			FOODORD_URL . 'public/js/wrc-delivery-pickup-toggle.js',
			array(),
			FOODORD_VERSION,
			true
		);

		$mode_status = array();
		$ref_time    = '';
		$timezone    = '';
		if ( class_exists( 'Foodord_Business_Hours' ) ) {
			$status_payload = Foodord_Business_Hours::get_status_payload();
			if ( isset( $status_payload['modes'] ) && is_array( $status_payload['modes'] ) ) {
				$mode_status = $status_payload['modes'];
			}
			$ref_time = isset( $status_payload['ref_time'] ) ? (string) $status_payload['ref_time'] : '';
			$timezone = isset( $status_payload['timezone'] ) ? (string) $status_payload['timezone'] : '';
		}

		// Determine current mode from session/cookie to keep selection across reloads.
		$mode = 'delivery';
		if ( function_exists( 'foodord_get_order_mode' ) ) {
			$mode = foodord_get_order_mode();
		} elseif ( function_exists( 'WC' ) && WC()->session ) {
			$session_mode = (string) WC()->session->get( 'foodord_order_mode', 'delivery' );
			if ( in_array( $session_mode, array( 'delivery', 'pickup' ), true ) ) {
				$mode = $session_mode;
			}
		}

		wp_localize_script(
			'foodord-delivery-pickup-toggle',
			'FoodordDeliveryPickup',
			array(
				'deliveryLabel' => __( 'Delivery', 'foodord' ),
				'pickupLabel'   => __( 'Pickup', 'foodord' ),
				'ajaxUrl'       => admin_url( 'admin-ajax.php' ),
				'nonce'         => wp_create_nonce( 'foodord_order_mode' ),
				'modeStatus'    => $mode_status,
				'closedLabel'   => __( 'Closed', 'foodord' ),
				'closedMessage' => __( 'Currently unavailable during closed hours.', 'foodord' ),
				'referenceTime' => $ref_time,
				'timezone'      => $timezone,
				'currentMode'   => $mode,
				'themeActive'   => defined( 'FOODORD_THEME_ACTIVE' ) ? (bool) FOODORD_THEME_ACTIVE : false,
			)
		);
	}
}
