from __future__ import annotations

from dataclasses import dataclass


@dataclass(slots=True, frozen=True)
class DocsFrontmatterView:
    raw: dict

    @property
    def document_id(self) -> str:
        return str(self.raw.get("id") or "").strip()

    @property
    def doc_type(self) -> str:
        return str(self.raw.get("type") or self.raw.get("doc_type") or "").strip()

    @property
    def name(self) -> str:
        return str(self.raw.get("name") or "").strip()

    @property
    def module(self) -> str:
        return str(self.raw.get("module") or "").strip()

    @property
    def domain(self) -> str:
        return str(self.raw.get("domain") or "").strip()

    @property
    def subdomain(self) -> str:
        return str(self.raw.get("subdomain") or self.raw.get("sub_domain") or "").strip()

    @property
    def layer(self) -> str:
        return str(self.raw.get("layer") or "").strip()

    @property
    def status(self):
        return self.raw.get("status")

    @property
    def updated_at(self):
        return self.raw.get("updated_at")

    @property
    def tags(self) -> list[str]:
        return self._string_list(self.raw.get("tags"))

    @property
    def entities(self) -> list[str]:
        return self._string_list(self.raw.get("entities"))

    @property
    def related_docs(self) -> list[str]:
        return self._string_list(self.raw.get("related_docs"))

    @property
    def parent(self) -> str | None:
        value = str(self.raw.get("parent") or "").strip()
        return value or None

    @property
    def children(self) -> list[str]:
        return self._string_list(self.raw.get("children"))

    @property
    def links(self):
        value = self.raw.get("links")
        if isinstance(value, (dict, list)):
            return value
        return {}

    def title(self, fallback: str = "") -> str:
        return str(self.raw.get("title") or "").strip() or fallback

    def _string_list(self, value) -> list[str]:
        if not isinstance(value, list):
            return []
        result: list[str] = []
        for item in value:
            normalized = str(item or "").strip()
            if normalized and normalized not in result:
                result.append(normalized)
        return result
