<?php
/**
 * WooCommerce product data integration for reservation pre-orders.
 *
 * @package FoodOrd
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class Foodord_Product_Data_Preorders
 */
class Foodord_Product_Data_Preorders {
	/**
	 * Bootstrap WooCommerce product data hooks.
	 */
	public function init(): void {
		if ( ! is_admin() ) {
			return;
		}

		add_filter( 'woocommerce_product_data_tabs', array( $this, 'register_product_data_tab' ), 100, 1 );
		add_action( 'woocommerce_product_data_panels', array( $this, 'render_product_data_panel' ) );
		add_action( 'woocommerce_process_product_meta', array( $this, 'save_product_settings' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_product_data_assets' ) );
	}

	/**
	 * Enqueue pre-order product data styles.
	 *
	 * @param string $hook Current admin page hook.
	 */
	public function enqueue_product_data_assets( string $hook ): void {
		if ( 'post.php' !== $hook && 'post-new.php' !== $hook ) {
			return;
		}

		$screen = get_current_screen();
		if ( ! $screen || 'product' !== $screen->post_type ) {
			return;
		}

		wp_enqueue_style(
			'foodord-product-data-preorders',
			plugin_dir_url( __FILE__ ) . 'css/foodord-product-data-preorders.css',
			array( 'woocommerce_admin_styles' ),
			FOODORD_VERSION
		);
	}

	/**
	 * Register the reservation pre-order product data tab.
	 *
	 * @param array<string,array<string,mixed>> $tabs Existing product data tabs.
	 * @return array<string,array<string,mixed>>
	 */
	public function register_product_data_tab( array $tabs ): array {
		$tabs['foodord_reservation_preorder'] = array(
			'label'    => esc_html__( 'Pre-order', 'foodord' ),
			'target'   => 'foodord_reservation_preorder_data',
			'class'    => array( 'show_if_simple', 'show_if_variable' ),
			'priority' => 85,
		);

		return $tabs;
	}

	/**
	 * Render the reservation pre-order product data panel.
	 */
	public function render_product_data_panel(): void {
		global $post;

		if ( ! $post instanceof WP_Post ) {
			return;
		}

		$setting_id     = Foodord_Table_Reservation_Preorders::PRODUCT_META_ENABLED;
		$description_id = $setting_id . '_description';

		?>
		<div id="foodord_reservation_preorder_data" class="panel woocommerce_options_panel hidden">
			<div class="options_group foodord-preorder-product-settings">
				<div class="foodord-preorder-product-setting">
					<div class="foodord-preorder-product-setting__content">
						<span class="foodord-preorder-product-setting__title"><?php esc_html_e( 'Available for reservation pre-order', 'foodord' ); ?></span>
						<p id="<?php echo esc_attr( $description_id ); ?>" class="foodord-preorder-product-setting__description">
							<?php esc_html_e( 'Allow guests to select this product while making a table reservation.', 'foodord' ); ?>
						</p>
					</div>
					<label class="foodord-preorder-product-toggle" for="<?php echo esc_attr( $setting_id ); ?>">
						<input
							id="<?php echo esc_attr( $setting_id ); ?>"
							type="checkbox"
							name="<?php echo esc_attr( $setting_id ); ?>"
							value="yes"
							aria-describedby="<?php echo esc_attr( $description_id ); ?>"
							<?php checked( Foodord_Table_Reservation_Preorders::is_product_enabled( (int) $post->ID ) ); ?>
						/>
						<span class="foodord-preorder-product-toggle__control" aria-hidden="true"></span>
						<span class="screen-reader-text"><?php esc_html_e( 'Available for reservation pre-order', 'foodord' ); ?></span>
					</label>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Save reservation pre-order product settings.
	 *
	 * @param int $post_id Product ID.
	 */
	public function save_product_settings( int $post_id ): void {
		if ( ! isset( $_POST['woocommerce_meta_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['woocommerce_meta_nonce'] ) ), 'woocommerce_save_data' ) ) {
			return;
		}

		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}

		if ( isset( $_POST[ Foodord_Table_Reservation_Preorders::PRODUCT_META_ENABLED ] ) ) {
			delete_post_meta( $post_id, Foodord_Table_Reservation_Preorders::PRODUCT_META_ENABLED );
			return;
		}

		update_post_meta( $post_id, Foodord_Table_Reservation_Preorders::PRODUCT_META_ENABLED, 'no' );
	}
}
