"""Core memory (RMM) — read, write, and bootstrap agent memory."""

from __future__ import annotations

from typing import Any, Dict, List, Optional

from .._resource import AsyncResource, Resource


class Memory(Resource):
    """Sync memory operations."""

    def blocks(self) -> Dict[str, Any]:
        """List all memory blocks with entry counts."""
        return self._get("/v1/memory/blocks")

    def block(self, block: str) -> Dict[str, Any]:
        """List entries in a specific block."""
        return self._get(f"/v1/memory/blocks/{block}")

    def entries(self) -> Dict[str, Any]:
        """List all core memory entries."""
        return self._get("/v1/memory/entries")

    def append(
        self,
        fact: str,
        *,
        block: Optional[str] = None,
        pinned: Optional[bool] = None,
    ) -> Dict[str, Any]:
        """Append a new memory entry."""
        body: Dict[str, Any] = {"fact": fact}
        if block is not None:
            body["block"] = block
        if pinned is not None:
            body["pinned"] = pinned
        return self._post("/v1/memory/entries", body)

    def update(
        self,
        entry_id: str,
        *,
        fact: Optional[str] = None,
        pinned: Optional[bool] = None,
    ) -> Dict[str, Any]:
        """Update a memory entry."""
        body: Dict[str, Any] = {}
        if fact is not None:
            body["fact"] = fact
        if pinned is not None:
            body["pinned"] = pinned
        return self._put(f"/v1/memory/entries/{entry_id}", body)

    def remove(self, entry_id: str) -> Dict[str, Any]:
        """Delete a memory entry."""
        return self._delete(f"/v1/memory/entries/{entry_id}")

    def init(
        self,
        documents: List[Dict[str, str]],
        *,
        model: Optional[str] = None,
    ) -> Dict[str, Any]:
        """Bootstrap memory from context documents."""
        body: Dict[str, Any] = {"documents": documents}
        if model is not None:
            body["model"] = model
        return self._post("/v1/memory/init", body)


class AsyncMemory(AsyncResource):
    """Async memory operations."""

    async def blocks(self) -> Dict[str, Any]:
        return await self._get("/v1/memory/blocks")

    async def block(self, block: str) -> Dict[str, Any]:
        return await self._get(f"/v1/memory/blocks/{block}")

    async def entries(self) -> Dict[str, Any]:
        return await self._get("/v1/memory/entries")

    async def append(
        self,
        fact: str,
        *,
        block: Optional[str] = None,
        pinned: Optional[bool] = None,
    ) -> Dict[str, Any]:
        body: Dict[str, Any] = {"fact": fact}
        if block is not None:
            body["block"] = block
        if pinned is not None:
            body["pinned"] = pinned
        return await self._post("/v1/memory/entries", body)

    async def update(
        self,
        entry_id: str,
        *,
        fact: Optional[str] = None,
        pinned: Optional[bool] = None,
    ) -> Dict[str, Any]:
        body: Dict[str, Any] = {}
        if fact is not None:
            body["fact"] = fact
        if pinned is not None:
            body["pinned"] = pinned
        return await self._put(f"/v1/memory/entries/{entry_id}", body)

    async def remove(self, entry_id: str) -> Dict[str, Any]:
        return await self._delete(f"/v1/memory/entries/{entry_id}")

    async def init(
        self,
        documents: List[Dict[str, str]],
        *,
        model: Optional[str] = None,
    ) -> Dict[str, Any]:
        body: Dict[str, Any] = {"documents": documents}
        if model is not None:
            body["model"] = model
        return await self._post("/v1/memory/init", body)
