<?php

declare(strict_types=1);

namespace kodorvan\neurobot\models;

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

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

// Framework for Telegram
use Zanzara\Telegram\Type\User as telegram;

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

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

	/**
	 * File
	 *
	 * @var string $database Path to the database file
	 */
	protected string $file = DATABASES . DIRECTORY_SEPARATOR . 'authorizations.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('system', type::char),
				new column('settings', type::char),
				new column('chat', type::char),
				new column('gpt_3_5_turbo', type::char),
				new column('gpt_4_1', type::char),
				new column('gpt_4_1_mini', type::char),
				new column('gpt_4_1_nano', type::char),
				new column('o4_mini', type::char),
				new column('gpt_5', type::char),
				new column('gpt_5_mini', type::char),
				new column('gpt_5_nano', type::char),
				new column('gpt_5_pro', type::char),
				new column('gpt_5_1', type::char),
				new column('gpt_5_2', type::char),
				new column('sora_2', type::char),
				new column('sora_2_pro', type::char),
				/* new column('sora_2_pro_hight', type::char), */
				new column('gpt_image_1', type::char),
				new column('gpt_image_1_mini', type::char),
				/* new column('gpt_image_1_text', type::char),
				new column('gpt_image_1_text_mini', type::char), */
				new column('system_accounts', type::char),
				new column('system_settings', type::char),
				new column('active', 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
	 * @param int $system
	 * @param int $settings
	 * @param int $chat
	 * @param int $gpt_3_5_turbo
	 * @param int $gpt_4_1
	 * @param int $gpt_4_1_mini
	 * @param int $gpt_4_1_nano
	 * @param int $o4_mini
	 * @param int $gpt_5
	 * @param int $gpt_5_mini
	 * @param int $gpt_5_nano
	 * @param int $gpt_5_pro
	 * @param int $gpt_5_1
	 * @param int $gpt_5_2
	 * @param int $sora_2
	 * @param int $sora_2_pro
	 * @param int $gpt_image_1
	 * @param int $gpt_image_1_mini
	 * @param int $system_accounts
	 * @param int $system_settings
	 * @param bool $active Is the record active?
	 *
	 * @return record|false The record, if created
	 */
	public function write(
		int $account,
		int $system = 1,
		int $settings = 1,
		int $chat = 1,
		int $gpt_3_5_turbo = 0,
		int $gpt_4_1 = 0,
		int $gpt_4_1_mini = 0,
		int $gpt_4_1_nano = 0,
		int $o4_mini = 0,
		int $gpt_5 = 1,
		int $gpt_5_mini = 0,
		int $gpt_5_nano = 0,
		int $gpt_5_pro = 1,
		int $gpt_5_1 = 1,
		int $gpt_5_2 = 1,
		int $sora_2 = 0,
		int $sora_2_pro = 0,
		int $gpt_image_1 = 0,
		int $gpt_image_1_mini = 0,
		int $system_accounts = 0,
		int $system_settings = 0,
		bool $active = true,
	): record|false
	{
		$record = $this->database->record(
			$this->database->count() + 1,
			$account,
			$system,
			$settings,
			$chat,
			$gpt_3_5_turbo,
			$gpt_4_1,
			$gpt_4_1_mini,
			$gpt_4_1_nano,
			$o4_mini,
			$gpt_5,
			$gpt_5_mini,
			$gpt_5_nano,
			$gpt_5_pro,
			$gpt_5_1,
			$gpt_5_2,
			$sora_2,
			$sora_2_pro,
			$gpt_image_1,
			$gpt_image_1_mini,
			$system_accounts,
			$system_settings,
			(int) $active,
			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->active = (int) $this->record->active;

		// 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->active = (bool) $this->record->active;

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

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