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

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

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

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

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

	/**
	 * 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('gpt_4_1', type::integer_unsigned),
				new column('gpt_4_1_mini', type::integer_unsigned),
				new column('gpt_4_1_nano', type::integer_unsigned),
				new column('o4_mini', type::integer_unsigned),
				new column('gpt_5', type::integer_unsigned),
				new column('gpt_5_mini', type::integer_unsigned),
				new column('gpt_5_nano', type::integer_unsigned),
				new column('gpt_5_pro', type::integer_unsigned),
				new column('gpt_5_1', type::integer_unsigned),
				new column('sora_2', type::integer_unsigned),
				new column('sora_2_pro', type::integer_unsigned),
				/* new column('sora_2_pro_hight', type::integer_unsigned), */
				new column('gpt_image_1', type::integer_unsigned),
				new column('gpt_image_1_mini', type::integer_unsigned),
				/* new column('gpt_image_1_text', type::integer_unsigned),
				new column('gpt_image_1_text_mini', type::integer_unsigned), */
				new column('expires', type::integer_unsigned),
				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 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 $sora_2
	 * @param int $sora_2_pro
	 * @param int $gpt_image_1
	 * @param int $gpt_image_1_mini
	 * @param int $expires
	 *
	 * @return int|false The record identifier, if created
	 */
	public function write(
		int $account = 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 = 0,
		int $gpt_5_1 = 0,
		int $sora_2 = 0,
		int $sora_2_pro = 0,
		int $gpt_image_1 = 0,
		int $gpt_image_1_mini = 0,
		int $expires = 0,
	): int|false {
		$record = $this->database->record(
			$this->database->count() + 1,
			$account,
			$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,
			$sora_2,
			$sora_2_pro,
			$gpt_image_1,
			$gpt_image_1_mini,
			$expires,
			svoboda::timestamp(),
			svoboda::timestamp()
		);

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

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