<?php

declare(strict_types=1);

namespace kodorvan\neurobot;

// Files of the project
use kodorvan\neurobot\models\account,
	kodorvan\neurobot\models\telegram\middlewares,
	kodorvan\neurobot\models\telegram\commands,
	kodorvan\neurobot\models\telegram\buttons,
	kodorvan\neurobot\models\telegram\chat,
	kodorvan\neurobot\models\telegram\account as telegram_account,
	kodorvan\neurobot\models\telegram\settings,
	kodorvan\neurobot\models\telegram\tariff,
	kodorvan\neurobot\models\acquirings\yookassa,
	kodorvan\neurobot\models\enumerations\tariff as tariff_type,
	kodorvan\neurobot\models\telegram\processes\settings\chat\memory\messages as processes_settings_chat_memory_messages,
	kodorvan\neurobot\models\telegram\commands\start as command_start,
	kodorvan\neurobot\models\telegram\commands\network as command_network,
	kodorvan\neurobot\models\telegram\commands\account as command_account,
	kodorvan\neurobot\models\telegram\commands\society as command_society,
	kodorvan\neurobot\models\telegram\commands\language as command_language,
	kodorvan\neurobot\models\telegram\middlewares\account as middleware_account,
	kodorvan\neurobot\models\telegram\middlewares\language as middleware_language,
	kodorvan\neurobot\models\telegram\middlewares\localization as middleware_localization,
	kodorvan\neurobot\models\telegram\middlewares\authorizations as middleware_authorizations,
	kodorvan\neurobot\models\telegram\middlewares\settings as middleware_settings,
	kodorvan\neurobot\models\telegram\middlewares\chat as middleware_chat,
	kodorvan\neurobot\models\telegram\middlewares\network as middleware_network,
	kodorvan\neurobot\models\telegram\middlewares\message as middleware_message,
	kodorvan\neurobot\models\telegram\middlewares\welcome as middleware_welcome;

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

// Library for neural networks support
use mirzaev\neuroseti\network;

// Framework for PHP
use mirzaev\minimal\core,
	mirzaev\minimal\route;

// Framework for Telegram
use SergiX44\Nutgram\Nutgram as telegram,
	SergiX44\Nutgram\Configuration as telegram_settings,
	SergiX44\Nutgram\RunningMode\Webhook as webhook;

// The symphony cache library
use Symfony\Component\Cache\Adapter\FilesystemAdapter as cache_adapter,
	Symfony\Component\Cache\Psr16Cache as cache;

// The library for YooKassa support
use YooKassa\Client as yookassa_client;

// Enabling debugging
/* ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1); */
/* ini_set('allow_url_fopen', 1); */

// Initializing path to the public directory 
define('INDEX', __DIR__. DIRECTORY_SEPARATOR	. '..');

// Initializing path to the project root directory
define('ROOT',  INDEX . DIRECTORY_SEPARATOR	. '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR	. '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);

// Initializing path to the directory of views 
define('VIEWS', INDEX . DIRECTORY_SEPARATOR	. '..' . DIRECTORY_SEPARATOR . 'views');

// Initializing path to the directory of settings 
define('SETTINGS', INDEX . DIRECTORY_SEPARATOR	. '..' . DIRECTORY_SEPARATOR . 'settings');

// Initializing system settings 
require SETTINGS . DIRECTORY_SEPARATOR . 'system.php';

// Initializing path to the directory of the storage 
define('STORAGE', INDEX . DIRECTORY_SEPARATOR	. '..' . DIRECTORY_SEPARATOR . 'storage');

// Initializing path to the databases directory
define('DATABASES', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'databases');

// Initializing path to the localizations directory
define('LOCALIZATIONS', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'localizations');

// Initiailizing Telegram key
define('TELEGRAM', require(SETTINGS . DIRECTORY_SEPARATOR . 'telegram.php'));

// Initiailizing OpenAI key
define('OPENAI_KEY', require(SETTINGS . DIRECTORY_SEPARATOR . 'openai.php'));

// Initiailizing YooKassa
define('YOOKASSA_DATA', require(SETTINGS . DIRECTORY_SEPARATOR . 'yookassa.php'));

// Initiailizing the proxy 
define('PROXY', require(SETTINGS . DIRECTORY_SEPARATOR . 'proxy.php'));

// Initializing dependencies
require ROOT . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

// Initializing the YooKassa client
define('YOOKASSA', new yookassa_client());

// Autherization for the market API
YOOKASSA->setAuth((int) YOOKASSA_DATA['identifier'], (string) YOOKASSA_DATA['key']);

// Initializing the robot
$robot = new telegram(
	token: TELEGRAM['key'],
	config: new telegram_settings(
		botName: TELEGRAM['name'],
		cache: new cache(new cache_adapter())
	)
);

$webhook = new webhook(secretToken: TELEGRAM['password']);
$webhook->setSafeMode(true);

$robot->setRunningMode($webhook);

$robot->middleware(middleware_account::class);
$robot->middleware(middleware_language::class);
$robot->middleware(middleware_localization::class);
$robot->middleware(middleware_authorizations::class);
$robot->middleware(middleware_welcome::class);

// Initializing the message listener
$robot->onMessage([chat::class, 'message'])
	->middleware(middleware_chat::class)
	->middleware(middleware_network::class)
	->middleware(middleware_message::class)
;

// Start
$robot->registerCommand(command_start::class);
$robot->onCommand('menu', command_start::class);

// Neural networks
$robot->registerCommand(command_network::class);

//
/* $robot->onText('\/code {code}', [telegram_account::class, 'code']); */

// Initializing the updates listener
/* $robot->onUpdate(function (context $context): void {}); */

// Initializing the robot middlewares
/* $robot->middleware([middlewares::class, 'account']);
$robot->middleware([middlewares::class, 'language']);
$robot->middleware([middlewares::class, 'localization']);
$robot->middleware([middlewares::class, 'authorizations']);
$robot->middleware([middlewares::class, 'welcome']); */

// Initializing the robot commands handlers
/* 
$robot->onCommand('model', [commands::class, 'model']);
$robot->onCommand('tariff', [commands::class, 'tariff']);
$robot->onCommand('settings', [commands::class, 'settings']); */
/* $robot->onCommand('account', [commands::class, 'account']); */
/* $robot->onCommand('language', [commands::class, 'language'])->middleware([middlewares::class, 'settings']); */
/* $robot->onCommand('repository', [commands::class, 'repository']); */
/* $robot->onCommand('society', [commands::class, 'society']); */
/* $robot->onCommand('system_settings', [commands::class, 'system_settings'])->middleware([middlewares::class, 'system_settings']); */

// Initializing the robot buttons handlers
/* $robot->onCbQueryData(['account_localizations'], [account::class, 'localizations']);
$robot->onCbQueryData(['account_localization_create_start'], [process_account_localization_create::class, 'start']); */

/* // Initializing the robot settings buttons handlers
$robot->onCbQueryData(['settings_chat_memory_messages'], [processes_settings_chat_memory_messages::class, 'request']);

// Initializing the robot settings language buttons handlers
foreach (language::cases() as $language) {
	// Iterating over languages

	// Initializing language buttons
	$robot->onCbQueryData(["settings_language_$language->name"], fn(context $context) => settings::language($context, $language));
};
 */

// Neural network
$robot->onCallbackQueryData('network', [command_network::class, 'handle']);
foreach (network::cases() as $network) {
	// Iterating over neural networks

	// Select the neural network
	$robot->onCallbackQueryData("settings_network_$network->name", fn(telegram $robot) => settings::network(robot: $robot, network: $network));
};

// Account
$robot->registerCommand(command_account::class);

// Language
$robot->registerCommand(command_language::class)->middleware(middleware_settings::class);
foreach (language::cases() as $language) {
	// Iterating over languages

	// Select the language
	$robot->onCallbackQueryData("settings_language_$language->name", fn(telegram $robot) => settings::language(robot: $robot, language: $language));
};

// Society
$robot->registerCommand(command_society::class);

/*
// PROCESS: settings select
$robot->onCbQueryData(['settings'], [commands::class, 'settings']);

// PROCESS: tariff select
$robot->onCbQueryData(['tariffs'], [tariff::class, 'menu']);

// BUTTONS: Neural network select
foreach (array_reverse(tariff_type::cases()) as $tariff) {
	// Iterating over neural networks

	// Initializing neural networks buttons
	$robot->onCbQueryData(["tariff_$tariff->name"], fn(context $context) => tariff::invoice($context, $tariff));
};

// COMMAND: deactivate the chat and create a new chat
foreach (array_reverse(language::cases()) as $language) {
	// Iterating over languages

	// Initializing path to the localization file
	$file = LOCALIZATIONS . DIRECTORY_SEPARATOR . strtolower($language->label()) . '.php';

	if (file_exists($file) && is_readable($file)) {
		// Found the localization file

		// Initializing localization
		$localization = require($file);

		// Initializing neural networks buttons
		$robot->onText('✂️ ' . $localization['chat_new'], [chat::class, 'deactivate']);
	}
};

// BUTTON: deactivate the chat and create a new chat
$robot->onCbQueryData(['chat_deactivate'], [chat::class, 'deactivate']);
 */

/* $robot->registerMyCommands(); */
// Starting chat-robot
$robot->run();
