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

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

/**
 * Class Auto_Attribute_Mapping_Rest_Controller
 *
 * Handles REST API requests for auto attribute mapping
 */
class Auto_Attribute_Mapping_Rest_Controller extends Base_Rest_Controller {

	/**
	 * Handler instance
	 *
	 * @var Auto_Attribute_Mapping_Handler
	 */
	private $handler;

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

	/**
	 * Get array and object attributes
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function get_array_object_attributes( $request ) {
		$connection_uuid      = sanitize_key( wp_unslash( $request->get_param( 'connection_uuid' ) ) );
		$call_type            = sanitize_text_field( wp_unslash( $request->get_param( 'call_type' ) ?? '' ) );
		$required_data_type   = sanitize_text_field( wp_unslash( $request->get_param( 'required_data_type' ) ?? '' ) );
		$order_req_attributes = $request->get_json_params();

		$result = $this->handler->get_array_object_attributes( $connection_uuid, $call_type, $required_data_type, $order_req_attributes );

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

		return $result;
	}

	/**
	 * Get variable attributes
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function get_variable_attributes( $request ) {
		$connection_uuid = sanitize_key( wp_unslash( $request->get_param( 'connection_uuid' ) ) );
		$prefix          = sanitize_text_field( wp_unslash( $request->get_param( 'prefix' ) ?? '' ) );
		$call_type       = sanitize_text_field( wp_unslash( $request->get_param( 'call_type' ) ?? '' ) );

		$result = $this->handler->get_variable_attributes( $connection_uuid, $prefix, $call_type );

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

		return $result;
	}

	/**
	 * Get child attributes
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function get_child_attributes( $request ) {
		$connection_uuid = sanitize_key( wp_unslash( $request->get_param( 'connection_uuid' ) ) );
		$prefix          = sanitize_text_field( wp_unslash( $request->get_param( 'prefix' ) ?? '' ) );
		$attribute_type  = sanitize_text_field( wp_unslash( $request->get_param( 'attribute_type' ) ?? '' ) );
		$call_type       = sanitize_text_field( wp_unslash( $request->get_param( 'call_type' ) ?? '' ) );
		$is_filter       = (bool) $request->get_param( 'is_filter' );

		$options = array(
			'order_req_attributes' => $request->get_json_params() ?? '',
			'required_data_type'   => sanitize_text_field( wp_unslash( $request->get_param( 'required_data_type' ) ?? '' ) ),
		);

		$result = $this->handler->get_child_attributes( $connection_uuid, $prefix, $attribute_type, $call_type, $is_filter, $options );

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

		return $result;
	}
}
