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

namespace MOWCPS\Common\Controllers\Rest_API;

use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
use MOWCPS\Common\Abstracts\Base_Rest_Controller;
use MOWCPS\Common\Handlers\Rest_API\Setting_Handler;
use MOWCPS\Common\Utils\Response_Builder;

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

/**
 * Class Setting_Rest_Controller
 *
 * Handles settings REST API callbacks for all setting types
 */
class Setting_Rest_Controller extends Base_Rest_Controller {
	/**
	 * Handler instance
	 *
	 * @var Setting_Handler
	 */
	private $handler;

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->handler = new Setting_Handler();
	}

	/**
	 * Get all settings by type for a supplier
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function get_items( $request ) {
		$supplier_uuid = sanitize_key( wp_unslash( $request->get_param( 'supplier_uuid' ) ) );
		$type          = sanitize_text_field( wp_unslash( $request->get_param( 'type' ) ) );
		$params        = array();

		$settings = $this->handler->get_all_by_type( $supplier_uuid, $type, $params );

		if ( is_wp_error( $settings ) ) {
			return Response_Builder::from_wp_error( $settings );
		}

		return Response_Builder::success( $settings );
	}

	/**
	 * Get setting by UUID
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function get_item( $request ) {
		$setting_uuid = sanitize_key( wp_unslash( $request->get_param( 'setting_uuid' ) ) );
		$setting      = $this->handler->get_by_uuid( $setting_uuid );

		if ( is_wp_error( $setting ) ) {
			return Response_Builder::from_wp_error( $setting );
		}

		return Response_Builder::success( $setting );
	}

	/**
	 * Create setting for a supplier
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function create_item( $request ) {
		$supplier_uuid = sanitize_key( $request->get_param( 'supplier_uuid' ) );
		$type          = sanitize_text_field( $request->get_param( 'type' ) );
		$data          = $request->get_json_params();

		$setting = $this->handler->create( $supplier_uuid, $type, $data );

		if ( is_wp_error( $setting ) ) {
			return Response_Builder::from_wp_error( $setting );
		}

		return Response_Builder::success( $setting, 201 );
	}

	/**
	 * Update setting
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function update_item( $request ) {
		$setting_uuid = sanitize_key( wp_unslash( $request->get_param( 'setting_uuid' ) ) );
		$data         = $request->get_json_params();

		$setting = $this->handler->update( $setting_uuid, $data );

		if ( is_wp_error( $setting ) ) {
			return Response_Builder::from_wp_error( $setting );
		}

		return Response_Builder::success( $setting );
	}

	/**
	 * Delete setting
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function delete_item( $request ) {
		$setting_uuid = sanitize_key( wp_unslash( $request->get_param( 'setting_uuid' ) ) );
		$result       = $this->handler->delete( $setting_uuid );

		if ( is_wp_error( $result ) ) {
			return Response_Builder::from_wp_error( $result );
		}

		return Response_Builder::deleted();
	}
}
