<?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\Controller\Base;
use Vvveb\System\Images;

/**
 * Handles listing, saving, reordering, and deleting taxonomy categories.
 */
class Categories extends Base {
	/**
	 * Deletes one or more taxonomy items by ID.
	 *
	 * @return never
	 */
	public function delete(): never {
		$taxonomy_item_id = $this->request->post['taxonomy_item_id'] ?? false;
		$categories       = new CategorySQL();

		if (is_numeric($taxonomy_item_id)) {
			$taxonomy_item_id = [$taxonomy_item_id];
		}

		if ($taxonomy_item_id && $categories->deleteTaxonomyItem(['taxonomy_item_id' => $taxonomy_item_id])) {
			echo __('Item removed!');
		}

		die();
	}

	/**
	 * Reorders taxonomy items based on submitted data.
	 *
	 * @return never
	 */
	public function reorder(): never {
		$data       = $this->request->post;
		$categories = new CategorySQL();

		//['taxonomy_items' => $data]
		if ($categories->updateTaxonomyItems($data)) {
			echo __('Items reordered!');
		}

		die();
	}

	/**
	 * Saves a new or existing taxonomy item.
	 *
	 * @return void
	 */
	public function save(): void {
		$data                = $this->request->post;
		$data['taxonomy_id'] = $data['taxonomy_id'] ?? $this->request->get['taxonomy_id'];
		$data['taxonomy_item']['taxonomy_id'] = $data['taxonomy_id'];
		$data['taxonomy_item']['sort_order'] = $data['sort_order'] ?? 0;

		$categories = new CategorySQL();
		$results    = [];
		$success    = false;

		$options = [
			'taxonomy_item_content' => $data['taxonomy_item_content'],
			'taxonomy_item'         => $data['taxonomy_item'],
		] + $this->global;

		if (isset($data['taxonomy_item_id']) && $data['taxonomy_item_id']) {
			$options['taxonomy_item_id'] = $data['taxonomy_item_id'];
			$results                     = $categories->editTaxonomyItem($options);
			$taxonomy_item_id            = $data['taxonomy_item_id'];

			if ($results) {
				$message = __('Item saved!');
				$success = true;
			}
		} else {
			$results = $categories->addTaxonomyItem($options);

			if ($results) {
				$taxonomy_item_id = $results['taxonomy_item'];
				$message = __('Item added!');
				$success = true;
			}
		}
		
		$results['message'] = $message;
		$results['success'] = $success;
		$results['taxonomy_item_id'] = $taxonomy_item_id;

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

	/**
	 * Lists all categories for a given taxonomy with multilingual support.
	 *
	 * @return void
	 */
	public function index(): void {
		$view        = $this->view;
		$categories  = new CategorySQL();

		$page        = $this->request->get['page'] ?? 1;
		$type        = $this->request->get['type'] ?? '';
		$taxonomy_id = $this->request->get['taxonomy_id'] ?? false;
		$limit       = 1000;

		$admin_path          = \Vvveb\adminPath();
		$controllerPath      = $admin_path . 'index.php?module=media/media';
		$this->view->scanUrl = "$controllerPath&action=scan";
		$this->view->uploadUrl = "$controllerPath&action=upload";

		if ($taxonomy_id) {
			$options = [
				'start'       => ($page - 1) * $limit,
				'limit'       => $limit,
				'taxonomy_id' => $taxonomy_id,
				'post_type'   => $type,
				'type'        => 'categories',
			] + $this->global;

			$view->taxonomy_id = $taxonomy_id;

			$results = $categories->getCategoriesAllLanguages($options);

			foreach ($results['categories'] as &$taxonomy_item) {
				$langs                      = $taxonomy_item['languages'] ? json_decode($taxonomy_item['languages'], true) : [];
				$taxonomy_item['languages'] = [];

				if (isset($taxonomy_item['image'])) {
					$taxonomy_item['image_url'] = Images::image($taxonomy_item['image'], 'taxonomy_item');
				}

				if ($langs) {
					foreach ($langs as $lang) {
						$taxonomy_item['languages'][$lang['language_id']] = $lang;
					}

					$taxonomy_item['name'] = $taxonomy_item['languages'][$this->global['language_id']]['name'] ?? $langs[0]['name'] ?? '';
				}
			}

			$view->set($results);
		}
	}
}
