<?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;



/**
 * Convert a date to a human-friendly relative format.
 *
 * @param string|int $date Date string or Unix timestamp
 *
 * @return string|int Human-friendly date string, or original timestamp if too far in the future
 */

function friendlyDate(string|int $date): string|int {
	$fileformats = [
		1          => static fn ($i) => __('%d second', '%d seconds', $i),
		60         => static fn ($i) => __('%d minute', '%d minutes', $i),
		3600       => static fn ($i) => __('%d hour', '%d hours', $i),
		86400      => static fn ($i) => __('%d day', '%d days', $i),
		604800     => static fn ($i) => __('%d week', '%d weeks', $i),
		2592000    => static fn ($i) => __('%d month', '%d months', $i),
		31536000   => static fn ($i) => __('%d year', '%d years', $i),
		315360000  => static fn ($i) => __('%d decade', '%d decades', $i),
		3153600000 => static fn ($i) => __('%d century', '%d centuries', $i),
	];

	$time_direction = __(' ago');

	if (is_string($date)) {
		$timestamp = strtotime($date);

		if ($timestamp === false) {
			return $date;
		}

		$date = $timestamp;
	}

	$diff           = time() - $date + 10;

	if ($diff < 0) {
		$time_direction = __(' from now');
		$diff           = abs($diff);
	}

	$lastTime = 1;
	//keep the closure, it is called with the computed unit count below
	$lastText = $fileformats[1];

	foreach ($fileformats as $time => $text) {
		if ($diff < $time) {
			$units = floor($diff / $lastTime);

			return sprintf($lastText($units), $units) . $time_direction;
		}
		$lastText = $text;
		$lastTime = $time;
	}

	return $date;
}



/**
 * Returns Bootstrap CSS class for order status badge.
 *
 * @param  int    $order_status_id Order status ID (1-7)
 * @return string CSS class string for the badge
 */

function orderStatusBadgeClass(int $order_status_id = 1): string {
	$classes = [
		1 => 'bg-primary-subtle text-body', //pending
		2 => 'bg-info-subtle text-body', //processing
		3 => 'bg-primary', //processed
		4 => 'bg-success', //complete
		5 => 'bg-danger', //canceled
		6 => 'bg-secondary', //archived
		7 => 'bg-warning', //requires_action
	];

	return $classes[$order_status_id] ?? 'bg-warning text-dark';
}



/**
 * Returns Bootstrap CSS class for payment status badge.
 *
 * @param  int    $payment_status_id Payment status ID (1-10)
 * @return string CSS class string for the badge
 */

function paymentStatusBadgeClass(int $payment_status_id = 1): string {
	$classes = [
		1  => 'bg-warning-subtle text-body', //not_paid
		2  => 'bg-success-subtle text-body', //awaiting
		3  => 'bg-danger-subtle text-body', //captured
		4  => 'bg-success', //paid
		5  => 'bg-danger', //canceled
		6  => 'bg-danger', //refunded
		7  => 'bg-danger', //partially_refunded
		8  => 'bg-danger', //chargeback
		9  => 'bg-danger', //requires_action
		10 => 'bg-danger', //fraud
	];

	return $classes[$payment_status_id] ?? 'bg-warning text-dark';
}



/**
 * Returns Bootstrap CSS class for shipping status badge.
 *
 * @param  int    $shipping_status_id Shipping status ID (1-10)
 * @return string CSS class string for the badge
 */

function shippingStatusBadgeClass(int $shipping_status_id = 1): string {
	$classes = [
		1  => 'bg-warning-subtle text-body', //not_fulfilled
		2  => 'bg-success-subtle text-body', //fulfilled
		3  => 'bg-success-subtle text-body', //partially_fulfilled
		4  => 'bg-success', //shipped
		5  => 'bg-success-subtle text-body', //partially_shipped
		6  => 'bg-danger-subtle text-body', //returned
		7  => 'bg-danger-subtle text-body', //partially_returned
		8  => 'bg-success', //delivered
		9  => 'bg-danger', //canceled
		10 => 'bg-warning', //requires_action
	];

	return $classes[$shipping_status_id] ?? 'bg-warning text-dark';
}



/**
 * Returns Bootstrap CSS class for comment status badge.
 *
 * @param  int    $status Comment status ID (0-3)
 * @return string CSS class string for the badge
 */

function commentStatusBadgeClass(int $status = 0): string {
	$classes = [
		0 => 'bg-primary-subtle text-body', //pending
		1 => 'bg-success-subtle text-body', //approved
		2 => 'bg-warning-subtle text-body', //spam
		3 => 'bg-danger-subtle text-body', //trash
	];

	return $classes[$status] ?? 'bg-secondary-subtle text-dark';
}



/**
 * Return all available field type definitions.
 *
 * @return array Associative array of field types grouped by category.
 */

function getFieldTypes(): array {
	return
	[
		'basic' => [
			'text' => [
				'name' => __('Text'),
				'icon' => 'icon-text-outline',
			],
			'textarea' => [
				'name' => __('Textarea'),
				'icon' => 'icon-document-text-outline',
			],
			'number' => [
				'name' => __('Number'),
				'icon' => 'icon-document-text-outline',
			],
			'range' => [
				'name' => __('Range'),
				'icon' => 'icon-git-commit-outline',
			],
			'email' => [
				'name' => __('Email'),
				'icon' => 'icon-mail-outline',
			],
			'url' => [
				'name' => __('Url'),
				'icon' => 'icon-link-outline',
			],
			'tel' => [
				'name' => __('Telephone'),
				'icon' => 'icon-call-outline',
			],
			'password' => [
				'name' => __('Password'),
				'icon' => 'icon-lock-open-outline',
			],
			'select' => [
				'name' => __('Select'),
				'icon' => 'icon-chevron-down-circle-outline',
			],
		],
		'content' => [
			'image'   => [
				'name' => __('Image'),
				'icon' => 'icon-image-outline',
			],
			'file'    => [
				'name' => __('File'),
				'icon' => 'icon-document-outline',
			],
			'wysiwyg' => [
				'name' => __('Wysiwyg Editor'),
				'icon' => 'icon-create-outline',
			],
			'oembed'  => [
				'name' => __('oEmbed'),
				'icon' => 'icon-videocam-outline',
			],
			'gallery'    => [
				'name' => __('Gallery'),
				'icon' => 'icon-images-outline',
			],
		],
		'choice' => [
			'select' => [
				'name' => __('Select'),
				'icon' => 'icon-chevron-down-circle-outline',
			],
			'checkbox' => [
				'name' => __('Checkbox'),
				'icon' => 'icon-checkbox-outline',
			],
			'radio' => [
				'name' => __('Radio Button'),
				'icon' => 'icon-checkmark-circle-outline',
			],
			'toggle' => [
				'name' => __('Toggle'),
				'icon' => 'icon-toggle-outline',
			],
		],
		'relational' => [
			'link' => [
				'name' => __('Link'),
				'icon' => 'icon-link-outline',
			],
			'post' => [
				'name' => __('Post'),
				'icon' => 'icon-document-text-outline',
			],
			'product' => [
				'name' => __('Product'),
				'icon' => 'icon-cube-outline',
			],
			'relationship' => [
				'name' => __('Relationship'),
				'icon' => 'icon-link-outline',
			],
			'taxonomy' => [
				'name' => __('Taxonomy'),
				'icon' => 'icon-apps-outline',
			],
			'user' => [
				'name' => __('User'),
				'icon' => 'icon-person-outline',
			],
		],
		'widgets' => [
			'google_map' => [
				'name' => __('Google Map'),
				'icon' => 'icon-map-outline',
			],
			'color' => [
				'name' => __('Color'),
				'icon' => 'icon-color-palette-outline',
			],
			'time' => [
				'name' => __('Time'),
				'icon' => 'icon-time-outline',
			],
			'date' => [
				'name' => __('Date'),
				'icon' => 'icon-today-outline',
			],
			'date_time' => [
				'name' => __('Date time'),
				'icon' => 'icon-calendar-clear-outline',
			],
			'month' => [
				'name' => __('Month'),
				'icon' => 'icon-calendar-outline',
			],
			'week' => [
				'name' => __('week'),
				'icon' => 'icon-calendar-number-outline',
			],
		],
		'advanced' => [
			'message' => [
				'name' => __('Message'),
				'icon' => 'icon-chevron-down-circle-outline',
			],
			'accordion' => [
				'name' => __('Accordion'),
				'icon' => 'icon-chevron-down-circle-outline',
			],
			'tab' => [
				'name' => __('Tab'),
				'icon' => 'icon-chevron-down-circle-outline',
			],
		],
	];
}



/**
 * Return the fully qualified class name for a field type.
 *
 * @param string $fieldType The field type identifier.
 *
 * @return string|null The fully qualified class name, or null if not found.
 */

function fieldTypeClass(string $fieldType): ?string {
	$field      = ucfirst($fieldType);
	$fieldClass = "Vvveb\System\Fields\\$field";

	if (! class_exists($fieldClass)) {
		return null;
	}

	return $fieldClass;
}



/**
 * Returns available post types for a given type category.
 *
 * @param  string $type The type category (e.g. 'post', 'product').
 * @return array
 */

function postTypes(string $type): array {
	$defaultTypes = [
		'post' => [
			'post' => [
				'name'    => 'Post',
				'source'  => 'default',
				'site_id' => '0',
				'type'    => 'post',
				'plural'  => 'posts',
				'icon'    => 'icon-document-text-outline',
				'comments'=> true,
			],
			'page' => [
				'name'    => 'Page',
				'source'  => 'default',
				'site_id' => '0',
				'type'    => 'page',
				'plural'  => 'pages',
				'icon'    => 'icon-document-outline',
				'comments'=> false,
			],
		],
		'product' => [
			'product' => [
				'name'    => 'Product',
				'type'    => 'product',
				'site_id' => '0',
				'source'  => 'default',
				'plural'  => 'products',
				'icon'    => 'icon-cube-outline',
			],
		],
	];

	$default               = $defaultTypes[$type] ?? [];
	$typeName              = ucfirst($type);
	list($pluginTypes)     = System\Event::trigger('Vvveb\postTypes', "custom$typeName", []);
	array_walk($pluginTypes, function (&$type,$key) {
		$type['source'] = 'plugin';
		$type['name'] = ucfirst($key);
	});

	$userTypes = getSetting($type, 'types', []);

	$params            = ['module' => "settings/{$type}-type"];
	$paramsList        = ['module' => "settings/{$type}-types"];

	array_walk($userTypes, function (&$type,$key) use ($params, $paramsList) {
		$type['source'] = 'user';
		$type['name'] = ucfirst($key);
		$type['url']        = \Vvveb\url($params + ['type' => $type['type']]);
		$type['delete-url'] = \Vvveb\url($paramsList + ['action' => 'delete', 'type[]' => $type['type']]);
	});

	$types = $default + $pluginTypes + $userTypes;

	return $types;
}

