<?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/bitrix.php';
require_once __DIR__ . '/../src/prompts.php';

$options = getopt('', ['open-line-id::', 'help']);
if (isset($options['help'])) {
    echo "Usage: php tools/check_prompts.php [--open-line-id=227]\n";
    exit(0);
}

$context = [];
if (isset($options['open-line-id'])) {
    $context['open_line_id'] = (string) $options['open-line-id'];
}

echo json_encode([
    'system_list_code' => config('SYSTEM_PROMPTS_LIST_CODE'),
    'correction_list_code' => config('CORRECTION_PROMPTS_LIST_CODE'),
    'system_open_line_property' => config('SYSTEM_PROMPT_OPEN_LINE_PROPERTY'),
    'correction_open_line_property' => config('CORRECTION_PROMPT_OPEN_LINE_PROPERTY'),
    'context' => $context,
    'prompts' => loadPromptsFromBitrixLists(),
    'filtered_system_prompt' => buildSystemPrompt($context),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL;
