<?php
/**
 * Product page recommendations.
 *
 * @package PRECO
 */

namespace PRECO\Includes\Frontend\Pages;

use PRECO\Includes\Frontend\Core\PageRecommendationsBase;

defined( 'ABSPATH' ) || exit;

/**
 * Product page recommendations.
 */
class ProductPageRecommendations extends PageRecommendationsBase {

	/**
	 * Page key in option structure.
	 *
	 * @return string
	 */
	protected function page_key() {
		return 'product-page';
	}

	/**
	 * Is the current request the relevant page?
	 *
	 * @return bool
	 */
	protected function is_active_page() {
		return function_exists( 'is_product' ) && \is_product();
	}

	/**
	 * Map of location key => hook definition.
	 *
	 * @return array
	 */
	protected function locations() {
		return array(
			'before-add-to-cart'  => array(
				'hook'          => 'woocommerce_before_add_to_cart_button',
				'priority'      => 10,
				'accepted_args' => 0,
			),
			'after-add-to-cart'   => array(
				'hook'          => 'woocommerce_after_add_to_cart_button',
				'priority'      => 10,
				'accepted_args' => 0,
			),
			'before-product-tabs' => array(
				'hook'          => 'woocommerce_after_single_product_summary',
				'priority'      => 5,
				'accepted_args' => 0,
			),
			'after-product-tabs'  => array(
				'hook'          => 'woocommerce_after_single_product_summary',
				'priority'      => 20,
				'accepted_args' => 0,
			),
			'after-product-meta'  => array(
				'hook'          => 'woocommerce_product_meta_end',
				'priority'      => 41,
				'accepted_args' => 0,
			),
		);
	}

	/**
	 * Build context for rule evaluation.
	 *
	 * @param string $location_key Location key.
	 * @param array  $hook_args Hook args.
	 * @return array
	 */
	protected function build_context( $location_key, $hook_args ) {
		$product_id = function_exists( 'get_the_ID' ) ? absint( \get_the_ID() ) : 0;

		return array(
			'page'         => $this->page_key(),
			'location_key' => (string) $location_key,
			'product_id'   => $product_id,
		);
	}
}
