<?php

declare(strict_types=1);

namespace mirzaev\deeproots\models\telegram\processes\question;

// Files of the project
use mirzaev\deeproots\models\core,
	mirzaev\deeproots\models\question,
	mirzaev\deeproots\models\question\localization,
	mirzaev\deeproots\models\answer,
	mirzaev\deeproots\models\enumerations\language,
	mirzaev\deeproots\models\telegram\commands,
	mirzaev\deeproots\models\telegram\processes\question\search as process_question_search;

// Framework for Telegram
use Zanzara\Context as context,
	Zanzara\Telegram\Type\Message as message;

// Baza database
use mirzaev\baza\record;

/**
 * Question create (system) 
 *
 * @package mirzaev\deeproots\models\telegram\processes\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 create extends core
{
	/**
	 * Process
	 *
	 * @var const string PROCESS Name of the process in the telegram user buffer
	 */
	public const string PROCESS = 'system_questions_create';

	/**
	 * Start
	 *
	 * Starting the process
	 *
	 * @param context $context Request data from Telegram
	 *
	 * @return void
	 */
	public static function start(context $context): void
	{
		// Initializing the account
		$account = $context->get('account');

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

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

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

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

				if ($localization) {
					// Initialized localization

					// Initializing the question model
					$model_question = new question();

					// Creating the question
					$question = $model_question->create();

					if ($question) {
						// Created the question

						// Sending the message
						$context->sendMessage('✅ *' . $localization[static::PROCESS . '_created'] . '*')
							->then(function (message $message) use ($context, $question) {
								// Sended the message

								// Sending the question search menu
								process_question_search::start($context, $question);
							});
					};
				} 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 the account*')
				->then(function (message $message) use ($context) {
					// Sended the message

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