<?php

declare(strict_types=1);

namespace kodorvan\neurobot\models;

// Files of the project
use kodorvan\neurobot\models\core,
	kodorvan\neurobot\models\chat;

// Svoboda time
use svoboda\time\statement as svoboda;

// Baza database
use mirzaev\baza\database,
	mirzaev\baza\column,
	mirzaev\baza\record,
	mirzaev\baza\enumerations\encoding,
	mirzaev\baza\enumerations\type;

// Active Record pattern
use mirzaev\record\interfaces\record as record_interface,
	mirzaev\record\traits\record as record_trait;

// Library for neural networks support
use mirzaev\neuroseti\network;

// Built-in libraries
use Exception as exception,
	RuntimeException as exception_runtime;

/**
 * Message
 *
 * @package kodorvan\neurobot\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 message extends core implements record_interface
{
	use record_trait;

	/**
	 * File
	 *
	 * @var string $database Path to the database file
	 */
	protected string $file = DATABASES . DIRECTORY_SEPARATOR . 'messages.baza';

	/**
	 * Database
	 *
	 * @var database $database The database
	 */
	public protected(set) database $database;

	/**
	 * Serialized
	 *
	 * @var bool $serialized Is the implementator object serialized?
	 */
	private bool $serialized = true;

	/**
	 * Separator symbol
	 *
	 * @see https://en.wikipedia.org/wiki/Comma-separated_values CSV
	 *
	 * @var string $separator
	 */
	private string $separator = ';';

	/**
	 * Constructor
	 *
	 * @method record|null $record The record
	 *
	 * @return void
	 */
	public function __construct(?record $record = null)
	{
		// Initializing the database
		$this->database = new database()
			->encoding(encoding::utf8)
			->columns(
				new column('identifier', type::long_long_unsigned),
				new column('telegram_identifier', type::long_long_unsigned),
				new column('chat', type::long_long_unsigned),
				new column('from', type::long_long_unsigned),
				new column('to', type::long_long_unsigned),
				new column('reply', type::long_long_unsigned),
				new column('text', type::string, ['length' => 4096]),
				// CSV 128 * 10 images
				new column('images', type::string, ['length' => 1280]),
				new column('system', type::char),
				new column('updated', type::integer_unsigned),
				new column('created', type::integer_unsigned)
			)
			->connect($this->file);

		// Initializing the record
		$record instanceof record and $this->record = $record;
	}

	/**
	 * Write
	 *
	 * @param int $telegram_identifier Identifier of the telegram message
	 * @param int $chat Identifier of the chat
	 * @param int $from Identifier of the telegram sender
	 * @param int $to Identifier of the telegram receiver
	 * @param int $reply Identifier of the Telegram message being responded to
	 * @param string $text Text of the message
	 * @param array $images The message images URL`s (example: ['/images/1.png', '/images/2.jpg'])
	 * @param bool $system Is the system message?
	 *
	 * @return record|false The created record
	 */
	public function write(
		int $telegram_identifier,
		int $chat,
		int $from,
		int $to,
		int $reply = 0,
		?string $text = null,
		array $images = [],
		bool $system = false,
	): record|false {
		$record = $this->database->record(
			$this->database->count() + 1,
			$telegram_identifier,
			$chat,
			$from,
			$to,
			$reply,
			$text ?? '',
			implode(';', $images),
			(int) $system,
			svoboda::timestamp(),
			svoboda::timestamp()
		);

		// Writing the record into the database
		$created = $this->database->write($record);

		// Exit (success)
		return $created ? $record : false;
	}

	/**
	 * Serialize
	 *
	 * @return self The instance from which the method was called (fluent interface)
	 */
	public function serialize(): self
	{
		if ($this->serialized) {
			// The record implementor is serialized

			// Exit (fail)
			throw new exception_runtime('The record implementator object is already serialized');
		}

		// Serializing the record parameters
		$this->record->images = implode(';', $this->record->images);
		$this->record->system = (int) $this->record->system;

		// Writing the serializing status
		$this->serialized = true;

		// Exit (success)
		return $this;
	}

	/**
	 * Deserialize
	 *
	 * @return self The instance from which the method was called (fluent interface)
	 */
	public function deserialize(): self
	{
		if (!$this->serialized) {
			// The record implementor is deserialized

			// Exit (fail)
			throw new exception_runtime('The record implementator object is already deserialized');
		}

		// Deserializing the record parameters
		$this->record->images = explode(';', $this->record->images);
		$this->record->system = (bool) $this->record->system;

		// Writing the serialized status
		$this->serialized = false;

		// Exit (success)
		return $this;
	}

}
