<?php
/**
 * Attribute Mapping 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\Attribute_Mapping_Rest_Controller;

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

/**
 * Class Attribute_Mapping_APIs
 *
 * Registers all Attribute Mapping REST API controllers and routes
 */
class Attribute_Mapping_APIs {

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

		// Supplier-scoped attribute mappings.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/attribute-mappings',
			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 mapping operations.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/attribute-mappings/(?P<mapping_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' ),
				),
			)
		);
	}
}
