<?php
/**
 * Auto Attribute Mapping Handler
 *
 * @category   WordPress_Plugin
 * @package    Common
 * @subpackage Handlers\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\Handlers\Rest_API;

use WP_Error;
use MOWCPS\Common\Models\Connection_Model;
use MOWCPS\Common\Models\Supplier_Model;
use MOWCPS\Common\Constants\Constants;
use MOWCPS\Common\Services\Data_Mapping\Auto_Attribute_Mapping_Service;
use MOWCPS\Common\DTO\Connection_Request;

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

/**
 * Class Auto_Attribute_Mapping_Handler
 *
 * Handles auto attribute mapping REST API operations
 */
class Auto_Attribute_Mapping_Handler {

	/**
	 * Get array and object attributes
	 *
	 * @param string $connection_uuid Connection UUID.
	 * @param string $call_type Call type.
	 * @param string $required_data_type Required data type.
	 * @param string $order_req_attributes Database attributes.
	 * @return array|WP_Error
	 */
	public function get_array_object_attributes( $connection_uuid, $call_type = '', $required_data_type = '', $order_req_attributes = '' ) {
		$validation = Connection_Request::validate_uuid( $connection_uuid, 'connection_uuid' );
		if ( is_wp_error( $validation ) ) {
			return $validation;
		}

		$connection = Connection_Model::get_by_uuid( $connection_uuid );
		if ( ! $connection ) {
			return new WP_Error(
				Constants::ERROR_CONNECTION_NOT_FOUND,
				Constants::MSG_CONNECTION_NOT_FOUND,
				array( 'status' => Constants::HTTP_NOT_FOUND )
			);
		}

		return Auto_Attribute_Mapping_Service::get_array_and_object_attributes( $connection, $call_type, $required_data_type, $order_req_attributes );
	}

	/**
	 * Get variable attributes
	 *
	 * @param string $connection_uuid Connection UUID.
	 * @param string $prefix Prefix.
	 * @param string $call_type Call type.
	 * @return array|WP_Error
	 */
	public function get_variable_attributes( $connection_uuid, $prefix = '', $call_type = '' ) {

		$connection = Connection_Model::get_by_uuid( $connection_uuid );
		if ( ! $connection ) {
			return new WP_Error(
				Constants::ERROR_CONNECTION_NOT_FOUND,
				Constants::MSG_CONNECTION_NOT_FOUND,
				array( 'status' => Constants::HTTP_NOT_FOUND )
			);
		}

		return Auto_Attribute_Mapping_Service::get_variable_attributes( $prefix, $connection['id'], $call_type );
	}

	/**
	 * Get child attributes
	 *
	 * @param string $connection_uuid Connection UUID.
	 * @param string $prefix Prefix.
	 * @param string $attribute_type Attribute type.
	 * @param string $call_type Call type.
	 * @param bool   $is_filter Whether to filter attributes.
	 * @param array  $options Additional options.
	 * @return array|WP_Error
	 */
	public function get_child_attributes( $connection_uuid, $prefix = '', $attribute_type = '', $call_type = '', $is_filter = false, $options = array() ) {
		$validation = Connection_Request::validate_uuid( $connection_uuid, 'connection_uuid' );

		if ( is_wp_error( $validation ) ) {
			return $validation;
		}

		$connection = Connection_Model::get_by_uuid( $connection_uuid );

		if ( ! $connection ) {
			return new WP_Error(
				Constants::ERROR_CONNECTION_NOT_FOUND,
				Constants::MSG_CONNECTION_NOT_FOUND,
				array( 'status' => Constants::HTTP_NOT_FOUND )
			);
		}

		return Auto_Attribute_Mapping_Service::get_child_attributes( $prefix, $connection['id'], $attribute_type, $call_type, $is_filter, $options );
	}
}
