from __future__ import annotations

from dataclasses import asdict, dataclass, field
from typing import Any


@dataclass
class Atom:
    """记忆原子。

    meta 扩展字段（v0.10）：
    - belief_status: active | superseded | retracted | hypothesis
    - vault: {namespace, path, vault_id} 资料库指针
    """

    id: str
    raw_text: str
    tags: list[str]
    source: str
    project: str
    created_at: str
    status: str = "raw"
    meta: dict[str, Any] = field(default_factory=dict)

    def to_dict(self) -> dict[str, Any]:
        return asdict(self)


@dataclass
class WikiHit:
    concept: str
    summary: str
    links: list[str] = field(default_factory=list)
    atom_ids: list[str] = field(default_factory=list)
    score: float = 0.0
    page_id: str = ""
    source: str = "keyword"


@dataclass
class QueryResult:
    answer: str
    wiki_hits: list[WikiHit] = field(default_factory=list)
    summary_snippets: list[str] = field(default_factory=list)
    citations: list[dict[str, Any]] = field(default_factory=list)
    trace: list[str] = field(default_factory=list)

    def to_dict(self) -> dict[str, Any]:
        return {
            "answer": self.answer,
            "wiki_hits": [asdict(h) for h in self.wiki_hits],
            "summary_snippets": self.summary_snippets,
            "citations": self.citations,
            "trace": self.trace,
        }


@dataclass
class AssistResult:
    """碎片化思考辅助输出 — 供宿主项目注入 prompt 的上下文包。"""

    context_block: str
    fragments: list[str] = field(default_factory=list)
    wiki_links: list[str] = field(default_factory=list)
    citations: list[dict[str, Any]] = field(default_factory=list)
    trace: list[str] = field(default_factory=list)

    def to_dict(self) -> dict[str, Any]:
        return asdict(self)
