<?php
namespace EasyChat\Library\Ai;

class TestNoticeSanitizer {
    public static function forPrompt($text): string {
        $text = trim((string)$text);
        if ($text === '') {
            return '';
        }

        $original = $text;
        $had_marker = false;

        $without_marker = preg_replace('/\btest_ai_operatoru_ne_otvechat_ctx_[a-z0-9]+_\d+_/iu', '', $text);
        if (is_string($without_marker) && $without_marker !== $text) {
            $text = $without_marker;
            $had_marker = true;
        }

         
         
         
        $text = preg_replace('/\([^)]*(?:тест|test|провер|proverk|operatoram|оператор)[^)]*(?:не\s+отвечать|не\s+отвечайте|ne\s+otvechat|ne\s+otvechayte)[^)]*\)/iu', '', $text);
        $text = preg_replace('/\b(?:testovyj|testovyi|testovy|test|тестовый|тестовий|тест)\s+(?:zapros|запрос)[\s\S]*?(?:ne\s+otvechat|ne\s+otvechayte|не\s+отвечать|не\s+отвечайте)[\s\S]*$/iu', '', $text);
        $text = preg_replace('/\b(?:zhivym\s+)?operatoram\s+ne\s+otvechat\b[\s\S]*$/iu', '', $text);
        $text = preg_replace('/\b(?:живым\s+)?операторам\s+не\s+отвечать\b[\s\S]*$/iu', '', $text);

        if ($had_marker) {
            $text = str_replace('_', ' ', $text);
        }

        $text = trim(preg_replace('/\s+/u', ' ', (string)$text));

        return $text !== '' ? $text : $original;
    }
}
