<?php

/**
 * Vvveb
 *
 * Copyright (C) 2022  Ziadin Givan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Vvveb\Controller\Content;

use \Vvveb\Sql\CategorySQL;
use function Vvveb\__;
use Vvveb\System\Images;
use function Vvveb\url;

/**
 * Provides autocomplete methods for various entity types (categories, manufacturers, etc.).
 *
 * Used by controllers that need autocomplete/search functionality.
 */
trait AutocompleteTrait {
	/**
	 * Returns category autocomplete suggestions as JSON.
	 *
	 * @return void
	 */
	public function categoriesAutocomplete(): void {
		$categories = new CategorySQL();
		$text       = trim($this->request->get['text'] ?? '');
		$type       = trim($this->request->get['type'] ?? '');
		$post_type  = $this->request->get['post_type'] ?? '';

		$results = $categories->getCategories([
			'post_type' => $post_type,
			'type'      => $type,
			'search'    => '%' . $text . '%',
		] + $this->global);

		$search = [];

		if (isset($results['categories'])) {
			foreach ($results['categories'] as $category) {
				$search[$category['taxonomy_item_id']] = $category['name'];
			}
		}

		$this->response->setType('json');
		$this->response->output($search);
	}

	/**
	 * Returns manufacturer autocomplete suggestions as JSON.
	 *
	 * @return void
	 */
	public function manufacturersAutocomplete(): void {
		$manufacturers = new \Vvveb\Sql\ManufacturerSQL();
		$text          = trim($this->request->get['text'] ?? '');

		$options = [
			'search' => '%' . $text . '%',
		] + $this->global;

		$results = $manufacturers->getAll($options);

		$search = [];

		if (isset($results['manufacturer'])) {
			foreach ($results['manufacturer'] as $manufacturer) {
				$manufacturer['image']                    = Images::image($manufacturer['image'], 'manufacturer');
				$search[$manufacturer['manufacturer_id']] = '<img width="32" height="32" src="' . $manufacturer['image'] . '"> ' . $manufacturer['name'];
			}
		}

		$this->response->setType('json');
		$this->response->output($search);
	}

	/**
	 * Returns vendor autocomplete suggestions as JSON.
	 *
	 * @return void
	 */
	public function vendorsAutocomplete(): void {
		$vendors    = new \Vvveb\Sql\VendorSQL();
		$text       = trim($this->request->get['text'] ?? '');

		$options = [
			'search' => '%' . $text . '%',
		] + $this->global;

		$results = $vendors->getAll($options);

		$search = [];

		if (isset($results['vendor'])) {
			foreach ($results['vendor'] as $vendor) {
				$vendor['image']               = Images::image($vendor['image'], 'vendor');
				$search[$vendor['vendor_id']]  = '<img width="32" height="32" src="' . $vendor['image'] . '"> ' . $vendor['name'];
			}
		}

		$this->response->setType('json');
		$this->response->output($search);
	}

	/**
	 * Returns URL autocomplete suggestions (products, posts, pages) as JSON.
	 *
	 * @return void
	 */
	public function urlAutocomplete(): void {
		$products   = new \Vvveb\Sql\ProductSQL();
		$text       = trim($this->request->get['text'] ?? '');
		$type       = $this->request->get['type'] ?? 'card'; //'key-value'

		$options = [
			'limit' => 5,
			'like'  => $text,
		] + $this->global;

		unset($options['admin_id']);
		$results = $products->getAll($options);

		$search = [];
		$pages  = [
			'index/index'             => __('Home'),
			'product/index'           => __('Shop'),
			'content'                 => __('Blog'),
			'user/index'              => __('User'),
			'user/login/index'        => __('Login'),
			'user/signup/index'       => __('Signup'),
			'cart/cart/index'         => __('Cart'),
			'checkout/checkout/index' => __('Checkout'),
		];

		foreach ($pages as $route => $name) {
			if (stripos($name, $text) !== false) {
				$url          = url($route);
				$search[$url] = $name;

				break;
			}
		}

		if (isset($results['product'])) {
			foreach ($results['product'] as $product) {
				$product['image'] = Images::image($product['image'], 'product', 'thumb');
				$url              = url('product/product/index', ['slug'=> $product['slug'], 'product_id' => $product['product_id']]);

				if ($type == 'card') {
					$search[]         = [
						'type' => 'cardimage',
						'src'  => $product['image'],
						'text' => $product['name'],
						'value'=> '<a href="' . $url . '">' . $product['name'] . '</a>',
					];
				} else {
					$search[$url] = '<img width="32" height="32" src="' . $product['image'] . '"> ' . $product['name'];
				}
			}
		}

		if (count($search) < 5) {
			$posts   = new \Vvveb\Sql\PostSQL();
			$results = $posts->getAll($options);

			if (isset($results['post'])) {
				foreach ($results['post'] as $post) {
					$post['image'] = Images::image($post['image'], 'post', 'thumb');
					$url           = url('content/post/index', ['slug'=> $post['slug'], 'post_id' => $post['post_id']]);

					if ($type == 'card') {
						$search[]      = [
							'type' => 'cardimage',
							'src'  => $post['image'],
							'text' => $post['name'],
							'value'=> '<a href="' . $url . '">' . $post['name'] . '</a>',
						];
					} else {
						$search[$url] = '<img width="32" height="32" src="' . $post['image'] . '"> ' . $post['name'];
					}
				}
			}
		}

		$this->response->setType('json');
		$this->response->output($search);
	}

	/**
	 * Returns product autocomplete suggestions as JSON.
	 *
	 * @return void
	 */
	public function productsAutocomplete(): void {
		$products   = new \Vvveb\Sql\ProductSQL();
		$text       = trim($this->request->get['text'] ?? '');

		$options = [
			'like' => $text,
		] + $this->global;

		unset($options['admin_id']);
		$results = $products->getAll($options);

		$search = [];

		if (isset($results['product'])) {
			foreach ($results['product'] as $product) {
				$product['image']                        = Images::image($product['image'], $this->object);
				$search[$product[$this->object . '_id']] = '<img width="32" height="32" src="' . $product['image'] . '"> ' . $product['name'];
			}
		}

		$this->response->setType('json');
		$this->response->output($search);
	}

	/**
	 * Returns admin user autocomplete suggestions as JSON.
	 *
	 * @return void
	 */
	public function adminsAutocomplete(): void {
		$admins     = new \Vvveb\Sql\AdminSQL();
		$text       = trim($this->request->get['text'] ?? '');

		$options = [
			'status' => 1,
			'search' => $text,
		] + $this->global;

		$results = $admins->getAll($options);

		$search = [];

		if (isset($results['admin'])) {
			foreach ($results['admin'] as $admin) {
				$text = '';

				if (isset($admin['avatar'])) {
					$admin['avatar'] = Images::image($admin['avatar'], 'admin');
					$text            =  '<img width="32" height="32" src="' . $admin['avatar'] . '"> ';
				}
				$text .= $admin['username'] . ' (' . $admin['first_name'] . ' ' . $admin['last_name'] . ')';

				$search[$admin['admin_id']] =  $text;
			}
		}

		$this->response->setType('json');
		$this->response->output($search);
	}

	/**
	 * Returns attribute autocomplete suggestions as JSON.
	 *
	 * @return void
	 */
	public function attributesAutocomplete(): void {
		$attributes = new \Vvveb\Sql\AttributeSQL();
		$text       = trim($this->request->get['text'] ?? '');

		$options = [
			'search' => $text,
		] + $this->global;

		$results = $attributes->getAll($options);

		$search = [];

		if (isset($results['attribute'])) {
			foreach ($results['attribute'] as $attribute) {
				$text = $attribute['name'] . ' (' . $attribute['group'] . ')';

				$search[$attribute['attribute_id']] =  $text;
			}
		}

		$this->response->setType('json');
		$this->response->output($search);
	}

	/**
	 * Returns option values autocomplete suggestions as JSON.
	 *
	 * @return void
	 */
	public function optionValuesAutocomplete(): void {
		$values = new \Vvveb\Sql\Option_valueSQL();

		$options = [
			'option_id'      => trim($this->request->get['option_id'] ?? ''),
		] + $this->global;

		$results = $values->getAll($options);

		$search = [];

		if (isset($results['option_value'])) {
			foreach ($results['option_value'] as $value) {
				$search[$value['option_value_id']] =  $value['name'];
			}
		}

		$this->response->setType('json');
		$this->response->output($search);
	}

	/**
	 * Returns digital asset autocomplete suggestions as JSON.
	 *
	 * @return void
	 */
	public function digitalAssetsAutocomplete(): void {
		$digital_assets = new \Vvveb\Sql\Digital_assetSQL();
		$text           = trim($this->request->get['text'] ?? '');

		$options = [
			'search' => $text,
		] + $this->global;

		$results = $digital_assets->getAll($options);

		$search = [];

		if (isset($results['digital_asset'])) {
			foreach ($results['digital_asset'] as $digital_asset) {
				$text = $digital_asset['name'] . ' (' . $digital_asset['file'] . ')';

				$search[$digital_asset['digital_asset_id']] =  $text;
			}
		}

		$this->response->setType('json');
		$this->response->output($search);
	}
}
