<?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_Batch_Taxonomies
 */
class Prodigy_Api_Batch_Taxonomies extends Prodigy_Api_Main {

	/**
	 * @var Prodigy_Taxonomies
	 */
	public Prodigy_Taxonomies $model;

	public const BATCHES_TAXONOMIES = 'taxonomies/batches';

	/**
	 * Prodigy_Api_Taxonomies constructor.
	 */
	public function __construct() {
		parent::__construct();
		$this->model = new Prodigy_Taxonomies();
		add_action( 'rest_api_init', array( $this, 'platform_register_route_batches_tax' ) );
	}

	/**
	 * Register REST API routes
	 */
	public function platform_register_route_batches_tax(): void {
		register_rest_route(
			get_prodigy_plugin_directory_name() . '/v1',
			'taxonomies/batches',
			array(
				'methods'             => 'POST',
				'callback'            => array( $this, 'createOrUpdate' ),
				'permission_callback' => function ( WP_REST_Request $request ) {
					if ( PRODIGY_DEBUG_MODE ) {
						Prodigy_Logger::add_record(
							Prodigy_Logger::TYPE_ERROR,
							__LINE__ . __METHOD__ . __CLASS__,
							get_prodigy_plugin_directory_name() . '/v1/taxonomies/batches',
							$request
						);
					}
					return $this->is_authorization( $request->get_header( 'x-Authorization' ) );
				},
				'args'                => array(
					'taxonomies' => array(
						'default'           => null,
						'type'              => 'array',
						'required'          => true,
						'validate_callback' => array( $this->model, 'validate_taxonomies_for_create' ),
					),
				),
			)
		);

		register_rest_route(
			get_prodigy_plugin_directory_name() . '/v1',
			self::BATCHES_TAXONOMIES,
			array(
				'methods'             => 'DELETE',
				'callback'            => array( $this, 'delete' ),
				'permission_callback' => function ( WP_REST_Request $request ) {
					return $this->is_authorization( $request->get_header( 'x-Authorization' ) );
				},
			)
		);
	}

	/**
	 * @param WP_REST_Request $request
	 * @return WP_REST_Response
	 */
	public function delete( WP_REST_Request $request ): WP_REST_Response {
		$status   = WP_Http::NO_CONTENT;
		$response = array();
		if ( isset( $request['id'] ) ) {
			$taxonomies = $request['id'];

			foreach ( $taxonomies as $key => $taxonomy_id ) {
				if ( isset( $request['slug'] ) ) {
					$slug = $request['slug'];
				} else {
					$attributes_info = Prodigy_Attribute_Taxonomies::get_by_remote_id( (int) $taxonomy_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 ) {
					$this->cache->reset_catalog();
					$response = array();
				} else {
					$response[ $key ]['data']['success']              = false;
					$response[ $key ]['data']['slug']                 = $slug;
					$response[ $key ]['data']['message']              = 'Taxonomy contains values';
					$response[ $key ]['data']['attributes']['values'] = wp_list_pluck( $terms, 'slug' );
					$status[ $key ]                                   = \WP_Http::UNPROCESSABLE_ENTITY;
				}
			}
		} else {
			$response['data']['error']   = 'Bad request';
			$response['data']['success'] = false;
			$status                      = \WP_Http::BAD_REQUEST;
		}

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


	/**
	 * @param WP_REST_Request $request
	 *
	 * @return WP_REST_Response
	 */
	public function createOrUpdate( WP_REST_Request $request ): WP_REST_Response {
		$status     = \WP_Http::CREATED;
		$taxonomies = $request['taxonomies'];

		$attributes_obj = new Prodigy_Attributes();

		$result_attributes = array();

		foreach ( $taxonomies as $taxonomy ) {
			$result_taxonomy = false;

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

			if ( ! is_wp_error( $result_taxonomy ) ) {
				$result_attributes[ $taxonomy['slug'] ]['data']    = $attributes_obj->add_collection( $taxonomy['slug'], $taxonomy['attributes'] );
				$result_attributes[ $taxonomy['slug'] ]['success'] = true;
			} else {
				$result_attributes[ $taxonomy['slug'] ]['success'] = false;
				$result_attributes[ $taxonomy['slug'] ]['error']   = $result_taxonomy->get_error_message();
				$status = WP_Http::BAD_REQUEST;
			}
		}

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

		$this->cache->reset_catalog();

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