<?php
/**
 * Attribute Mapping 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\Attribute_Mapping_Handler;
use MOWCPS\Common\Utils\Response_Builder;

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

/**
 * Class Attribute_Mapping_Rest_Controller
 *
 * Handles attribute mapping REST API callbacks
 */
class Attribute_Mapping_Rest_Controller extends Base_Rest_Controller {
	/**
	 * Handler instance
	 *
	 * @var Attribute_Mapping_Handler
	 */
	private $handler;

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

	/**
	 * Get all attribute mappings 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' ) ) );
		$params        = array();

		if ( $request->get_param( 'connection_id' ) ) {
			$params['connection_id'] = sanitize_key( wp_unslash( $request->get_param( 'connection_id' ) ) );
		}
		if ( $request->get_param( 'product_type' ) ) {
			$params['product_type'] = sanitize_text_field( wp_unslash( $request->get_param( 'product_type' ) ) );
		}
		if ( $request->get_param( 'type' ) ) {
			$params['type'] = sanitize_text_field( wp_unslash( $request->get_param( 'type' ) ) );
		}

		$mappings = $this->handler->get_all_by_supplier_uuid( $supplier_uuid, $params );

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

		return Response_Builder::success( $mappings );
	}

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

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

		return Response_Builder::success( $mapping );
	}

	/**
	 * Create attribute mapping 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( wp_unslash( $request->get_param( 'supplier_uuid' ) ) );
		$data          = $request->get_json_params();

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

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

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

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

		$mapping = $this->handler->update( $mapping_uuid, $data );

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

		return Response_Builder::success( $mapping );
	}

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

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

		return Response_Builder::deleted();
	}
}
