<?php
/**
 * Preview 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\Preview_Handler;
use MOWCPS\Common\Utils\Response_Builder;

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

/**
 * Class Preview_Rest_Controller
 *
 * Handles preview REST API callbacks
 */
class Preview_Rest_Controller extends Base_Rest_Controller {
	/**
	 * Handler instance
	 *
	 * @var Preview_Handler
	 */
	private $handler;

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

	/**
	 * Get preview products from supplier
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function get_preview_products( $request ) {
		$supplier_uuid = sanitize_key( wp_unslash( $request->get_param( 'supplier_uuid' ) ) );
		$params        = array();

		// Get pagination parameters.
		if ( $request->get_param( 'page' ) ) {
			$params['page'] = intval( $request->get_param( 'page' ) );
		}

		if ( $request->get_param( 'per_page' ) ) {
			$params['per_page'] = intval( $request->get_param( 'per_page' ) );
		}

		// Get attribute mapping IDs.
		if ( $request->get_param( 'attribute_mapping_ids' ) ) {
			$params['attribute_mapping_ids'] = array_map( 'sanitize_text_field', (array) $request->get_param( 'attribute_mapping_ids' ) );
		}

		$result = $this->handler->get_preview_products( $supplier_uuid, $params );

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

		return Response_Builder::success( $result );
	}

	/**
	 * Save preview filter
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function save_preview_filter( $request ) {
		$supplier_uuid = sanitize_key( wp_unslash( $request->get_param( 'supplier_uuid' ) ) );
		$data          = $request->get_json_params();

		$result = $this->handler->save_preview_filter( $supplier_uuid, $data );

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

		return Response_Builder::success( $result );
	}

	/**
	 * Get existing preview filter
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function get_preview_filter( $request ) {
		$supplier_uuid = sanitize_key( wp_unslash( $request->get_param( 'supplier_uuid' ) ) );

		$result = $this->handler->get_preview_filter( $supplier_uuid );

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

		return Response_Builder::success( $result );
	}

	/**
	 * Delete preview filter
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function delete_preview_filter( $request ) {
		$supplier_uuid = sanitize_key( wp_unslash( $request->get_param( 'supplier_uuid' ) ) );

		$result = $this->handler->delete_preview_filter( $supplier_uuid );

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

		return Response_Builder::success( $result );
	}

	/**
	 * Delete preview cache only (keep filter in DB)
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function delete_preview_cache( $request ) {
		$supplier_uuid = sanitize_key( wp_unslash( $request->get_param( 'supplier_uuid' ) ) );

		$result = $this->handler->delete_preview_cache( $supplier_uuid );

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

		return Response_Builder::success( $result );
	}
}
