<?php
/**
 * Sync 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\Sync_Handler;
use MOWCPS\Common\Utils\Response_Builder;
use MOWCPS\Common\Constants\Constants;

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

/**
 * Class Sync_Rest_Controller
 *
 * Handles sync REST API callbacks
 */
class Sync_Rest_Controller extends Base_Rest_Controller {
	/**
	 * Handler instance
	 *
	 * @var Sync_Handler
	 */
	private $handler;

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

	/**
	 * Start sync
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function start_sync( $request ) {
		$supplier_uuid    = sanitize_key( wp_unslash( $request->get_param( 'supplier_uuid' ) ) );
		$params           = $request->get_params();
		$params['action'] = self::extract_action_from_url( $request );

		$result = $this->handler->start_sync( $supplier_uuid, $params );
		if ( is_wp_error( $result ) ) {
			return Response_Builder::from_wp_error( $result );
		}

		return Response_Builder::success( $result );
	}

	/**
	 * Extract action from URL path
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return string Action extracted from URL.
	 */
	private static function extract_action_from_url( $request ) {
		$route      = $request->get_route();
		$path_parts = explode( '/', trim( $route, '/' ) );

		// Find the action in the path: /suppliers/{uuid}/sync/{action}.
		foreach ( $path_parts as $index => $part ) {
			if ( 'sync' === $part && isset( $path_parts[ $index + 1 ] ) ) {
				return $path_parts[ $index + 1 ];
			}
		}

		return Constants::SYNC_ACTION_START;
	}

	/**
	 * Pause sync
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function pause_sync( $request ) {
		// Action is extracted from URL: /suppliers/{uuid}/sync/pause.
		$supplier_uuid = sanitize_key( wp_unslash( $request->get_param( 'supplier_uuid' ) ) );
		$params        = $request->get_params();
		$result        = $this->handler->pause_sync( $supplier_uuid, $params );

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

		return Response_Builder::success( $result );
	}

	/**
	 * Resume sync
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function resume_sync( $request ) {
		// Action is extracted from URL: /suppliers/{uuid}/sync/resume.
		$supplier_uuid = sanitize_key( wp_unslash( $request->get_param( 'supplier_uuid' ) ) );
		$params        = $request->get_params();
		$result        = $this->handler->resume_sync( $supplier_uuid, $params );

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

		return Response_Builder::success( $result );
	}

	/**
	 * Abort sync
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function abort_sync( $request ) {
		// Action is extracted from URL: /suppliers/{uuid}/sync/abort.
		$supplier_uuid = sanitize_key( wp_unslash( $request->get_param( 'supplier_uuid' ) ) );
		$params        = $request->get_params();
		$result        = $this->handler->abort_sync( $supplier_uuid, $params );

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

	/**
	 * Get attribute mappings
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error
	 */
	public function get_attribute_mappings( $request ) {
		$supplier_uuid = sanitize_key( wp_unslash( $request->get_param( 'supplier_uuid' ) ) );
		$params        = $request->get_params();
		$result        = $this->handler->get_attribute_mappings( $supplier_uuid, $params );

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

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