"""联网自学批次 → vault/niannian/ MD。"""

from __future__ import annotations

from typing import Any

from niannian.soul.search_providers import SearchHit
from niannian.vault.store import VaultStore


def write_niannian_web_md(
    store: VaultStore,
    hit: SearchHit,
    *,
    task: dict[str, Any],
    batch_id: str,
) -> dict[str, Any]:
    topic_id = str(task.get("topic_id") or "web")
    topic_label = str(task.get("topic_label") or task.get("label") or topic_id)
    body = (
        f"## 摘要\n\n{hit.snippet.strip()}\n\n"
        f"## 要点\n\n- {hit.title}\n\n"
        f"## 来源说明\n\n{hit.url}\n\n"
        f"## 正文\n\n{hit.snippet.strip()}"
    )
    return store.write_document(
        namespace="niannian",
        title=hit.title,
        body=body,
        frontmatter={
            "source_type": "crawl",
            "source_url": hit.url,
            "trust_tier": "web",
            "extracted_by": "niannian",
            "category": topic_label,
            "subproject": topic_id,
            "taxonomy_status": "confirmed",
            "web_batch_id": batch_id,
            "page_type": "sources",
        },
    )
