<?php
/**
 * Sync 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\Sync_Rest_Controller;

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

/**
 * Class Sync_APIs
 *
 * Registers all Manual Sync REST API controllers and routes
 */
class Sync_APIs {

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

		// Start sync.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/sync/start',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => array( $controller, 'start_sync' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		// Pause sync.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/sync/pause',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => array( $controller, 'pause_sync' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		// Resume sync.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/sync/resume',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => array( $controller, 'resume_sync' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		// Abort sync.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/sync/abort',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => array( $controller, 'abort_sync' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

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