"""Prompts for unified document analysis (metadata extraction + summary generation).

将元数据提取和摘要生成合并为一次 LLM 调用的 Prompt 模板。
"""

from app.core.graph_schema_loader import get_schema

# ---------------------------------------------------------------------------
# System prompt
# ---------------------------------------------------------------------------

SYSTEM_PROMPT = """\
你是政务公文分析专家。从用户提供的公文内容中，同时完成两项任务：
1. 提取结构化元数据字段
2. 生成简明中文摘要

以 JSON 对象返回，不含任何解释文字。"""

# ---------------------------------------------------------------------------
# User prompt (use build_user_prompt(content, max_chars) to fill in)
# ---------------------------------------------------------------------------

_USER_PROMPT_TEMPLATE = """\
请从以下政务公文内容中提取元数据并生成摘要。

公文内容（截取前 {max_chars} 字）：
---
{content}
---

请返回如下 JSON 对象。元数据字段缺失时返回空字符串或空列表，日期统一为 YYYY-MM-DD 格式。\
摘要要求 150～250 字，涵盖文件主要目的、核心政策措施、适用范围，使用政务文体，第三人称叙述，纯文本不分段。

{{{{
  "metadata": {{{{
    "title":        "文件完整标题",
    "doc_number":   "完整文号，如「国办发〔2024〕1号」",
    "issuing_org":  "发文机关全称",
    "doc_type":     "公文文种，仅限：{doc_types}",
    "document_scene_type": "standard_service_guide 或 other，仅当文档明显属于标准办事指南时返回 standard_service_guide",
    "publish_date": "YYYY-MM-DD，无法确定则空字符串",
    "signer":       "签发人姓名，无则空字符串",
    "subject_words": ["主题词1", "主题词2"]
  }}}},
  "summary": "150-250字的摘要正文"
}}}}"""


def build_user_prompt(content: str, max_chars: int) -> str:
    """Build the unified analysis user prompt with dynamic doc_types."""
    schema = get_schema()
    doc_types_str = (
        "/".join(schema.doc_types)
        if schema.doc_types
        else "通知/决定/通报/报告/请示/批复/函/纪要/意见/公告/其他"
    )
    return _USER_PROMPT_TEMPLATE.format(
        doc_types=doc_types_str,
        content=content,
        max_chars=max_chars,
    )
