from __future__ import annotations

import json

from app.modules.agent.runtime.docs_qa_pipeline.models import DocsEvidenceBundle, OpenAPIResult


class DocsPromptPayloadBuilder:
    def build(
        self,
        *,
        question: str,
        intent: str,
        sub_intent: str,
        evidence_bundle: DocsEvidenceBundle,
        api_contract: OpenAPIResult | None = None,
    ) -> str:
        payload = {
            "question": question,
            "documents": list(evidence_bundle.documents),
            "facts": list(evidence_bundle.facts),
            "entities": list(evidence_bundle.entities),
            "workflows": list(evidence_bundle.workflows),
            "relations": list(evidence_bundle.relations),
            "chunks": list(evidence_bundle.chunks),
        }
        if api_contract is not None:
            payload["api_contract"] = {
                "path": api_contract.path,
                "method": api_contract.method,
                "request_schema": api_contract.request_schema,
                "response_schema": api_contract.response_schema,
                "diagnostics": dict(api_contract.diagnostics),
            }
        return json.dumps(payload, ensure_ascii=False, indent=2)
