<?php
/**
 * Preview REST API Loader
 *
 * @category   WordPress_Plugin
 * @package    Common
 * @subpackage Rest_APIs
 * @author     miniOrange <info@miniorange.com>
 * @license    MIT/Expat
 * @link       https://plugins.miniorange.com/woocommerce-api-product-sync-with-woocommerce-rest-apis
 */

namespace MOWCPS\Common\Rest_APIs;

use WP_REST_Server;
use MOWCPS\Common\Constants\Constants;
use MOWCPS\Common\Controllers\Rest_API\Preview_Rest_Controller;

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

/**
 * Class Preview_APIs
 *
 * Registers all Preview REST API controllers and routes
 */
class Preview_APIs {

	/**
	 * Register all routes
	 *
	 * @return void
	 */
	public static function register_routes() {
		$controller = new Preview_Rest_Controller();

		// Get preview products from supplier (POST with uuids in payload, pagination in query).
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/preview/products',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => array( $controller, 'get_preview_products' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		// Save preview filter.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/preview/save-filter',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => array( $controller, 'save_preview_filter' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		// Get existing preview filter.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/preview/filter',
			array(
				'methods'             => WP_REST_Server::READABLE,
				'callback'            => array( $controller, 'get_preview_filter' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		// Delete preview filter.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/preview/filter',
			array(
				'methods'             => WP_REST_Server::DELETABLE,
				'callback'            => array( $controller, 'delete_preview_filter' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		// Delete preview cache only (keep filter in DB).
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/preview/cache',
			array(
				'methods'             => WP_REST_Server::DELETABLE,
				'callback'            => array( $controller, 'delete_preview_cache' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);
	}
}
