from __future__ import annotations

import json

from app.modules.agent.code_qa_runtime.models import CodeQaValidationResult
from app.modules.agent.llm import AgentLlmService


class CodeQaAnswerRepairService:
    def __init__(self, llm: AgentLlmService) -> None:
        self._llm = llm

    def repair(
        self,
        *,
        draft_answer: str,
        validation: CodeQaValidationResult,
        prompt_payload: str,
    ) -> str:
        repair_input = json.dumps(
            {
                "draft_answer": draft_answer,
                "validation_reasons": validation.reasons,
                "prompt_payload": prompt_payload,
            },
            ensure_ascii=False,
            indent=2,
        )
        return self._llm.generate(
            "code_qa_repair_answer",
            repair_input,
            log_context="graph.project_qa.code_qa.repair",
        ).strip()
