<?php

/**
 * Implements wiaas shop based search on top of woocommerce product api
 *
 * Class Wiaas_WC_Package_API_Integration
 */
class Wiaas_WC_Package_API_Integration {

	/**
	 * Rest base for woocommerce product search
	 *
	 * @var string
	 */
	private static $wc_rest_base = '/wc/v2/products';

    public static function init() {

        add_filter('woocommerce_rest_product_object_query', array(__CLASS__, 'filter_packages'), 10, 2);

        add_filter('rest_dispatch_request', array(__CLASS__, 'validate_package_search_request'), 10, 4);
    }


	/**
	 * Force wc api request to send shop id when searching packages
	 *
	 * @param $null
	 * @param $request
	 * @param $route
	 * @param $handler
	 *
	 * @return null|WP_Error
	 */
    public static function validate_package_search_request($null, $request, $route, $handler) {

    	if (strpos($route, self::$wc_rest_base) !== false) {

    		if (empty($request['shop_id']) || ! absint($request['shop_id'])) {

				    return new WP_Error(
					    'missing_shop',
					    'Shop parameter is missing',
					    array ( 'status' => 400 )
				    );
		    }
	    }

	    return null;
    }

	/**
	 * Filter woocommerce REST API query so only valid wiaas packages are returned to the customer
	 *
	 * @param $args
	 * @param $request
	 *
	 * @return mixed
	 */
    public static function filter_packages($args, $request) {

	    if ( empty($args['tax_query']) ){
		    $args['tax_query'] = array();
	    }

	    // Retrieve only packages with available package status
	    $args['tax_query'][] = array(
			    'taxonomy' => 'package_status',
			    'field' => 'name',
			    'terms' => Wiaas_Package_Status::AVAILABLE
	    );

	    /**
	     * Retrieve packages that satisfy one of two:
	     *
	     *  1) Package has at least one visible customer specific price set (for current customer)
	     *  2) Package has at least one visible default price set and not customer specific prices set (for current customer)
	     *
	     * This approach enables us that if package has specific prices set for current customer only those prices
	     * are taken into account and default ones are ignored.
	     * Only if package has no specific prices for current customer default prices are taken into account.
	     *
	     */

        $shop_id = absint($request['shop_id']);
        $customer_id = wiaas_get_current_user_organization_id();

        $default_price_search_term = '_' . $shop_id . '_default';
        $customer_visible_price_search_term = '_' . $shop_id . '_customer_' . $customer_id . '_visible';
	    $customer_hidden_price_search_term = '_' . $shop_id . '_customer_' . $customer_id . '_hidden';

	    $args['tax_query'][] = array(
	    	'relation' => 'OR',
	    	array(
			    'taxonomy'     => '_wiaas_shop_prices',
			    'terms'   =>  $customer_visible_price_search_term,
			    'field' => 'slug'
		    ),
		    array(
			    array(
				    'taxonomy'     => '_wiaas_shop_prices',
				    'terms'   =>  $default_price_search_term,
				    'field' => 'slug'
			    ),
			    array(
				    'taxonomy'     => '_wiaas_shop_prices',
				    'terms'   =>  $customer_hidden_price_search_term,
				    'field' => 'slug',
				    'operator' => 'NOT IN'
			    )
		    )
	    );

        return $args;
    }
}

Wiaas_WC_Package_API_Integration::init();