<?php

declare(strict_types=1);

namespace deeproots\deeproots\models\question;

// Files of the project
use deeproots\deeproots\models\core,
	deeproots\deeproots\models\question,
	deeproots\deeproots\models\enumerations\language;

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

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

/**
 * Question localization
 *
 * @uses question
 * @package deeproots\deeproots\models\question
 *
 * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
 * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
 */
final class localization extends core
{
	/**
	 * Files
	 *
	 * @var string $database Path to the database files
	 */
	protected string $files = DATABASES . DIRECTORY_SEPARATOR . 'questions' . DIRECTORY_SEPARATOR . 'localizations';

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

	/**
	 * Constructor
	 *
	 * @param language $language Language
	 *
	 * @return void
	 */
	public function __construct(language $language)
	{
		// Initializing the database
		$this->database = new database()
			->encoding(encoding::ascii)
			->columns(
				new column('identifier', type::integer_unsigned),
				new column('text', type::string, ['length' => 256]),
				new column('A', type::string, ['length' => 128]),
				new column('B', type::string, ['length' => 128]),
				new column('C', type::string, ['length' => 128]),
				new column('D', type::string, ['length' => 128]),
				new column('updated', type::integer_unsigned),
				new column('created', type::integer_unsigned)
			)
			->connect($this->files . DIRECTORY_SEPARATOR . strtolower($language->label()) . '.baza');
	}

	/**
	 * Create
	 *
	 * Creates the record in the database
	 *
	 * @param int $identifier Identifier of the question::class record
	 * @param string $text Text (length: 256)
	 * @param string $a Answer A (length: 128)
	 * @param string $b Answer B (length: 128)
	 * @param string $c Answer C (length: 128)
	 * @param string $d Answer D (length: 128)
	 *
	 * @return int|false The record identifier, if created
	 */
	public function create(int $identifier, string $text, string $a, string $b, string $c, string $d): int|false
	{
		// Initializing the record
		$record = $this->database->record(
			$identifier,
			$text,
			$a,
			$b,
			$c,
			$d,
			svoboda::timestamp(),
			svoboda::timestamp()
		);

		// Creating the record in the database
		$created = $this->database->write($record);

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