from __future__ import annotations

import json

from app.modules.rag.code_qa_pipeline.contracts import AnswerSynthesisInput, EvidenceBundle

_LAYER_GUIDE = (
    "- C0_SOURCE_CHUNKS: фактический код, это основной источник деталей.\n"
    "- C1_SYMBOL_CATALOG: объявления и сигнатуры символов.\n"
    "- C2_DEPENDENCY_GRAPH: связи вызовов и зависимостей.\n"
    "- C3_ENTRYPOINTS: подтвержденные точки входа.\n"
    "- C4_SEMANTIC_ROLES: вспомогательная роль компонента, использовать осторожно."
)


class CodeQaPromptPayloadBuilder:
    def build(
        self,
        *,
        user_query: str,
        synthesis_input: AnswerSynthesisInput,
        evidence_pack: EvidenceBundle,
        answer_mode: str,
    ) -> str:
        payload = {
            "user_query": user_query,
            "resolved_scenario": synthesis_input.resolved_scenario,
            "resolved_target": synthesis_input.resolved_target,
            "answer_mode": answer_mode,
            "fast_context": synthesis_input.fast_context,
            "deep_context": synthesis_input.deep_context,
            "evidence_summary": synthesis_input.evidence_summary,
            "diagnostic_hints": synthesis_input.diagnostic_hints,
            "retrieval_summary": evidence_pack.retrieval_summary,
            "layer_guide": _LAYER_GUIDE,
        }
        return json.dumps(payload, ensure_ascii=False, indent=2)
