from __future__ import annotations

from dataclasses import dataclass, field


@dataclass(slots=True)
class DocEntry:
    path: str
    doc_id: str


@dataclass(slots=True)
class DocsState:
    by_path: set[str] = field(default_factory=set)
    by_doc_id: dict[str, str] = field(default_factory=dict)

    @classmethod
    def from_rows(cls, rows: list[dict]) -> "DocsState":
        state = cls()
        for row in rows:
            path = str(row.get("path") or "").strip()
            if not path:
                continue
            state.by_path.add(path)
            metadata = row.get("metadata") if isinstance(row.get("metadata"), dict) else {}
            raw_doc_id = metadata.get("id") or metadata.get("doc_id") or metadata.get("document_id")
            doc_id = str(raw_doc_id or "").strip()
            if doc_id and doc_id not in state.by_doc_id:
                state.by_doc_id[doc_id] = path
        return state
