"""MemoryNode — the fundamental unit of memory in LociDB."""

from __future__ import annotations

import uuid
from datetime import datetime, timezone
from typing import List, Optional

from pydantic import BaseModel, Field


class MemoryNode(BaseModel):
    """A single memory node in the cognitive graph.

    Each node holds raw or summarised content, its vector embedding,
    importance metadata, and entity/scope tags that position it in
    the knowledge graph.
    """

    id: str = Field(default_factory=lambda: uuid.uuid4().hex)
    content: str
    embedding: List[float] = Field(default_factory=list)
    importance: float = Field(default=0.5, ge=0.0, le=1.0)
    timestamp: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
    entity_tags: List[str] = Field(default_factory=list)
    context_scope: str = Field(default="global")
    summary_level: int = Field(default=0, ge=0, le=3)
    access_count: int = Field(default=0)
    last_accessed: Optional[datetime] = None
    memory_type: str = Field(default="memory")
    deleted_at: Optional[datetime] = None

    model_config = {"frozen": False}

    def touch(self) -> None:
        """Record an access — bumps count and updates last-accessed timestamp."""
        self.access_count += 1
        self.last_accessed = datetime.now(timezone.utc)
