from __future__ import annotations

from dataclasses import dataclass, field
from typing import Optional, List, Protocol


class FactResolver(Protocol):
    def resolve(self, fact_id: str) -> Optional["Fact"]:
        ...


@dataclass
class Fact:
    id: str
    turn_id: Optional[str]
    statement_id: Optional[str]
    user_id: str
    agent_id: str
    text: str
    embedding: Optional[List[float]]
    notion_id: Optional[str] = None
    reference_type: Optional[str] = None
    prev_id: Optional[str] = None
    next_id: Optional[str] = None
    _resolver: Optional[FactResolver] = field(
        default=None,
        repr=False,
        compare=False,
    )

    def __str__(self) -> str:
        return self.text or ""

    def prev(self) -> Optional["Fact"]:
        if self._resolver is None or not self.prev_id:
            return None
        return self._resolver.resolve(self.prev_id)

    def next(self) -> Optional["Fact"]:
        if self._resolver is None or not self.next_id:
            return None
        return self._resolver.resolve(self.next_id)
