from typing import Optional, Dict
from pydantic import BaseModel, Field

from .source import SourceReference

class Fact(BaseModel):
    """A discrete piece of factual information retrieved from sources."""
    statement: str = Field(..., description="The factual statement")
    sources: list[SourceReference] = Field(..., description="Source references supporting this fact")
    confidence: Optional[float] = Field(None, ge=0.0, le=1.0, description="Confidence in the accuracy of this fact (0-1)")


class KnowledgeCluster(BaseModel):
    """A cluster of related facts about a specific topic or concept."""
    topic: str = Field(..., description="The topic or concept this knowledge cluster is about")
    summary: str = Field(..., description="A summary of the knowledge in this cluster")
    facts: list[Fact] = Field(..., description="List of facts comprising this knowledge cluster")
    related_topics: Optional[list[str]] = Field(None, description="Related topics that might be relevant")


class Understanding(BaseModel):
    """A collection of knowledge retrieved and synthesized from multiple sources."""
    query: str = Field(..., description="The original query or question")
    knowledge_clusters: list[KnowledgeCluster] = Field(..., description="Organized clusters of knowledge supporting the answer")
    suporting_conclusions: list[str] = Field(..., description="The conclusions that gives strength to the answer")
    all_sources: list[SourceReference] = Field(..., description="All unique sources referenced in this knowledge base")
    metadata: Optional[Dict[str, str]] = Field(None, description="Additional metadata about the knwoledge meting retreived")
    answer: str = Field(..., description="The synthesized answer to the query")
