<?php

declare(strict_types=1);

if (PHP_SAPI !== 'cli') {
    http_response_code(403);
    echo "This script can be run only from CLI.\n";
    exit(1);
}

require_once __DIR__ . '/../src/config.php';
require_once __DIR__ . '/../src/logger.php';
require_once __DIR__ . '/../src/storage.php';
require_once __DIR__ . '/../src/history.php';
require_once __DIR__ . '/../src/bitrix.php';
require_once __DIR__ . '/../src/crm.php';

$methodsResponse = bitrixCallRaw('methods');
$methods = [];
foreach (($methodsResponse['result'] ?? []) as $method) {
    if (stripos((string) $method, 'imbot') !== false || stripos((string) $method, 'im.') === 0) {
        $methods[] = $method;
    }
}

echo json_encode([
    'configured_bot_id' => (int) config('BITRIX_AI_BOT_ID', 0),
    'configured_bot_code' => config('BITRIX_AI_BOT_CODE'),
    'configured_openline_bot_id' => (int) config('BITRIX_OPENLINE_BOT_ID', 0),
    'configured_openline_bot_code' => config('BITRIX_OPENLINE_BOT_CODE'),
    'managed_open_line_ids_from_prompt_lists' => managedOpenLineIdsFromPrompts(),
    'bot_handler_url' => appUrl('bot_handler.php'),
    'scope' => bitrixCallRaw('scope'),
    'methods' => $methods,
    'bot_list' => bitrixCallRaw('imbot.bot.list'),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL;
