"""
Memory Models for Breakpilot Agents

Data classes for memory items used by MemoryStore.
"""

from typing import Dict, Any, Optional
from datetime import datetime, timezone
from dataclasses import dataclass, field


@dataclass
class Memory:
    """Represents a stored memory item"""
    key: str
    value: Any
    agent_id: str
    created_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
    expires_at: Optional[datetime] = None
    access_count: int = 0
    last_accessed: Optional[datetime] = None
    metadata: Dict[str, Any] = field(default_factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        return {
            "key": self.key,
            "value": self.value,
            "agent_id": self.agent_id,
            "created_at": self.created_at.isoformat(),
            "expires_at": self.expires_at.isoformat() if self.expires_at else None,
            "access_count": self.access_count,
            "last_accessed": self.last_accessed.isoformat() if self.last_accessed else None,
            "metadata": self.metadata
        }

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "Memory":
        return cls(
            key=data["key"],
            value=data["value"],
            agent_id=data["agent_id"],
            created_at=datetime.fromisoformat(data["created_at"]),
            expires_at=datetime.fromisoformat(data["expires_at"]) if data.get("expires_at") else None,
            access_count=data.get("access_count", 0),
            last_accessed=datetime.fromisoformat(data["last_accessed"]) if data.get("last_accessed") else None,
            metadata=data.get("metadata", {})
        )

    def is_expired(self) -> bool:
        """Check if the memory has expired"""
        if not self.expires_at:
            return False
        return datetime.now(timezone.utc) > self.expires_at
