from __future__ import annotations

from app.modules.rag.contracts import EvidenceLink, EvidenceType, RagDocument, RagLayer, RagSource, RagSpan
from app.modules.rag.indexing.code.edges.extractor import PyEdge
from app.modules.rag.retrieval.test_filter import is_test_path


class EdgeDocumentBuilder:
    def build(self, source: RagSource, edge: PyEdge) -> RagDocument:
        dst = edge.dst_ref or edge.dst_symbol_id or "unknown"
        return RagDocument(
            layer=RagLayer.CODE_DEPENDENCY_GRAPH,
            lang="python",
            source=source,
            title=f"{edge.src_qname}:{edge.edge_type}",
            text=f"{edge.src_qname} {edge.edge_type} {dst}",
            span=RagSpan(edge.start_line, edge.end_line),
            metadata={
                "edge_id": edge.edge_id,
                "edge_type": edge.edge_type,
                "src_symbol_id": edge.src_symbol_id,
                "src_qname": edge.src_qname,
                "dst_symbol_id": edge.dst_symbol_id,
                "dst_ref": edge.dst_ref,
                "resolution": edge.resolution,
                "is_test": is_test_path(source.path),
            },
            links=[EvidenceLink(type=EvidenceType.EDGE, target_id=edge.edge_id, path=source.path, start_line=edge.start_line, end_line=edge.end_line)],
        )
