<?php

namespace Prodigy\Includes\Api\V1;

use Prodigy\Includes\Helpers\Prodigy_Logger;
use Prodigy\Includes\Models\Prodigy_Attribute_Taxonomies;
use Prodigy\Includes\Models\Prodigy_Attributes;
use Prodigy\includes\models\Prodigy_Product_Attributes;
use Prodigy\Includes\Models\Prodigy_Taxonomies;
use WP_Http;
use WP_REST_Request;
use WP_REST_Response;

defined( 'ABSPATH' ) || exit;
/**
 * Class Prodigy_Api_Taxonomies
 */
class Prodigy_Api_Taxonomies extends Prodigy_Api_Main {

	/**
	 * Prodigy_Api_Taxonomies constructor.
	 */
	public function __construct() {
		parent::__construct();
		add_action( 'rest_api_init', array( $this, 'platform_register_route' ) );
	}

	/**
	 * Create and Update attributes and values (taxonomies and terms)
	 *
	 * @return void
	 */
	public function platform_register_route(): void {
		register_rest_route(
			get_prodigy_plugin_directory_name() . '/v1',
			self::API_TAXONOMIES_PATH,
			array(
				'methods'             => 'POST',
				'callback'            => array( $this, 'createOrUpdate' ),
				'permission_callback' => function ( WP_REST_Request $request ) {
					return $this->is_authorization( $request->get_header( 'x-Authorization' ) );
				},
				'args'                => array(
					'id'         => array(
						'default'  => null,
						'type'     => 'integer',
						'required' => true,
					),
					'name'       => array(
						'default'  => null,
						'required' => true,
						'type'     => 'string',
					),
					'slug'       => array(
						'default'  => null,
						'required' => true,
						'type'     => 'string',
					),
					'attributes' => array(
						'default'           => null,
						'required'          => true,
						'validate_callback' => array( $this, 'validate' ),
					),
				),
			)
		);

		register_rest_route(
			get_prodigy_plugin_directory_name() . '/v1',
			self::API_TAXONOMIES_PATH . '/' . '(?P<id>\d+)',
			array(
				'methods'             => 'DELETE',
				'callback'            => array( $this, 'delete' ),
				'permission_callback' => function ( WP_REST_Request $request ) {
					return $this->is_authorization( $request->get_header( 'x-Authorization' ) );
				},
				'args'                => array(
					'id'   => array(
						'default'           => null,
						'type'              => 'integer',
						'required'          => true,
						'validate_callback' => function ( $param, $request, $key ) {
							return Prodigy_Attribute_Taxonomies::remote_id_exists( $param );
						},
					),
					'slug' => array(
						'default'           => null,
						'required'          => false,
						'type'              => 'string',
						'validate_callback' => function ( $param, $request, $key ) {
							return taxonomy_exists( $param );
						},
					),
				),
			)
		);
	}

	/**
	 * @param $params
	 *
	 * @return bool
	 */
	public function validate( $params ): bool {
		$result = true;

		if (
			! isset( $params )
			|| ! is_array( $params )
		) {
			return false;
		}

		foreach ( $params as $param ) {

			if (
				! $param['id']
				|| ! $param['name']
				|| ! $param['slug']
			) {
				$result = false;
				break;
			}
		}

		return $result;
	}

	/**
	 * @param WP_REST_Request $request
	 *
	 * @return WP_REST_Response
	 */
	public function createOrUpdate( WP_REST_Request $request ): WP_REST_Response {
		$taxonomy_obj    = new Prodigy_Taxonomies();
		$attributes_obj  = new Prodigy_Attributes();
		$result_taxonomy = false;
		$status          = \WP_Http::OK;
		if ( PRODIGY_DEBUG_MODE ) {
			Prodigy_Logger::add_record( Prodigy_Logger::TYPE_INFO, 'Create attribute ' . $request['slug'] );
		}

		if ( PRODIGY_DEBUG_MODE ) {
			Prodigy_Logger::add_record( Prodigy_Logger::TYPE_INFO, 'Create attribute values ' . wp_json_encode( $request['attributes'] ) );
		}

		$taxonomy = array(
			'name' => $request['name'],
			'slug' => $request['slug'],
			'id'   => $request['id'],
		);

		if ( ! taxonomy_exists( $taxonomy['slug'] ) ) {
			$result_taxonomy = $taxonomy_obj->add( $taxonomy );
		}

		if ( is_wp_error( $result_taxonomy ) ) {
			$result_attributes[ $taxonomy['slug'] ]['success'] = false;
			$result_attributes[ $taxonomy['slug'] ]['error']   = $result_taxonomy->get_error_message();
			$status = \WP_Http::BAD_REQUEST;
		}

		$result_attributes[ $taxonomy['slug'] ]['data']    = $attributes_obj->update_attribute_options( $request['slug'], $request['attributes'] );
		$result_attributes[ $taxonomy['slug'] ]['success'] = true;

		$this->cache->reset_catalog();

		$response['data']['success']    = true;
		$response['data']['taxonomies'] = $result_attributes;

		return new WP_REST_Response( $response, $status );
	}

	/**
	 * @param WP_REST_Request $request
	 *
	 * @return WP_REST_Response
	 */
	public function delete( WP_REST_Request $request ): WP_REST_Response {
		$remote_id = $request->get_url_params()['id'];
		if ( isset( $request['slug'] ) ) {
			$slug = $request['slug'];
		} else {
			$attributes_info = Prodigy_Attribute_Taxonomies::get_by_remote_id( (int) $remote_id );
			$slug            = $attributes_info->slug;
		}

		$terms = get_terms(
			array(
				'taxonomy'   => $slug,
				'hide_empty' => false,
			)
		);

		$is_delete = Prodigy_Product_Attributes::delete_prodigy_terms( $slug );

		if ( $is_delete ) {
			$response = array();
			$status   = WP_Http::NO_CONTENT;
			$this->cache->reset_catalog();
		} else {
			$response['data']['success']              = false;
			$response['data']['slug']                 = $slug;
			$response['data']['message']              = 'Taxonomy contains values';
			$response['data']['attributes']['values'] = wp_list_pluck( $terms, 'slug' );
			$status                                   = WP_Http::UNPROCESSABLE_ENTITY;
		}

		return new WP_REST_Response( $response, $status );
	}
}
