<?php

declare(strict_types=1);

namespace App\Config;

final class PromptBuilderConfig
{
    private const MAIN_DEVICE_REQUEST_ROLE_KEYWORDS = [
        'messanlage', 'messanlagen', 'anlage', 'anlagen', 'messgerät', 'messgeraet',
        'messgeräte', 'messgeraete', 'analysegerät', 'analysegeraet', 'analysegeräte',
        'analysegeraete', 'analysator', 'analysatoren', 'analyzer', 'gerät', 'geraet',
        'geräte', 'geraete', 'system', 'systeme', 'monitor', 'monitore', 'controller',
        'tester', 'pocket tester', 'pockettester', 'handmessgerät', 'handmessgeraet',
        'überwachungsgerät', 'ueberwachungsgeraet', 'testomat', 'testoamt',
    ];

    private const MAIN_DEVICE_PRODUCT_ROLE_KEYWORDS = [
        'messanlage', 'messanlagen', 'messgerät', 'messgeraet', 'messgeräte', 'messgeraete',
        'analysegerät', 'analysegeraet', 'analysegeräte', 'analysegeraete', 'analysator',
        'analysatoren', 'analyzer', 'online-analysator', 'online analysator',
        'online-analysegerät', 'online analysegeraet', 'gerät', 'geraet', 'geräte',
        'geraete', 'system', 'systeme', 'monitor', 'monitore', 'controller',
        'tester', 'pocket tester', 'pockettester', 'handmessgerät', 'handmessgeraet',
        'labor messgerät', 'labor-messgerät', 'labor messgeraet', 'labor-messgeraet',
        'kombimessgerät', 'kombi-messgerät', 'kombimessgeraet', 'kombi-messgeraet',
        'überwachungsgerät', 'ueberwachungsgeraet', 'messumformer', 'transmitter',
        'regler', 'testomat',
    ];

    private const ACCESSORY_PRODUCT_ROLE_KEYWORDS = [
        'indikator', 'indikatoren', 'indicator', 'reagenz', 'reagenzien', 'reagent',
        'zubehör', 'zubehor', 'ersatzteil', 'ersatzteile', 'kit', 'set',
        'verbrauchsmaterial', 'consumable', 'nachfüll', 'nachfuell', 'refill',
        'lösung', 'loesung', 'solution', 'teststreifen', 'test strip', 'filter',
        'pumpenkopf', 'motorblock', 'service set', 'serviceset', 'service-set',
        'elektrode', 'elektroden', 'electrode', 'electrodes', 'glasschaft-elektrode',
        'kunststoffschaft-elektrode', 'sensor', 'sensoren', 'sensors', 'sonde',
        'sonden', 'probe', 'probes', 'messsonde', 'elektrolyt', 'kabel', 'adapter',
        'ph-indikator', 'ph indikator', 'ph-indikatoren', 'ph indikatoren',
    ];

    private const TECHNICAL_PRODUCT_KEYWORDS = [
            'technisch',
            'technical',
            'produkt',
            'product',
            'gerät',
            'device',
            'modell',
            'model',
            'messprinzip',
            'measurement principle',
            'schnittstelle',
            'interface',
            'relais',
            'relay',
            'indikator',
            'indicator',
            'grenzwert',
            'threshold',
            'messbereich',
            'measurement range',
            'gemessen',
            'measured',
            'minimaler',
            'minimum',
            'resthärte',
            'resthaerte',
            '°dh',
            'dh',
            'spannung',
            'voltage',
            'strom',
            'current',
            'druck',
            'pressure',
            'temperatur',
            'temperature',
            'schutzart',
            'ip',
            'fehlercode',
            'error code',
            'wasserhärte',
            'hardness',
            'testomat',
            'chlor',
            'chlormessung',
        ];

    private const MEASUREMENT_EVIDENCE_PARAMETERS = [
        [
            'id' => 'ph',
            'label' => 'pH / pH-Wert',
            'request_terms' => ['ph', 'pH', 'pH-Wert', 'ph-wert', 'ph wert'],
            'positive_terms' => ['pH', 'pH-Wert', 'ph-wert', 'ph wert'],
            'positive_context_terms' => ['Messung', 'messen', 'misst', 'Messbereich', 'Messparameter', 'Messgröße', 'Messgroesse', 'Bestimmung', 'bestimmen', 'Analyse', 'analysiert', 'überwachen', 'ueberwachen', 'Indikator für', 'Indikator fuer', 'Reagenz für', 'Reagenz fuer', 'Sensor', 'Elektrode'],
            'negative_context_terms' => ['Betriebsbereich', 'Betriebsumgebung', 'Einsatzbedingungen', 'störungsfrei', 'stoerungsfrei', 'pH-Wert bei', 'ph wert bei', 'ph-wert bei', 'bei 20 °C', 'bei 20 °c', 'bei 20°C', 'bei 20°c', 'Reagenzlösung hat', 'Loesung hat', 'Lösung hat'],
            'non_equivalent_terms' => ['p-Wert', 'p Wert', 'm-Wert', 'minus m-Wert', 'Alkalität', 'Säurekapazität', 'mmol/l'],
            'safe_no_evidence_answer_de' => 'Ich finde in den bereitgestellten Quellen keinen sicher belegten Testomat für pH-Messung.',
            'safe_no_accessory_evidence_answer_de' => 'Ich finde in den bereitgestellten Quellen keinen sicher belegten pH-Indikator oder ein pH-Reagenz für Messgeräte.',
        ],
        [
            'id' => 'redox',
            'label' => 'Redox / ORP',
            'request_terms' => ['redox', 'orp', 'oxidations-reduktionspotential', 'oxidations reduktionspotential'],
            'positive_terms' => ['Redox', 'ORP', 'Oxidations-Reduktionspotential', 'Oxidations Reduktionspotential'],
            'positive_context_terms' => ['Messung', 'messen', 'misst', 'Messbereich', 'Messparameter', 'Messgröße', 'Messgroesse', 'Bestimmung', 'bestimmen', 'Analyse', 'analysiert', 'überwachen', 'ueberwachen', 'Indikator für', 'Indikator fuer', 'Reagenz für', 'Reagenz fuer', 'Sensor', 'Elektrode'],
            'negative_context_terms' => ['Betriebsbereich', 'Betriebsumgebung', 'Einsatzbedingungen', 'störungsfrei', 'stoerungsfrei'],
            'non_equivalent_terms' => [],
            'safe_no_evidence_answer_de' => 'Ich finde in den bereitgestellten Quellen keinen sicher belegten Treffer für Redox-/ORP-Messung.',
            'safe_no_accessory_evidence_answer_de' => 'Ich finde in den bereitgestellten Quellen keinen sicher belegten Redox-/ORP-Indikator oder ein Redox-/ORP-Reagenz für Messgeräte.',
        ],
        [
            'id' => 'free_chlorine',
            'label' => 'freies Chlor',
            'request_terms' => ['freies chlor', 'freiem chlor', 'freien chlor', 'free chlorine'],
            'positive_terms' => ['freies Chlor', 'freiem Chlor', 'freien Chlor', 'free chlorine'],
            'positive_context_terms' => ['Messung', 'messen', 'misst', 'Messbereich', 'Messparameter', 'Messgröße', 'Messgroesse', 'Bestimmung', 'bestimmen', 'Analyse', 'analysiert', 'überwachen', 'ueberwachen', 'Indikator für', 'Indikator fuer', 'Reagenz für', 'Reagenz fuer', 'Sensor', 'Elektrode'],
            'negative_context_terms' => ['Betriebsbereich', 'Betriebsumgebung', 'Einsatzbedingungen', 'störungsfrei', 'stoerungsfrei'],
            'non_equivalent_terms' => ['Chlor gesamt', 'Gesamtchlor', 'total chlorine'],
            'safe_no_evidence_answer_de' => 'Ich finde in den bereitgestellten Quellen keinen sicher belegten Treffer für die Messung von freiem Chlor.',
            'safe_no_accessory_evidence_answer_de' => 'Ich finde in den bereitgestellten Quellen keinen sicher belegten Indikator oder ein Reagenz für die Messung von freiem Chlor.',
        ],
    ];

    private const ACCESSORY_REQUEST_KEYWORDS = [
            'passend',
            'passende',
            'passendes',
            'zubehör',
            'zubehor',
            'dazu',
            'indikator',
            'indikatoren',
            'ph-indikator',
            'ph indikator',
            'ph-indikatoren',
            'ph indikatoren',
            'reagenz',
            'kit',
            'set',
            'zusatz',
            'ergänzung',
            'ergaenzung',
        ];

    /**
     * @param array<string, mixed> $config
     */
    public function __construct(
        private readonly array $config = [],
        private readonly ?DomainVocabularyConfig $vocabulary = null,
    ) {
    }

    public function getCharsPerToken(): int
    {
        return $this->getInt('budget.chars_per_token', 4);
    }

    public function getHistoryPaddingChars(): int
    {
        return $this->getInt('budget.history_padding_chars', 400);
    }

    public function getOutputReserveRatio(): float
    {
        return $this->getFloat('budget.output_reserve_ratio', 0.25);
    }

    public function getOutputReserveMinTokens(): int
    {
        return $this->getInt('budget.output_reserve_min_tokens', 768);
    }

    public function getOutputReserveMaxTokens(): int
    {
        return $this->getInt('budget.output_reserve_max_tokens', 6000);
    }

    public function getSafetyReserveRatio(): float
    {
        return $this->getFloat('budget.safety_reserve_ratio', 0.05);
    }

    public function getSafetyReserveMinTokens(): int
    {
        return $this->getInt('budget.safety_reserve_min_tokens', 256);
    }

    public function getSafetyReserveMaxTokens(): int
    {
        return $this->getInt('budget.safety_reserve_max_tokens', 1024);
    }

    public function getMinPromptBudgetTokens(): int
    {
        return $this->getInt('budget.min_prompt_budget_tokens', 1024);
    }

    public function getMaxShopResultsInPrompt(): int
    {
        return $this->getInt('shop_results.max_results_in_prompt', 24);
    }

    public function getDetailedShopResultsMaxCount(): int
    {
        return $this->getInt('shop_results.detailed_max_count', 5);
    }

    public function getTechnicalProductKeywordMatchThreshold(): int
    {
        return $this->getInt('technical_product_keyword_match_threshold', 2);
    }

    private function getInt(string $path, int $default): int
    {
        $value = $this->getValue($path, $default);

        return is_numeric($value) ? (int) $value : $default;
    }

    private function getFloat(string $path, float $default): float
    {
        $value = $this->getValue($path, $default);

        return is_numeric($value) ? (float) $value : $default;
    }

    private function getString(string $path, string $default): string
    {
        $value = $this->getValue($path, $default);

        if (!is_scalar($value)) {
            return $default;
        }

        $value = (string) $value;

        return $value !== '' ? $value : $default;
    }

    /**
     * @return string[]
     */
    private function getStringList(string $path, array $default): array
    {
        $value = $this->getValue($path, $default);

        if (!is_array($value)) {
            return $default;
        }

        $out = [];
        foreach ($value as $item) {
            if (!is_scalar($item)) {
                continue;
            }

            $item = trim((string) $item);
            if ($item === '' || in_array($item, $out, true)) {
                continue;
            }

            $out[] = $item;
        }

        return $out !== [] ? $out : $default;
    }

    /**
     * @return string[]
     */
    private function vocabularyView(string $path, array $fallback): array
    {
        return $this->vocabulary?->view($path, $fallback) ?? $fallback;
    }

    private function getValue(string $path, mixed $default): mixed
    {
        $current = $this->config;

        foreach (explode('.', $path) as $segment) {
            if (!is_array($current) || !array_key_exists($segment, $current)) {
                return $default;
            }

            $current = $current[$segment];
        }

        return $current;
    }

    public function getSystemSectionLabel(): string
    {
        return $this->getString('sections.system_label', 'SYSTEM');
    }

    public function getUserQuestionSectionLabel(): string
    {
        return $this->getString('sections.user_question_label', 'USER QUESTION');
    }

    public function getConversationContextSectionLabel(): string
    {
        return $this->getString('sections.conversation_context_label', 'CONVERSATION CONTEXT (contextual only)');
    }

    /**
     * @return string[]
     */
    public function getConversationContextIntroLines(): array
    {
        return $this->getStringList('conversation_context.intro_lines', [
            'The following messages are previous turns of this conversation.',
            'Use them only to resolve references, follow-up questions, and user intent.',
            'Previous assistant answers are not a factual source for technical values, product compatibility, indicators, ranges, prices, or availability.',
            'All factual claims must come from retrieved factual knowledge, user-provided URL content, or live shop data.',
            'Conversation context must not override retrieved factual knowledge or live shop data.',
        ]);
    }

    public function getShopSearchQuerySectionLabel(): string
    {
        return $this->getString('sections.shop_search_query_label', 'SHOP SEARCH QUERY');
    }

    public function getShopSearchQuerySourceLine(): string
    {
        return $this->getString('shop_search.source_line', 'Source: Shop Search');
    }

    /**
     * @return string[]
     */
    public function getLiveShopResultsHeaderLines(): array
    {
        return $this->getStringList('shop_results.header_lines', [
            'LIVE SHOP RESULTS (authoritative for current commercial details):',
            'Use these results as the primary source for current price, availability, URL, current shop-visible product naming, and explicitly shop-visible product suitability when the user asks which product/device can measure or monitor something.',
            'If retrieved documents conflict with shop data on price, availability, URL, current naming, or explicitly shop-visible product suitability, prefer the shop data for those fields.',
            'If retrieved documents do not identify a matching product, but a live shop result explicitly names the requested measurement parameter or application, do not conclude that no matching product exists.',
            'Output real URL values exactly as provided in the shop results. Do not replace them with placeholders, link labels, or product names.',
            'Do not infer undocumented technical specifications from shop data.',
            'Commercial fields from shop data may only be assigned to a product if the shop item clearly matches the same product identity.',
            'Do not merge a device identified in retrieved knowledge with price, URL, product number, or availability from a different shop item such as a reagent, accessory, kit, consumable, or service item.',
            'If a shop result has no price field, do not state a price for it.',
            'Never interpret a missing price or a zero price as free, kostenlos, gratis, or available for 0.00 EUR.',
            'Treat every SHOP PRODUCT RECORD as atomic: exact product name, product number, price, availability, URL, image, description, and metadata must stay together.',
            'When outputting a shop item, use the exact shop product name from that same SHOP PRODUCT RECORD as the heading. Never use a retrieved-knowledge device name as the heading for a different shop URL or product number.',
            'If a technical device from retrieved knowledge and a shop record are not clearly the same exact product identity, separate Fachliche Einordnung from Shop-Treffer instead of merging them.',
        ]);
    }

    public function getLiveShopResultsOverflowNoticeTemplate(): string
    {
        return $this->getString('shop_results.overflow_notice_template', 'Only the top %d ranked shop results are shown here out of %d total results.');
    }

    public function getShopRecordHeaderTemplate(): string
    {
        return $this->getString('shop_results.record_header_template', '[%d] SHOP PRODUCT RECORD');
    }

    public function getShopExactProductNameLabel(): string
    {
        return $this->getString('shop_results.exact_product_name_label', 'Exact shop product name');
    }

    /**
     * @return string[]
     */
    public function getShopAtomicRecordNoteLines(): array
    {
        return $this->getStringList('shop_results.atomic_record_note_lines', [
            'Record boundary: all fields below belong only to this exact shop product record.',
        ]);
    }

    public function getOutputPrioritySectionLabel(): string
    {
        return $this->getString('sections.output_priority_label', 'OUTPUT PRIORITY');
    }

    /**
     * @return string[]
     */
    public function getOutputPriorityRules(): array
    {
        return $this->getStringList('output_priority.rules', [
            '- Use retrieved knowledge first to determine the technically matching product or answer.',
            '- For product-selection questions such as which device can measure or monitor a parameter, use relevant live shop results as a fallback when retrieved knowledge does not identify a matching product.',
            '- If shop results are present, use them afterwards to add current price, availability, and the actual URL.',
            '- Do not let bundles, accessories, or service items override a better technical match unless the user explicitly asks for them.',
        ]);
    }
    /**
     * @return string[]
     */
    public function getOutputPriorityTechnicalRules(): array
    {
        return $this->getStringList('output_priority.technical_rules', [
            '- For technical questions, answer the exact requested fact first and keep it as the main answer.',
            '- If one source chunk contains both the best matching value and nearby comparison values, use the nearby values only as context and do not include them unless the user asks for comparison or alternatives.',
            '- For lowest/highest/minimum/maximum questions, answer only the requested extreme value and the product/device explicitly connected to it.',
            '- Do not add runner-up products, second-lowest values, adjacent ranges, broader tables, or explanatory comparisons unless explicitly requested.',
        ]);
    }

    public function getFallbackEscalationSectionLabel(): string
    {
        return $this->getString('sections.fallback_escalation_label', 'FALLBACK AND ESCALATION RULES');
    }

    public function getFallbackEscalationStateLineTemplate(): string
    {
        return $this->getString('fallback_escalation.state_line_template', '- Internal confidence state: {state}.');
    }

    /**
     * @return string[]
     */
    public function getFallbackEscalationBaseRules(): array
    {
        return $this->getStringList('fallback_escalation.base_rules', [
            '- Prefer transparent uncertainty over a confident but unsupported answer.',
            '- Never present missing or weak evidence as proof that a product, value, accessory, or suitability does not exist.',
            '- A negative answer is allowed only when the provided sources explicitly support that negative finding for the asked scope.',
            '- If the sources merely do not prove suitability, answer as missing evidence instead of as a definitive exclusion. Avoid words such as "ausschließlich", "keines", or "nicht geeignet" unless directly grounded.',
            '- If several products, parameters, or accessories could match, ask one focused clarification question instead of guessing.',
            '- For risky or binding product selection, state that sales or support should verify the application before a final selection.',
        ]);
    }

    /**
     * @return string[]
     */
    public function getFallbackEscalationStateRules(string $state): array
    {
        return $this->getStringList('fallback_escalation.states.' . $state, []);
    }

    /**
     * @return string[]
     */
    public function getFallbackEscalationWithoutShopCheckRules(): array
    {
        return $this->getStringList('fallback_escalation.without_shop_check_rules', [
            '- If the question is product-related and no live shop check was performed in this run, do not make a portfolio-wide negative statement such as "there is no product".',
            '- Phrase missing evidence narrowly, for example: "Im RAG-Wissen finde ich dazu keine belastbare Information."',
            '- If useful, say that a shop search can be used to look for matching products, but do not claim shop results were checked unless they are present in the prompt.',
        ]);
    }

    public function getResponseFormatSectionLabel(): string
    {
        return $this->getString('sections.response_format_label', 'RESPONSE FORMAT RULES');
    }

    /**
     * @return string[]
     */
    public function getResponseFormatBaseRules(): array
    {
        return $this->getStringList('response_format.base_rules', [
            '- Keep normal spacing between all words. Never fuse words together.',
            '- Use short, clean paragraphs or short labeled sections.',
            '- Do not use persuasive or promotional wording.',
            '- Do not repeat the same fact in slightly different wording.',
            '- Never mention brands, manufacturers, model names, or product families that do not appear in the provided shop results, retrieved knowledge, URL content, or conversation context.',
            '- If no suitable product is explicitly grounded in the provided sources, say that plainly instead of inventing alternatives.',
            '- Do not generate external alternative lists, vendor suggestions, or purchase recommendations unless they are explicitly present in the provided sources.',
            '- Do not combine technical identity from one source with commercial fields from a different product.',
            '- Product number, price, availability, and URL must belong to the same explicitly grounded product.',
            '- Avoid absolute negative wording such as "ausschließlich", "keines", "nicht geeignet", "gibt es nicht", or portfolio-wide negations unless the provided sources explicitly support that exact scope.',
            '- Prefer narrow evidence wording, for example "in den vorliegenden Quellen nicht sicher belegt", "in den aktuellen Treffern nicht belegt", or "die gezeigten Treffer belegen keine Eignung".',
            '- When mentioning options outside the provided sources, do not recommend specific external products, vendors, or purchases. Phrase only the required category neutrally and state that such evidence is not present in the provided sources.',
        ]);
    }

    /**
     * @return string[]
     */
    public function getResponseFormatWithShopRules(): array
    {
        return $this->getStringList('response_format.with_shop_rules', [
            '- If a product is identified, prefer this structure per product: product name, product number, price, availability, URL, then only the most relevant technical facts.',
            '- Keep price, availability, and URL on separate lines when they are present.',
            '- Only use shop price, URL, product number, or availability for the main product when the shop result clearly matches that same main product.',
            '- If the matching shop item appears to be an accessory, reagent, consumable, set, or kit, keep it separate and do not present its commercial fields as the main device.',
            '- If the commercial match is uncertain, say that commercial details for the main product are not clearly available in the provided shop results.',
            '- If no price is shown for a shop item, omit the price instead of writing 0,00 €, free, kostenlos, or a guessed price.',
            '- For every shop hit shown in the answer, copy the exact shop product name verbatim from the same SHOP PRODUCT RECORD as the item heading.',
            '- Never place a shop URL, product number, price, or availability below a different heading taken from retrieved knowledge.',
            '- If technical RAG knowledge and shop records cannot be matched with high confidence, use separate sections: Fachliche Einordnung and Shop-Treffer.',
            '- For uncertain technical suitability from shop hits, use a short section like "Shop-Treffer (technische Eignung nicht sicher belegt)" and list only exact shop fields. Do not add a technical explanation or recommendation.',
        ]);
    }

    /**
     * @return string[]
     */
    public function getResponseFormatWithoutShopRules(): array
    {
        return $this->getStringList('response_format.without_shop_rules', [
            '- If no shop results are present, do not compensate by inventing external products or external manufacturers.',
        ]);
    }

    /**
     * @return string[]
     */
    public function getResponseFormatTechnicalRules(): array
    {
        return $this->getStringList('response_format.technical_rules', [
            '- Write like technical documentation: precise, neutral, and source-close.',
            '- Prefer exact values, ranges, thresholds, compatibility notes, and application areas over general explanation.',
            '- For direct follow-up questions about an indicator, value, threshold, or device, answer the resolved mapping first before any table or explanation.',
            '- If the sources only support a negative finding, output only that negative finding and do not add speculative alternatives.',
            '- For product-selection answers, keep the answer minimal: suitable product if explicitly supported, exact evidence, current shop fields if same product identity is clear. Do not add sections for Vorteile, Einsatzbereiche, Messprinzip, or Hinweise unless directly asked and explicitly sourced.',
            '- If no suitable product is grounded, do not pivot to alternative product categories as a recommendation. You may only say neutrally what kind of explicitly designated product or accessory would be needed, and that it is not evidenced in the current sources.',
        ]);
    }

    /**
     * @return string[]
     */
    public function getResponseFormatAccessoryRules(): array
    {
        return $this->getStringList('response_format.accessory_rules', [
            '- If the user asks for a matching accessory, separate the answer into: main device and matching accessory.',
            '- The main device must come first. The accessory must not replace the main device.',
            '- Only name an accessory as matching if compatibility is explicitly grounded in the provided sources.',
            '- Do not call accessories, indicators, reagents, kits, sets, or consumables a device, measuring device, or main product unless the source explicitly says so.',
        ]);
    }

    public function getLanguageRulesSectionLabel(): string
    {
        return $this->getString('sections.language_rules_label', 'LANGUAGE RULES');
    }

    /**
     * @return string[]
     */
    public function getLanguageRules(): array
    {
        return $this->getStringList('language.rules', [
            '- Answer only in the same language as the user question.',
            '- All headings, labels, notes, and structural elements must be in the same language as the user question.',
            '- Do not switch languages unless the user does.',
            '- If headings are used, write them in the user\'s language.',
        ]);
    }

    public function getFactGroundingRulesSectionLabel(): string
    {
        return $this->getString('sections.fact_grounding_rules_label', 'FACT GROUNDING RULES');
    }

    /**
     * @return string[]
     */
    public function getFactGroundingBaseRules(): array
    {
        return $this->getStringList('fact_grounding.base_rules', [
            '- State only facts that are explicitly present in the provided sources.',
            '- Extract concrete values exactly when they are present, including units, ranges, model names, indicator names, IP classes, temperatures, pressures, dimensions, counts, relay outputs, current outputs, and error codes.',
            '- Do not invent missing values.',
            '- Do not replace missing values with estimates, defaults, or typical industry assumptions.',
            '- Do not claim that information is missing if it appears in the provided sources.',
            '- Do not compare with other products unless those products are also present in the provided sources.',
            '- Prefer source-faithful wording over persuasive wording.',
            "- Avoid marketing language such as 'ideal', 'perfect', 'unverzichtbar', 'entscheidend', 'optimal', 'kosteneffizient', 'prozesssicher', or 'state-of-the-art'.",
            '- Clearly separate explicit facts from inferences.',
            "- If a conclusion goes beyond the source wording, label it exactly as 'Inference:'.",
            '- If a sentence cannot be traced to the provided sources, do not write it.',
            '- For follow-up questions, use the conversation only to resolve what the user refers to; do not copy technical facts from previous assistant answers unless the same fact is present in the current retrieved sources.',
            '- Never mention external manufacturers, external brands, or external products unless they are explicitly present in the provided sources.',
            '- If the sources do not identify a suitable product, do not invent one.',
            '- Do not turn absence of evidence into a broad portfolio statement. Use scoped wording tied to the provided sources and current search results.',
            '- Strong negative terms such as "ausschließlich", "keines", "nicht geeignet", or "gibt es nicht" require explicit source support for the full stated scope.',
        ]);
    }

    /**
     * @return string[]
     */
    public function getFactGroundingWithShopRules(): array
    {
        return $this->getStringList('fact_grounding.with_shop_rules', [
            '- Use shop data as highest priority for current commercial fields: price, availability, URL, current shop-visible naming, and explicitly shop-visible product suitability for product-selection questions.',
            '- Use retrieved knowledge as highest priority for technical matching, thresholds, measurement principles, and technical explanation when it contains a matching product or fact.',
            '- If retrieved knowledge is silent or only contains unrelated products, but live shop results explicitly match the requested parameter/application, use the shop results and do not answer with a negative RAG-only conclusion.',
            '- For product-selection questions, a shop result proves technical suitability only when the same SHOP PRODUCT RECORD explicitly states the requested measurement parameter, application, or compatibility. Search ranking, generated query terms, generic category matches, and similar wording are not proof.',
            '- If the requested parameter appears only in the generated shop query, metadata, unrelated highlights, or another product record, treat suitability as unverified and say that the shop hit requires technical verification.',
            '- Do not convert p-Wert, m-Wert, minus m-Wert, alkalinity, acid capacity, or other water-treatment parameters into pH or pH-Wert unless the same source explicitly says pH or pH-Wert.',
            '- When shop results are present and relevant, include current price and the actual URL if available.',
            '- If the shop data does not provide a positive price for a result, do not output any price for that result.',
            '- Do not let accessories, bundles, or service items override a technically better product match unless the user explicitly asks for them.',
            '- Do not call accessories, indicators, reagents, kits, sets, or consumables a device, measuring device, or main product unless the source explicitly says so.',
            '- Do not claim that an accessory is required, necessary, used for calibration, or sets the measurement range unless this is explicitly stated in the provided sources.',
            '- Do not assign the product number, price, URL, or availability of a reagent, accessory, kit, set, consumable, or service item to a device identified in retrieved knowledge.',
            '- Only use commercial fields for the main product when the shop item and the technically identified product clearly refer to the same product identity.',
            '- If the shop match is ambiguous, keep the technical identification and commercial details separate.',
            '- Shop product names are authoritative for their own shop URL, product number, price, availability, image, description, and metadata.',
            '- Do not rewrite a shop record heading with a similar device name from retrieved knowledge. If identities differ or are uncertain, separate the RAG device from the shop hit.',
            '- If the user asks for a main device, measuring device, analyzer, system, or measuring installation, do not present an accessory, indicator, reagent, kit, set, consumable, or service item as the requested main solution.',
            '- If the only shop hit is role-incompatible with the requested product role, state that no matching main-device shop hit is available in the provided shop data; mention the incompatible hit only as a separate accessory/consumable hit if useful.',
            '- Never rename a role-incompatible accessory shop record into a main device in headings, summaries, or shop-hit lines.',
        ]);
    }

    /**
     * @return string[]
     */
    public function getFactGroundingWithoutShopRules(): array
    {
        return $this->getStringList('fact_grounding.without_shop_rules', [
            '- Use retrieved knowledge as authoritative for factual answers.',
            '- If no shop results are present, do not compensate with external recommendations or external product suggestions.',
        ]);
    }

    /**
     * @return string[]
     */
    public function getFactGroundingTechnicalRules(): array
    {
        return $this->getStringList('fact_grounding.technical_rules', [
            '- For technical product questions, answer primarily with explicitly stated facts.',
            '- For measurement-parameter questions, do not treat similar or neighboring abbreviations as equivalent. In particular, p-Wert is not pH-Wert unless the source explicitly says pH or pH-Wert.',
            '- Do not invent or infer measurement principles, methods, calibration functions, benefits, advantages, application areas, or alternative products from product family names, search rank, or shop query wording.',
            '- Behave like a technical documentation assistant, not like a sales advisor.',
            '- Keep interpretations minimal and do not generalize application areas beyond the provided sources.',
            '- Do not describe benefits, consequences, risks, or operational outcomes unless they are explicitly stated in the sources.',
            '- Do not translate technical facts into business value unless the source explicitly does so.',
            '- Do not recommend process changes unless explicitly present in the source.',
            '- Do not use persuasive summaries or advisory conclusions.',
            '- If the retrieved knowledge describes one specific named product, stay within that product and do not merge related product families or variants.',
            '- Use neutral engineering language.',
            '- Do not name specific chemicals, indicator substances, standards, or mechanisms unless explicitly stated in the source.',
            '- If the source states signal logic such as green/red, output that signal logic only and do not expand it into operational recommendations or alarm semantics unless explicitly stated.',
            '- If the source lists application areas, repeat only those areas and do not broaden them.',
            '- If the source names an indicator and threshold, reproduce that exactly without extrapolation.',
            '- For lowest, highest, smallest, largest, minimum, maximum, Grenzwert, Messbereich or Aufloesung questions, first identify the exact numeric extreme from the retrieved knowledge and answer that value directly.',
            '- For lowest/highest/minimum/maximum questions, answer only the requested extreme unless the user explicitly asks for a comparison or alternatives.',
            '- For direct numeric lookup questions such as which device measures a given threshold, answer with the exact matching device/value pair first and avoid advisory caveats.',
            '- Do not add the runner-up product, second-lowest value, or adjacent range unless the user asks for it.',
            '- Do not add calibration, accuracy, pretreatment, temperature, or application notes unless those exact notes are requested and explicitly present in the retrieved source.',
            '- For follow-up questions such as "which indicator measures that value", first resolve the referenced value/device, then use the retrieved source entry that explicitly connects value, device and indicator.',
            '- For direct follow-up indicator/value questions, start with the exact mapping in one sentence, for example: Der Wert 0,02 °dH wird beim Testomat 808 mit Indikatortyp 300 gemessen.',
            '- Do not output the full indicator table, measurement principle, application areas, or advisory notes unless the user explicitly asks for all indicators, details, a table, or device information.',
            '- For numeric extreme questions, do not combine a value, device name, indicator name, range or product variant from different chunks unless the same retrieved entry explicitly connects them.',
            '- If several devices or indicators are present, keep each device-indicator-range assignment separate and do not transfer an indicator from one product to another.',
            '- If the source states only a threshold function, do not expand it into broader control logic.',
            '- If a detail is not explicitly stated in the provided sources, say so plainly.',
            '- Prefer short, source-close sentences over explanatory expansion.',
            '- If the sources only support that a product family is not suitable, output only that unsuitability and stop there.',
        ]);
    }

    public function getRetrievedKnowledgeSectionLabel(): string
    {
        return $this->getString('sections.retrieved_knowledge_label', 'RETRIEVED KNOWLEDGE (primary for technical matching and factual explanation)');
    }

    public function getRetrievedKnowledgeSourceLine(): string
    {
        return $this->getString('retrieved_knowledge.source_line', 'Source: Documents');
    }

    public function getUrlContentSectionLabel(): string
    {
        return $this->getString('sections.url_content_label', 'CONTENT FROM URL (authoritative if user-provided)');
    }

    public function getUrlContentSourceLine(): string
    {
        return $this->getString('url_content.source_line', 'Source: URL');
    }

    public function getShopProductNumberLabel(): string
    {
        return $this->getString('shop_results.fields.product_number_label', 'Product number');
    }

    public function getShopManufacturerLabel(): string
    {
        return $this->getString('shop_results.fields.manufacturer_label', 'Manufacturer');
    }

    public function getShopPriceLabel(): string
    {
        return $this->getString('shop_results.fields.price_label', 'Price');
    }

    public function getShopAvailabilityLabel(): string
    {
        return $this->getString('shop_results.fields.availability_label', 'Available');
    }

    public function getShopAvailabilityYesLabel(): string
    {
        return $this->getString('shop_results.fields.availability_yes_label', 'yes');
    }

    public function getShopAvailabilityNoLabel(): string
    {
        return $this->getString('shop_results.fields.availability_no_label', 'no');
    }

    public function getShopHighlightPrefix(): string
    {
        return $this->getString('shop_results.fields.highlight_prefix', '- ');
    }

    public function getShopUrlLabel(): string
    {
        return $this->getString('shop_results.fields.url_label', 'URL');
    }

    public function getShopProductImageLabel(): string
    {
        return $this->getString('shop_results.fields.product_image_label', 'Product image');
    }

    public function getShopDescriptionLabel(): string
    {
        return $this->getString('shop_results.fields.description_label', 'Description');
    }

    public function getShopMetaInformationLabel(): string
    {
        return $this->getString('shop_results.fields.meta_information_label', 'Meta information');
    }

    public function getShopRequestedRoleLabel(): string
    {
        return $this->getString('shop_results.fields.requested_role_label', 'Requested product role');
    }

    public function getShopInferredRoleLabel(): string
    {
        return $this->getString('shop_results.fields.inferred_role_label', 'Inferred shop product role');
    }

    public function getShopRoleCompatibilityLabel(): string
    {
        return $this->getString('shop_results.fields.role_compatibility_label', 'Role compatibility with request');
    }

    public function getShopRoleIncompatibleCommercialSuppressionNote(): string
    {
        return $this->getString(
            'shop_results.fields.role_incompatible_commercial_suppression_note',
            'Commercial fields suppressed: this shop record is not a matching main-device result for the requested product role.'
        );
    }

    /**
     * @return string[]
     */
    public function getMainDeviceRequestRoleKeywords(): array
    {
        return $this->getStringList('role_guard.main_device_request_keywords', self::MAIN_DEVICE_REQUEST_ROLE_KEYWORDS);
    }

    /**
     * @return string[]
     */
    public function getMainDeviceProductRoleKeywords(): array
    {
        return $this->getStringList('role_guard.main_device_product_keywords', self::MAIN_DEVICE_PRODUCT_ROLE_KEYWORDS);
    }

    /**
     * @return string[]
     */
    public function getAccessoryProductRoleKeywords(): array
    {
        return $this->getStringList('role_guard.accessory_product_keywords', self::ACCESSORY_PRODUCT_ROLE_KEYWORDS);
    }

    /**
     * @return string[]
     */
    public function getTechnicalProductKeywords(): array
    {
        return $this->getStringList(
            'technical_product_keywords',
            $this->vocabularyView('prompt.technical_product_keywords', self::TECHNICAL_PRODUCT_KEYWORDS)
        );
    }

    /**
     * @return string[]
     */
    public function getAccessoryRequestKeywords(): array
    {
        return $this->getStringList(
            'accessory_request_keywords',
            $this->vocabularyView('prompt.accessory_request_keywords', self::ACCESSORY_REQUEST_KEYWORDS)
        );
    }

    public function getMeasurementEvidenceSectionLabel(): string
    {
        return $this->getString('sections.measurement_evidence_label', 'MEASUREMENT PARAMETER EVIDENCE CHECK');
    }

    /**
     * @return string[]
     */
    public function getMeasurementEvidenceIntroRules(): array
    {
        return $this->getStringList('measurement_evidence_guard.intro_rules', [
            '- This block is generated from the current user question and is stricter than broad product-selection wording.',
            '- For measurement-parameter questions, technical suitability requires explicit positive evidence for the requested parameter in the same source record.',
            '- Similar water-treatment parameters, abbreviations, units, product families, search queries, or ranking positions are not enough.',
        ]);
    }

    /**
     * @return array<int, array<string, mixed>>
     */
    public function getMeasurementEvidenceParameters(): array
    {
        $value = $this->getValue('measurement_evidence_guard.parameters', self::MEASUREMENT_EVIDENCE_PARAMETERS);

        if (!is_array($value)) {
            return self::MEASUREMENT_EVIDENCE_PARAMETERS;
        }

        $out = [];

        foreach ($value as $item) {
            if (!is_array($item)) {
                continue;
            }

            $id = isset($item['id']) && is_scalar($item['id']) ? trim((string) $item['id']) : '';
            $label = isset($item['label']) && is_scalar($item['label']) ? trim((string) $item['label']) : '';

            if ($id === '' || $label === '') {
                continue;
            }

            $out[] = [
                'id' => $id,
                'label' => $label,
                'request_terms' => $this->normalizeMixedStringList($item['request_terms'] ?? []),
                'positive_terms' => $this->normalizeMixedStringList($item['positive_terms'] ?? []),
                'positive_context_terms' => $this->normalizeMixedStringList($item['positive_context_terms'] ?? []),
                'negative_context_terms' => $this->normalizeMixedStringList($item['negative_context_terms'] ?? []),
                'non_equivalent_terms' => $this->normalizeMixedStringList($item['non_equivalent_terms'] ?? []),
                'safe_no_evidence_answer_de' => isset($item['safe_no_evidence_answer_de']) && is_scalar($item['safe_no_evidence_answer_de'])
                    ? trim((string) $item['safe_no_evidence_answer_de'])
                    : '',
                'safe_no_accessory_evidence_answer_de' => isset($item['safe_no_accessory_evidence_answer_de']) && is_scalar($item['safe_no_accessory_evidence_answer_de'])
                    ? trim((string) $item['safe_no_accessory_evidence_answer_de'])
                    : '',
            ];
        }

        return $out !== [] ? $out : self::MEASUREMENT_EVIDENCE_PARAMETERS;
    }

    /**
     * @return string[]
     */
    private function normalizeMixedStringList(mixed $value): array
    {
        if (!is_array($value)) {
            return [];
        }

        $out = [];
        foreach ($value as $item) {
            if (!is_scalar($item)) {
                continue;
            }

            $item = trim((string) $item);
            if ($item !== '' && !in_array($item, $out, true)) {
                $out[] = $item;
            }
        }

        return $out;
    }

    public function getTechnicalProductModelPattern(): string
    {
        return $this->getString('technical_product_model_pattern', '/\b[\p{L}]{2,}\s?\d{2,5}\b/u');
    }
}