from __future__ import annotations

from app.core.rag.contracts import EvidenceLink, EvidenceType, RagDocument, RagLayer, RagSource
from app.core.rag.indexing.docs.chunkers.markdown_chunker import SectionChunk
from app.core.rag.indexing.docs.frontmatter_metadata import merge_frontmatter_metadata
from app.core.rag.indexing.docs.frontmatter_view import DocsFrontmatterView


class DocsDocumentBuilder:
    def build_document_catalog(self, source: RagSource, frontmatter: dict, summary_text: str, doc_kind: str, *, fallback_title: str) -> RagDocument:
        view = DocsFrontmatterView(frontmatter)
        metadata = merge_frontmatter_metadata({}, frontmatter)
        if view.document_id:
            metadata["document_id"] = view.document_id
        metadata["source_path"] = source.path
        metadata["summary_text"] = summary_text[:4000]
        metadata["doc_kind"] = doc_kind
        row_title = str(frontmatter.get("title") or "").strip() or fallback_title or source.path
        return RagDocument(
            layer=RagLayer.DOCS_DOCUMENT_CATALOG,
            source=source,
            title=row_title,
            text=summary_text[:4000] or row_title,
            metadata=metadata,
        )

    def build_doc_chunk(self, source: RagSource, chunk: SectionChunk, frontmatter: dict, doc_kind: str) -> RagDocument:
        view = DocsFrontmatterView(frontmatter)
        document_id = view.document_id or source.path
        metadata = merge_frontmatter_metadata(
            {
            "document_id": document_id,
            "type": view.doc_type,
            "module": view.module,
            "domain": view.domain,
            "subdomain": view.subdomain,
            "tags": view.tags,
            "section_path": chunk.section_path,
            "section_title": chunk.section_title,
            "order": chunk.order,
            "doc_kind": doc_kind,
            "source_path": source.path,
            "artifact_type": "DOCS",
            },
            frontmatter,
        )
        return RagDocument(
            layer=RagLayer.DOCS_DOC_CHUNKS,
            source=source,
            title=f"{document_id}:{chunk.section_title}",
            text=chunk.content,
            metadata=metadata,
            links=[EvidenceLink(type=EvidenceType.DOC_SECTION, target_id=document_id, path=source.path, note=chunk.section_path)],
        )

    def build_entity_record(self, source: RagSource, frontmatter: dict, entity: str) -> RagDocument:
        view = DocsFrontmatterView(frontmatter)
        document_id = view.document_id or source.path
        metadata = merge_frontmatter_metadata(
            {
            "entity_name": entity,
            "document_id": document_id,
            "document_type": view.doc_type,
            "module": view.module,
            "domain": view.domain,
            "subdomain": view.subdomain,
            "tags": view.tags,
            "source_path": source.path,
            "artifact_type": "DOCS",
            },
            frontmatter,
        )
        return RagDocument(
            layer=RagLayer.DOCS_ENTITY_CATALOG,
            source=source,
            title=entity,
            text=entity,
            metadata=metadata,
        )
