"""Wiki graph lint — orphan nodes · 弱边。"""

from __future__ import annotations

from typing import Any

from niannian.brain.cerebellum.wiki_store import WikiStore


class WikiLintService:
    def __init__(self, store) -> None:
        self._wiki = WikiStore(store)

    def run(self, *, weak_edge_threshold: float = 0.35) -> dict[str, Any]:
        graph = self._wiki.load_graph()
        nodes = graph.get("nodes") or {}
        edges = graph.get("edges") or []
        page_ids = set(self._wiki.pages_map().keys())
        orphans = [pid for pid in nodes.keys() if pid not in page_ids]
        weak = [
            e
            for e in edges
            if isinstance(e, dict) and float(e.get("weight") or 0) < weak_edge_threshold
        ]
        return {
            "orphan_nodes": orphans,
            "weak_edges": weak[:50],
            "node_count": len(nodes),
            "edge_count": len(edges),
        }
