from __future__ import annotations

import json

from app.services.consistency.schema import RequirementSnapshot


SYSTEM_INSTRUCTION = """你是需求代码一致性审查助手。
只能基于输入的需求、验收准则、函数摘要、代码片段、调用链证据判断。
不得补充未给出的代码事实。
证据不足时输出 uncertain。
输出严格 JSON，不要 Markdown。"""


def build_requirement_query(requirement: RequirementSnapshot) -> str:
    parts = []
    req_type = (requirement.requirement_type or "").lower()
    if req_type == "interface":
        parts.extend(
            [
                requirement.interface_name or "",
                requirement.interface_type or "",
                requirement.data_source or "",
                requirement.data_destination or "",
                requirement.description,
            ]
        )
    else:
        parts.extend(
            [
                requirement.description,
                "\n".join(requirement.acceptance_criteria),
                requirement.section_title or "",
                requirement.interface_name or "",
                requirement.data_source or "",
                requirement.data_destination or "",
            ]
        )
    return "\n".join(part for part in parts if part).strip()


def build_judgment_prompt(requirement: RequirementSnapshot, evidence_context: str) -> str:
    payload = {
        "requirement": requirement.to_dict(),
        "evidence": evidence_context,
        "output_schema": {
            "verdict": "implemented | partial | missing | conflict | uncertain",
            "confidence": 0.0,
            "covered_points": [],
            "missing_points": [],
            "conflict_points": [],
            "primary_evidence": [],
            "reasoning": "brief reason based only on evidence",
            "suggestion": "next action",
        },
    }
    return SYSTEM_INSTRUCTION + "\n\n" + json.dumps(payload, ensure_ascii=False, indent=2)

