<?php

declare(strict_types=1);

namespace kodorvan\neurobot\models\telegram;

// Files of the project
use kodorvan\neurobot\models\core,
	kodorvan\neurobot\models\code,
	kodorvan\neurobot\models\tariff,
	kodorvan\neurobot\models\account as model;

// Library for languages support
use mirzaev\languages\language;

// The library for escaping all markdown symbols
use function mirzaev\unmarkdown;

// Baza database
use mirzaev\baza\database,
	mirzaev\baza\column,
	mirzaev\baza\record,
	mirzaev\baza\enumerations\encoding,
	mirzaev\baza\enumerations\type;

// Svoboda time
use svoboda\time\statement as svoboda;

// Framework for Telegram
use SergiX44\Nutgram\Telegram\Types\User\User as telegram_user;

// Built-in libraries
use Error as error;

/**
 * Telegram account
 *
 * @package kodorvan\neurobot\models\telegram
 *
 * @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
{
	/* Code
	 *
	 * Write neural network into the account record
	 *
	 * @param context $context Request data from Telegram
	 * @param string $code The activation code
	 *
	 * @return void
	 */
	public static function code(context $context, string $code): void
	{
		// Initializing the account
		$account = $context->get('account');

		if ($account instanceof model) {
			// Initialized the account

			// Initializing the language
			$language = $context->get('language');

			if ($language instanceof language) {
				// Initialized the language

				// Initializing localization 
				$localization = $context->get('localization');

				if ($localization) {
					// Initialized localization

					// Search for already activated similar code
					$duplicate = new code()->read(filter: fn(record $record) => $record->value === $code && $record->activated === 1 && $record->account === $account->identifier);

					if ($duplicate instanceof code) {
						// Found already activated code

						// Sending the message
						$context->sendMessage('⚠️ *' . $localization['code_already_activated'] . '*')->then(function (message $message) use ($context) {
							// Sended the message

							// Sending the main menu
							commands::menu(context: $context);
						});
					} else {
						// Not found already activated code

						// Activating the code
						$activated = $account->code($code);

						if ($activated) {
							// Activated the code

							// Initializing the account tariff
							$tariff = $account->tariff();

							if ($tariff instanceof tariff) {
								// Initialized the account tariff

								// Initializing the title text
								$title = '🎉 *' . $localization['code_activated'] . '*';

								// Initializing the tariff text
								$tariff = '*' . $localization['code_tariff'] . ':* ' . unmarkdown($tariff->type->label($language) . ' (' . $tariff->used . '/' . $tariff->tokens . ')');

								// Sending the message
								$context->sendMessage(
									<<<TXT
									$title

									$tariff
									TXT
								)->then(function (message $message) use ($context) {
									// Sended the message

									// Sending the main menu
									commands::menu(context: $context);
								});
							}
						} else {
							// Not activated the code
						}
					}
				} else {
					// Not initialized localization

					// Sending the message
					$context->sendMessage('⚠️ *Failed to initialize localization*')
						->then(function (message $message) use ($context) {
							// Sended the message

							// Ending the conversation process
							$context->endConversation();
						});
				}
			} else {
				// Not initialized language

				// Sending the message
				$context->sendMessage('⚠️ *Failed to initialize language*')
					->then(function (message $message) use ($context) {
						// Sended the message

						// Ending the conversation process
						$context->endConversation();
					});
			}
		} else {
			// Not initialized the account

			// Sending the message
			$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
				->then(function (message $message) use ($context) {
					// Sended the message

					// Ending the conversation process
					$context->endConversation();
				});
		}
	}
}
