"""Memory forget tool — delete a fact from long-term memory."""

from navi.memory.store import MemoryStore
from navi.tools.base import current_user_id

from .base import Tool, ToolResult


class MemoryForgetTool(Tool):
    name = "memory_forget"
    description = (
        "Delete a fact from your long-term memory. "
        "Use when the user explicitly asks you to forget something, "
        "or when you know a stored fact is outdated or incorrect."
    )
    parameters = {
        "type": "object",
        "properties": {
            "key": {
                "type": "string",
                "description": "The key of the fact to delete (e.g. 'location', 'employer', 'home_server')",
            },
            "category": {
                "type": "string",
                "description": "Optional: narrow by category (profile, preferences, technical, projects, other)",
            },
        },
        "required": ["key"],
    }

    def __init__(self, memory_store: MemoryStore) -> None:
        self._store = memory_store

    async def execute(self, params: dict) -> ToolResult:
        key = params.get("key", "").strip()
        category = params.get("category", "").strip() or None

        if not key:
            return ToolResult(success=False, output="Key is required.", error="missing key")

        user_id = current_user_id.get(None)
        deleted = await self._store.delete_fact(key, category, user_id=user_id)
        if deleted == 0:
            return ToolResult(success=False, output=f"No fact found with key '{key}'.", error="not found")

        noun = "fact" if deleted == 1 else "facts"
        return ToolResult(success=True, output=f"Deleted {deleted} {noun} with key '{key}'.")
