<?php

namespace Prodigy\Includes\Content;

use Prodigy\Includes\Helpers\Prodigy_Page;
use Prodigy\Includes\Helpers\Prodigy_Url_Helper;
use Prodigy\Includes\Models\Prodigy_Attributes;
use Prodigy\includes\models\Prodigy_Product_Attributes;
use Prodigy\Includes\Models\Prodigy_Taxonomies;
use Prodigy\Includes\Prodigy;
use Prodigy\Includes\Prodigy_Elementor_Template_Loader;

defined( 'ABSPATH' ) || exit;

/**
 * Prodigy main content class
 *
 * @version    2.8.9
 */
class Prodigy_Main_Content {

	/**
	 * Elementor options from DB
	 *
	 * @var false|array
	 */
	public $elementor_options;

	/**
	 * Customizer options from DB
	 *
	 * @var false|array
	 */
	public $customizer_options;

	/**
	 * Types of related products
	 */
	const REMOTE_UP_SELL_PRODUCTS                     = 'up-sell-products';
	const REMOTE_CROSS_SELL_PRODUCTS                  = 'cross-sell-products';
	const REMOTE_TYPE_INCLUDED_UP_CROSS_SELL_PRODUCTS = 'products';
	const DEFAULT_SORT_TYPE                           = 'created_at';
	const DEFAULT_ORDER_BY_TYPE                       = 'desc';

	public function __construct() {
		$this->customizer_options = get_option( 'prodigy_shop_settings', array() );
	}

	/**
	 * @param array  $includes
	 * @param int    $id
	 * @param string $type
	 *
	 * @return array
	 */
	public function parse_included_obj_by_id( array $includes, int $id, string $type = '' ): array {
		$remote_included = $includes['included'];
		$data            = array();

		if ( empty( $remote_included ) ) {
			return $data;
		}

		foreach ( $remote_included as $remote_include ) {
			if ( ! empty( $type ) ) {
				if ( (int) $remote_include['id'] === $id && $remote_include['type'] === $type ) {
					$data = $remote_include;
				}
			} elseif ( (int) $remote_include['id'] === $id ) {
				$data = $remote_include;
			}
		}

		return $data;
	}

	/**
	 * @param array  $ids
	 * @param array  $includes
	 * @param string $type
	 *
	 * @return array
	 */
	public function parse_included_obj_by_ids( array $ids, array $includes = array(), string $type = '' ): array {
		$remote_included = $includes['included'] ?? null;
		$data            = array();

		if ( empty( $remote_included ) ) {
			return $data;
		}

		foreach ( $remote_included as $remote_include ) {
			if ( ! empty( $type ) ) {
				if ( in_array( $remote_include['id'], $ids, true ) && $remote_include['type'] === $type ) {
					$data[] = $remote_include;
				}
			} elseif ( in_array( $remote_include['id'], $ids, true ) ) {
				$data[] = $remote_include;
			}
		}

		return $data ?? array();
	}

	/**
	 * @param array $product
	 *
	 * @return array
	 */
	protected function get_up_sell_products_main( array $product ): array {
		$id_attributes = array();

		if ( ! empty( $product['data']['relationships'] ) ) {
			$data_parse = $product['data']['relationships'][ self::REMOTE_UP_SELL_PRODUCTS ];
			if ( ! empty( $data_parse['data'] ) ) {
				$id_attributes = wp_list_pluck( $data_parse['data'], 'id', 'id' );
			}
		}

		return $this->parse_included_obj_by_ids( $id_attributes, $product, self::REMOTE_TYPE_INCLUDED_UP_CROSS_SELL_PRODUCTS );
	}

	/**
	 * @param array $product
	 *
	 * @return array
	 */
	protected function get_cross_sell_products_main( array $product ): array {
		$id_attributes = array();
		if ( ! empty( $product['data']['relationships'] ) ) {
			$data_parse = $product['data']['relationships'][ self::REMOTE_CROSS_SELL_PRODUCTS ];
			if ( ! empty( $data_parse['data'] ) ) {
				$id_attributes = wp_list_pluck( $data_parse['data'], 'id', 'id' );
			}
		}

		return $this->parse_included_obj_by_ids( $id_attributes, $product, self::REMOTE_TYPE_INCLUDED_UP_CROSS_SELL_PRODUCTS );
	}

	/**
	 * @param array      $query
	 * @param array|null $options
	 *
	 * @return string
	 */
	public function set_sort_products_query_parameter( array $query, ?array $options = null ): string {
		$order    = self::DEFAULT_ORDER_BY_TYPE;
		$order_by = self::DEFAULT_SORT_TYPE;
		if ( ! isset( $query['sort'] ) ) {
			if ( isset( $options['content_archive_products_content_order_by'] ) || isset( $this->elementor_options['content_archive_products_content_order_by'] ) ) {
				$order_by = $options['content_archive_products_content_order_by'] ?? $this->elementor_options['content_archive_products_content_order_by'];
			}
			if ( isset( $options['content_archive_products_content_order'] ) || isset( $this->elementor_options['content_archive_products_content_order'] ) ) {
				$order = $options['content_archive_products_content_order'] ?? $this->elementor_options['content_archive_products_content_order'];
			}

			$query['sort'] = $this->customizer_options['prodigy_shop_default_sortable'] ?? self::mapping_order_params( $order ) . $order_by;
		}

		return build_query( $query );
	}


	/**
	 * @param string|null $order
	 *
	 * @return string
	 */
	public static function mapping_order_params( ?string $order = null ): string {
		$hsParams = array(
			'id'         => 'id',
			'created_at' => 'created_at',
			'date'       => 'created_at',
			'title'      => 'name',
			'rating'     => 'rating',
			'price'      => 'price',
			'desc'       => '-',
			'asc'        => '',
		);

		return $hsParams[ $order ] ?? '';
	}


	/**
	 * @param string $tax_name
	 * @param string $tax_slug
	 *
	 * @return string
	 */
	public function get_catalog_category_param( string $tax_name, string $tax_slug ): string {
		$category = isset( $_GET[ Prodigy::get_prodigy_category_type() ] ) ? sanitize_text_field( wp_unslash( $_GET[ Prodigy::get_prodigy_category_type() ] ) ) : '';
		$tag      = isset( $_GET[ Prodigy::get_prodigy_tag_type() ] ) ? sanitize_text_field( wp_unslash( $_GET[ Prodigy::get_prodigy_tag_type() ] ) ) : '';
		$brand    = isset( $_GET[ Prodigy::get_prodigy_brand_type() ] ) ? sanitize_text_field( wp_unslash( $_GET[ Prodigy::get_prodigy_brand_slug() ] ) ) : '';

		if ( ! empty( $category ) ) {
			$tax_name = $category;
			$tax_slug = Prodigy::get_prodigy_category_slug();
		}

		if ( ! empty( $tag ) ) {
			$tax_name = $tag;
			$tax_slug = Prodigy::get_prodigy_tag_slug();
		}

		if ( ! empty( $brand ) ) {
			$tax_name = $brand;
			$tax_slug = Prodigy::get_prodigy_brand_slug();
		}

		if ( empty( $tax_name ) && empty( $tax_slug ) ) {
			$info = wp_doing_ajax() ? wp_parse_url( $_GET['current_url'] ?? '' ) : Prodigy_Url_Helper::prodigy_parse_request_uri( -1 );

			$path = explode( '/', $info['path'] );
			foreach ( $path as $part ) {
				if (
					$part === Prodigy::get_prodigy_category_slug() ||
					$part === Prodigy::get_prodigy_tag_slug() ||
					$part === Prodigy::get_prodigy_brand_slug()
				) {
					$tax_slug = $part;
					$tax_name = true;
				}
			}

			if ( $tax_slug === Prodigy::get_prodigy_category_slug() ) {
				foreach ( $path as $part ) {
					$term = get_term_by( 'slug', $part, Prodigy::get_prodigy_category_type() );
					if ( $term ) {
						$tax_name = $part;
					}
				}
			}

			if ( $tax_slug === Prodigy::get_prodigy_tag_slug() ) {
				foreach ( $path as $part ) {
					$term = get_term_by( 'slug', $part, Prodigy::get_prodigy_tag_type() );
					if ( ! empty( $term ) ) {
						$tax_name = $part;
					}
				}
			}

			if ( $tax_slug === Prodigy::get_prodigy_brand_slug() ) {
				foreach ( $path as $part ) {
					$term = get_term_by( 'slug', $part, Prodigy::get_prodigy_brand_type() );
					if ( ! empty( $term ) ) {
						$tax_name = $part;
					}
				}
			}
		}

		// Validate taxonomy terms if we have them
		if ( is_string( $tax_name ) && ! empty( $tax_slug ) ) {
			$taxonomy_mapping = array(
				Prodigy::get_prodigy_category_slug() => Prodigy::get_prodigy_category_type(),
				Prodigy::get_prodigy_tag_slug()      => Prodigy::get_prodigy_tag_type(),
				Prodigy::get_prodigy_brand_slug()    => Prodigy::get_prodigy_brand_type(),
			);

			if ( isset( $taxonomy_mapping[ $tax_slug ] ) ) {
				$taxonomy_name = $taxonomy_mapping[ $tax_slug ];
				Prodigy_Url_Helper::validate_taxonomy_term_exists( $taxonomy_name, $tax_name );
			}
		}

		$result = '';
		if ( is_string( $tax_name ) && ! empty( $tax_slug ) ) {
			$is_prodigy_category = ( $tax_slug === Prodigy::get_prodigy_category_slug() );
			$is_prodigy_tag      = ( $tax_slug === Prodigy::get_prodigy_tag_slug() );
			$is_prodigy_brand    = ( $tax_slug === Prodigy::get_prodigy_brand_slug() );

			if ( $is_prodigy_category ) {
				$taxonomy = Prodigy_Taxonomies::get_taxonomy_by_different_slugs( Prodigy::get_prodigy_category_type(), $tax_name );
				if ( ! empty( $taxonomy ) ) {
					$termmeta = get_term_meta( $taxonomy->term_id, Prodigy::PRODIGY_HOSTED_CATEGORY_RELATION, true );
					if ( ! empty( $termmeta ) ) {
						$result = 'category_id=' . $termmeta;
					}
				}
			} elseif ( $is_prodigy_tag ) {
				$taxonomy = Prodigy_Taxonomies::get_taxonomy_by_different_slugs( Prodigy::get_prodigy_tag_type(), $tax_name );
				if ( ! empty( $taxonomy ) ) {
					$termmeta = get_term_meta( $taxonomy->term_id, Prodigy::PRODIGY_HOSTED_TAG_RELATION, true );
					if ( ! empty( $termmeta ) ) {
						$result = 'tag_id=' . $termmeta;
					}
				}
			} elseif ( $is_prodigy_brand ) {
				$taxonomy = Prodigy_Taxonomies::get_taxonomy_by_different_slugs( Prodigy::get_prodigy_brand_type(), $tax_name );
				if ( ! empty( $taxonomy ) ) {
					$slug = $taxonomy->slug;
					if ( ! empty( $slug ) ) {
						$result = 'brand=' . $slug;
					}
				}
			}
		}

		return $result;
	}


	/**
	 * @param string $query
	 * @param int    $count_items_per_page
	 *
	 * @return string
	 */
	public function catalog_query_builder( string $query, int $count_items_per_page ): string {
		$request_attr = array();
		if ( ! empty( $query ) ) {
			parse_str( urldecode( $query ), $request_attr );
		}

		$attributes = array();
		if ( ! empty( $request_attr ) ) {
			if ( ! empty( $request_attr['search'] ) ) {
				$attributes['search'] = $request_attr['search'];
			}

			if ( ! empty( $request_attr['attr'] ) ) {
				foreach ( $request_attr['attr'] as $key => $attribute ) {
					$values[ $key ] = explode( ';', $attribute );

					$attribute_values = array();
					foreach ( $values[ $key ] as $key_val => $value ) {
						$taxonomy = Prodigy_Product_Attributes::get_attribute_taxonomies_by_remote_id( $key );
						if ( isset( $taxonomy ) ) {
							$term_attribute = Prodigy_Attributes::get_attributes_by_different_slugs( $taxonomy->slug, $value );

							if ( isset( $term_attribute ) ) {
								$attribute_value_remote_id                           = get_term_meta(
									$term_attribute->term_id,
									'prodigy_attribute_value_remote_id',
									true
								);
								$attribute_values[ $key_val ]                        = $attribute_value_remote_id;
								$attributes['options_filter'][ $key ]['property_id'] = $key;
								$attributes['options_filter'][ $key ]['option_ids']  = $attribute_values;
							}
						}
					}
				}
			}
			if ( isset( $request_attr['category_id'] ) ) {
				$attributes['category_id'] = $request_attr['category_id'];
			}

			if ( isset( $request_attr['tag_id'] ) ) {
				$attributes['tag_id'] = $request_attr['tag_id'];
			}

			if ( isset( $request_attr['brand'] ) ) {
				$attributes['brand_slug'] = $request_attr['brand'];
			}

			if ( isset( $request_attr['price_min'] ) ) {
				$attributes['price']['min'] = $request_attr['price_min'];
				$attributes['price']['max'] = $request_attr['price_max'];
			}
		}

		$attributes['page']['size']   = $count_items_per_page;
		$attributes['page']['number'] = $request_attr['pg'] ?? 1;

		if ( ! empty( $request_attr['sort'] ) ) {
			$patterns[0]        = '/_asc/';
			$patterns[1]        = '/_desc/';
			$replacements       = '';
			$sort               = preg_replace( $patterns, $replacements, $request_attr['sort'] );
			$sort               = str_replace( 'title', 'name', $sort );
			$attributes['sort'] = $sort;
		}

		$attributes['sync'] = true;

		if ( ! Prodigy_Page::prodigy_is_frontend() ) {
			$attributes['admin'] = true;
		}

		$query_string = http_build_query( $attributes );

		return urldecode( preg_replace( '/%5B(?:[0-9]|[1-9][0-9]+)%5D/', '[]', $query_string ) );
	}
}
