<?php
/**
 * Settings 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\Setting_Rest_Controller;

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

/**
 * Class Settings_APIs
 *
 * Registers all Settings REST API controllers and routes
 */
class Settings_APIs {

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

		// Generic settings by UUID.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/settings/(?P<setting_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' ),
				),
			)
		);

		// All typed settings endpoints.
		$setting_types = array(
			'schedule',
			'filter',
			'outbound',
			'product-operations',
			'file-sync',
			'page',
			'batch',
			'multi-threading',
		);

		foreach ( $setting_types as $type ) {
			register_rest_route(
				Constants::REST_API_NAMESPACE,
				'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/settings',
				array(
					array(
						'methods'             => WP_REST_Server::READABLE,
						'callback'            => array( $controller, 'get_items' ),
						'permission_callback' => array( $controller, 'check_permissions' ),
						'args'                => array(
							'type' => array(
								'required' => true,
								'default'  => $type,
								'enum'     => $setting_types,
							),
						),
					),
					array(
						'methods'             => WP_REST_Server::CREATABLE,
						'callback'            => array( $controller, 'create_item' ),
						'permission_callback' => array( $controller, 'check_permissions' ),
						'args'                => array(
							'type' => array(
								'required' => true,
								'default'  => $type,
								'enum'     => $setting_types,
							),
						),
					),
				)
			);
		}
	}
}
