<?php

declare(strict_types=1);

namespace deeproots\deeproots;

// Files of the project
use deeproots\deeproots\models\telegram\middlewares,
    deeproots\deeproots\models\telegram\commands,
    deeproots\deeproots\models\telegram\settings,
    deeproots\deeproots\models\telegram\processes\question\search as process_question_search,
    deeproots\deeproots\models\telegram\buttons\question\search as buttons_question_search,
    deeproots\deeproots\models\telegram\processes\question\create as process_question_create,
    deeproots\deeproots\models\enumerations\language;

// Framework for Telegram
use Zanzara\Zanzara as zanzara,
    Zanzara\Context as context,
    Zanzara\Config as config;

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

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

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

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

// Initializing path to the storage directory
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_KEY', require(SETTINGS . DIRECTORY_SEPARATOR . 'telegram.php'));

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

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

// Initializing the configuration
$config = new config();
$config->setParseMode(config::PARSE_MODE_MARKDOWN);
$config->useReactFileSystem(true);

// Initializing the robot
$robot = new Zanzara(TELEGRAM_KEY, $config);

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

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

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

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

$robot->onCbQueryData(['system_questions_search_identifier'], [buttons_question_search::class, 'identifier'])->middleware([middlewares::class, 'system_questions']);
$robot->onCbQueryData(['system_questions_search_text'], [buttons_question_search::class, 'text'])->middleware([middlewares::class, 'system_questions']);
$robot->onCbQueryData(['system_questions_search_a'], [buttons_question_search::class, 'a'])->middleware([middlewares::class, 'system_questions']);
$robot->onCbQueryData(['system_questions_search_b'], [buttons_question_search::class, 'b'])->middleware([middlewares::class, 'system_questions']);
$robot->onCbQueryData(['system_questions_search_c'], [buttons_question_search::class, 'c'])->middleware([middlewares::class, 'system_questions']);
$robot->onCbQueryData(['system_questions_search_d'], [buttons_question_search::class, 'd'])->middleware([middlewares::class, 'system_questions']);
$robot->onCbQueryData(['system_questions_search_end'], [process_question_search::class, 'end'])->middleware([middlewares::class, 'system_questions']);
$robot->onCbQueryData(['system_questions_search_next'], [process_question_search::class, 'next'])->middleware([middlewares::class, 'system_questions']);
$robot->onCbQueryData(['system_questions_search'], [process_question_search::class, 'start'])->middleware([middlewares::class, 'system_questions']);
$robot->onCbQueryData(['system_questions_create'], [process_question_create::class, 'start'])->middleware([middlewares::class, 'system_questions']);
$robot->onCbQueryData(['system_questions'], [commands::class, 'system_questions'])->middleware([middlewares::class, 'system_questions']);

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