<?php

declare(strict_types=1);

namespace mirzaev\huesos\models;

// Files of the project
use mirzaev\huesos\models\core,
	mirzaev\huesos\models\traits\document as document_trait,
	mirzaev\huesos\models\interfaces\document as document_interface,
	mirzaev\huesos\models\interfaces\collection as collection_interface,
	mirzaev\huesos\models\enumerations\language;

// Framework for ArangoDB
use mirzaev\arangodb\collection,
	mirzaev\arangodb\document;

// Library для ArangoDB
use ArangoDBClient\Document as _document;

// Built-in libraries
use exception,
	datetime;

/**
 * Model of a suspension
 *
 * @package mirzaev\huesos\models
 *
 * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
 * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
 */
final class suspension extends core implements document_interface, collection_interface
{
	use document_trait;

	/**
	 * Name of the collection in ArangoDB
	 */
	final public const string COLLECTION = 'suspension';

	/**
	 * Search for active suspension
	 *
	 * @param array &$errors Registry of errors
	 *
	 * @return static|null Object implements the instance of suspension from ArangoDB
	 */
	public static function search(array &$errors = []): static|null
	{
		try {
			if (collection::initialize(static::COLLECTION, static::TYPE, errors: $errors)) {
				// Initialized the collection

				// Search for active suspension
				$result = collection::execute(
					<<<'AQL'
					FOR d IN @@collection 
						FILTER d.end > @time 
						SORT d.end DESC 
						LIMIT 1 
						RETURN d
					AQL,
					[
						'@collection' => static::COLLECTION,
						'time' => time()
					],
					errors: $errors
				);

				if ($result instanceof _document) {
					// Found active settings

					// Initializing the object
					$suspension = new static;

					if (method_exists($suspension, '__document')) {
						// Object can implement a document from ArangoDB

						// Writing the instance of suspension document from ArangoDB to the implement object
						$suspension->__document($result);

						// Exit (success)
						return $suspension;
					} else throw new exception('Class ' . static::class . ' does not implement a document from ArangoDB');
				} else return null;
			} else throw new exception('Failed to initialize ' . static::TYPE . ' collection: ' . static::COLLECTION);
		} catch (exception $e) {
			// Writing to the registry of errors
			$errors[] = [
				'text' => $e->getMessage(),
				'file' => $e->getFile(),
				'line' => $e->getLine(),
				'stack' => $e->getTrace()
			];
		}

		// Exit (fail)
		return null;
	}

	/**
	 * Generate message about remaining time
	 *
	 * @param language|null $language Language of the generated text (otherwise used from settings.language)
	 * @param array &$errors Registry of errors
	 *
	 * @return string|null Text: "? days, ? hours and ? minutes"
	 */
	public function message(?language $language = language::en, array &$errors = []): ?string
	{
		try {
			// Initializing the time until the suspension ends
			$difference = date_diff(new datetime('@' . $this->document->end), new datetime());

			// Generate text about remaining time and exit (success)
			return sprintf(
				'%u %s, %u %s и %u %s',
				$difference->d,
				match ($difference->d > 20 ? $difference->d % 10 : $difference->d % 100) {
					1 => match ($language) {
						language::en => 'day',
						language::ru => 'день',
						default => 'day'
					},
					2, 3, 4 => match ($language) {
						language::en => 'days',
						language::ru => 'дня',
						default => 'days'
					},
					default => match ($language) {
						language::en => 'days',
						language::ru => 'дней',
						default => 'days'
					}
				},
				$difference->h,
				match ($difference->h > 20 ? $difference->h % 10 : $difference->h % 100) {
					1 => match ($language) {
						language::en => 'hours',
						language::ru => 'час',
						default => 'hour'
					},
					2, 3, 4 => match ($language) {
						language::en => 'hours',
						language::ru => 'часа',
						default => 'hours'
					},
					default => match ($language) {
						language::en => 'hours',
						language::ru => 'часов',
						default => 'hours'
					}
				},
				$difference->i,
				match ($difference->i > 20 ? $difference->i % 10 : $difference->i % 100) {
					1 => match ($language) {
						language::en => 'minute',
						language::ru => 'минута',
						default => 'minute'
					},
					2, 3, 4 => match ($language) {
						language::en => 'minutes',
						language::ru => 'минуты',
						default => 'minutes'
					},
					default => match ($language) {
						language::en => 'minutes',
						language::ru => 'минут',
						default => 'minutes'
					}
				}
			);
		} catch (exception $e) {
			// Writing to the registry of errors
			$errors[] = [
				'text' => $e->getMessage(),
				'file' => $e->getFile(),
				'line' => $e->getLine(),
				'stack' => $e->getTrace()
			];
		}

		// Exit (fail)
		return null;
	}
}
