<?php

declare(strict_types=1);

namespace kodorvan\neurobot\models;

// Files of the project
use kodorvan\neurobot\models\core,
	kodorvan\neurobot\models\tariff,
	kodorvan\neurobot\models\enumerations\tariff as tariff_type,
	kodorvan\neurobot\models\enumerations\acquiring as acquiring;

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

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

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

// Port of the fast BPE tokeniser for OpenAI
use Yethee\Tiktoken\EncoderProvider as tiktoken;

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

/**
 * Invoice
 *
 * @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 invoice extends core implements record_interface
{
	use record_trait;

	/**
	 * File
	 *
	 * @var string $database Path to the database file
	 */
	protected string $file = DATABASES . DIRECTORY_SEPARATOR . 'payments.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;

	/**
	 * 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('account', type::long_long_unsigned),
				new column('acquiring', type::string, ['length' => 32]),
				new column('acquiring_identifier', type::string, ['length' => 256]),
				new column('paid', 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 $account The account identifier (0 for disable)
	 * @param tariff_type $type The tariff
	 *
	 * @return int|false The record identifier, if created
	 */
	public function write(
		int $account = 0,
		acquiring $acquiring = ACQUIRING_DEFAULT ?? acquiring::yookassa,
		string $acquiring_identifier = '',
	): int|false {
		$record = $this->database->record(
			$this->database->count() + 1,
			$account,
			$acquiring->name,
			$acquiring_identifier,
			0,
			svoboda::timestamp(),
			svoboda::timestamp()
		);

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

		// Exit (success)
		return $created ? $record->identifier : 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->acquiring = $this->record->acquiring->name;

		// 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->acquiring = acquiring::{$this->record->acquiring} ?? ACQUIRING_DEFAULT ?? acquiring::yookassa;

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

		// Exit (success)
		return $this;
	}
}
