<?php

declare(strict_types=1);

namespace kodorvan\brainrot\models;

// Files of the project
use kodorvan\brainrot\models\core,
	kodorvan\brainrot\models\authorizations;

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

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

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

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

/**
 * Account
 *
 * @package kodorvan\brainrot\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 account extends core implements record_interface
{
	use record_trait;
	/**
	 * File
	 *
	 * @var string $database Path to the database file
	 */
	protected string $file = DATABASES . DIRECTORY_SEPARATOR . 'accounts.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::integer_unsigned),
				new column('identifier_telegram', type::long_long_unsigned),
				new column('domain', type::string, ['length' => 32]),
				new column('name_first', type::string, ['length' => 64]),
				new column('name_second', type::string, ['length' => 64]),
				new column('language', type::string, ['length' => 2]),
				new column('robot', type::char),
				new column('experience', type::long_long_unsigned),
				new column('slots', 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;
	}

	/**
	 * Initialize
	 *
	 * Searches for the account record in the database, and if it does not find it, it creates it
	 *
	 * @param telegram $telegram The telegram account
	 *
	 * @throws exception_runtime if update the account record in the database by the telegram account values
	 * @throws exception_runtime if failed to find the registered account
	 * @throws exception_runtime if failed to registrate the account
	 *
	 * @return record The account record from the database
	 */
	public function initialize(telegram $telegram): record
	{
		// Searching for the account in the database
		$account = $this->database->read(filter: fn(record $record) => $record->identifier_telegram === $telegram->getId(), amount: 1)[0] ?? null;

		if ($account instanceof record) {
			// Found the account record

			if (
				$account->name_first !== $telegram->getFirstName() ||
				$account->name_second !== $telegram->getLastName() ||
				$account->domain !== $telegram->getUsername()
			) {
				// The telegram account was updated

				// Updating the account in the database
				$updated = $this->database->read(
					filter: fn(record $record) => $record->identifier_telegram === $telegram->getId(),
					update: function (record &$record) use ($telegram) {
						// Writing new values into the record
						$record->name_first = $telegram->getFirstName();
						$record->name_second = $telegram->getLastName();
						$record->domain = $telegram->getUsername();
						$record->updated = svoboda::timestamp();
					},
					amount: 1
				)[0] ?? null;

				if ($updated instanceof record && $updated->values() !== $account->values()) {
					// Updated the account in the database

					// Exit (success)
					return $updated;
				} else {
					// Not updated the account in the database

					// Exit (fail)
					throw new exception_runtime('Failed to update the account record in the database by the telegram account values');
				}
			}

			// Exit (success)
			return $account;
		} else {
			// Not found the account record

			if ($this->registrate($telegram)) {
				// Registered the account

				// Searching for the registered account in the database
				$account = $this->database->read(filter: fn(record $record) => $record->identifier_telegram === $telegram->getId(), amount: 1)[0] ?? null;

				if ($account instanceof record) {
					// Found the registered account

					// Exit (success)
					return $account;
				} else {
					// Not found the registered account

					// Exit (fail)
					throw new exception_runtime('Failed to find the registered account');
				}
			} else {
				// Not registered the account

				// Exit (fail)
				throw new exception_runtime('Failed to registrate the account');
			}
		}
	}

	/**
	 * Registrate
	 *
	 * Creates the account record in the database
	 *
	 * @param telegram $telegram The telegram account
	 * @param int $experience The player experience
	 * @param int $slots The player avaiable slots
	 *
	 * @return int|false The record identifier, if created
	 */
	public function registrate(
		telegram $telegram,
		int $experience = 0,
		int $slots = 1
	): int|false {
		// Initializing the identifier
		$identifier = $this->database->count() + 1;

		// Initializing the record
		$record = $this->database->record(
			$identifier,
			(int) $telegram->getId(),
			(string) $telegram->getFirstName(),
			(string) $telegram->getLastName(),
			(string) $telegram->getUsername(),
			(string) $telegram->getLanguageCode(),
			(int) $telegram->isBot(),
			$experience,
			$slots,
			svoboda::timestamp(),
			svoboda::timestamp()
		);

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

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

	/**
	 * Authorizations
	 *
	 * Search for the account authorizations
	 *
	 * @return authorizations|null The account authorizations
	 */
	public function authorizations(): ?authorizations
	{
		// Initializing the authorizations model
		$authorizations = new authorizations();

		// Search for the account authorizations
		$record = $authorizations->database->read(filter: fn(record $record) => $record->account === $this->identifier, amount: 1)[0] ?? null;

		if ($record instanceof record) {
			// Found the account authorizations

			// Writing the found authorizations record into the record object
			$authorizations->record = $record;

			// Exit (success)
			return $authorizations;
		}

		// Exit (fail)
		return null;
	}
}
