<?php
/**
 * Connection 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\Connection_Rest_Controller;

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

/**
 * Class Connection_APIs
 *
 * Registers all Connection REST API controllers and routes
 */
class Connection_APIs {

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

		// Supplier-scoped connections.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/connections',
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $controller, 'get_items' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
				),
				array(
					'methods'             => WP_REST_Server::CREATABLE,
					'callback'            => array( $controller, 'create_item' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
				),
			)
		);

		// Individual connection operations.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/connections/(?P<connection_uuid>[a-zA-Z0-9-]+)',
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $controller, 'get_item' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
				),
				array(
					'methods'             => WP_REST_Server::EDITABLE,
					'callback'            => array( $controller, 'update_item' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
				),
				array(
					'methods'             => WP_REST_Server::DELETABLE,
					'callback'            => array( $controller, 'delete_item' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
				),
			)
		);

		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/connections/(?P<connection_uuid>[a-zA-Z0-9-]+)/test',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => array( $controller, 'test_connection' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/connections/(?P<connection_uuid>[a-zA-Z0-9-]+)/chunked-upload',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => array( $controller, 'chunked_upload' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);
	}
}
