"""文史 / crawl 批次 → 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_cultural_md(
    store: VaultStore,
    hit: SearchHit,
    *,
    task: dict[str, Any],
    batch_id: str,
) -> dict[str, Any]:
    domain_id = str(task.get("domain_id") or "自学批次")
    domain_label = str(task.get("domain_label") or "文史")
    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": "curated",
            "extracted_by": "niannian",
            "category": "文史",
            "subproject": domain_id,
            "taxonomy_status": "confirmed",
            "cultural_batch_id": batch_id,
            "cultural_domain": domain_label,
        },
    )
