"""Memory hook service for registering memory-related hooks.

This service handles:
1. Memory hook registration with the hook service
2. Memory management integration
3. Hook lifecycle management

Extracted from ClaudeRunner to follow Single Responsibility Principle.
"""

from typing import Any, Dict

from claude_mpm.core.base_service import BaseService
from claude_mpm.services.core.interfaces import MemoryHookInterface


class MemoryHookService(BaseService, MemoryHookInterface):
    """Service for managing memory-related hooks."""

    def __init__(self, hook_service=None):
        """Initialize the memory hook service.

        Args:
            hook_service: Hook service for registering hooks
        """
        super().__init__(name="memory_hook_service")
        self.hook_service = hook_service
        self.registered_hooks = []  # Track registered hook IDs

    async def _initialize(self) -> None:
        """Initialize the service. No special initialization needed."""
        pass

    async def _cleanup(self) -> None:
        """Cleanup service resources. No cleanup needed."""
        pass

    def register_memory_hooks(self):
        """Register memory-related hooks with the hook service.

        WHY: Memory management is a cross-cutting concern that needs to be
        integrated at various points in the Claude interaction lifecycle.
        These hooks ensure memory is properly managed and persisted.

        DESIGN DECISION: We register hooks for key lifecycle events:
        - Before Claude interaction: Load relevant memories
        - After Claude interaction: Save new memories
        - On error: Ensure memory state is preserved
        """
        if not self.hook_service:
            self.logger.debug(
                "Hook service not available, skipping memory hook registration"
            )
            return

        try:
            # Create hook objects for the actual HookService interface
            from claude_mpm.hooks.base_hook import (
                HookContext,
                HookResult,
                PostDelegationHook,
                PreDelegationHook,
            )

            # Create memory loading hook
            class MemoryLoadHook(PreDelegationHook):
                def __init__(self, memory_service):
                    super().__init__(name="memory_load", priority=10)
                    self.memory_service = memory_service

                def execute(self, context: HookContext) -> HookResult:
                    return self.memory_service._load_relevant_memories_hook(context)

            # Create memory saving hook
            class MemorySaveHook(PostDelegationHook):
                def __init__(self, memory_service):
                    super().__init__(name="memory_save", priority=90)
                    self.memory_service = memory_service

                def execute(self, context: HookContext) -> HookResult:
                    return self.memory_service._save_new_memories_hook(context)

            # Register the hook objects
            load_hook = MemoryLoadHook(self)
            save_hook = MemorySaveHook(self)

            success1 = self.hook_service.register_hook(load_hook)
            success2 = self.hook_service.register_hook(save_hook)

            if success1:
                self.registered_hooks.append("memory_load")
            if success2:
                self.registered_hooks.append("memory_save")

            self.logger.debug("Memory hooks registered successfully")

        except Exception as e:
            self.logger.warning(f"Failed to register memory hooks: {e}")

    def _load_relevant_memories_hook(self, context):
        """Hook function to load relevant memories before Claude interaction.

        Args:
            context: Hook context containing interaction details

        Returns:
            HookResult with success status and any modifications
        """
        try:
            # This would integrate with a memory service to load relevant memories
            # For now, this is a placeholder for future memory integration
            self.logger.debug("Loading relevant memories for interaction")

            # Example: Load memories based on context
            # if hasattr(context, 'prompt'):
            #     relevant_memories = memory_service.search_memories(context.prompt)
            #     context.memories = relevant_memories

            from claude_mpm.hooks.base_hook import HookResult

            return HookResult(success=True, data=context.data, modified=False)

        except Exception as e:
            self.logger.warning(f"Failed to load memories: {e}")
            from claude_mpm.hooks.base_hook import HookResult

            return HookResult(
                success=False, data=context.data, modified=False, error=str(e)
            )

    def _load_relevant_memories(self, context):
        """Legacy hook function for backward compatibility."""
        result = self._load_relevant_memories_hook(context)
        return result.data

    def _save_new_memories_hook(self, context):
        """Hook function to save new memories after Claude interaction.

        Args:
            context: Hook context containing interaction results

        Returns:
            HookResult with success status and any modifications
        """
        try:
            # This would integrate with a memory service to save new memories
            # For now, this is a placeholder for future memory integration
            self.logger.debug("Saving new memories from interaction")

            from claude_mpm.hooks.base_hook import HookResult

            return HookResult(success=True, data=context.data, modified=False)

        except Exception as e:
            self.logger.warning(f"Failed to save memories: {e}")
            from claude_mpm.hooks.base_hook import HookResult

            return HookResult(
                success=False, data=context.data, modified=False, error=str(e)
            )

    def _save_new_memories(self, context):
        """Legacy hook function for backward compatibility."""
        result = self._save_new_memories_hook(context)
        return result.data

    def _preserve_memory_state(self, context):
        """Hook function to preserve memory state on interaction error.

        Args:
            context: Hook context containing error details
        """
        try:
            # This would ensure memory state is preserved even if interaction fails
            self.logger.debug("Preserving memory state after error")

        except Exception as e:
            self.logger.warning(f"Failed to preserve memory state: {e}")

    def unregister_memory_hooks(self):
        """Unregister memory-related hooks from the hook service."""
        if not self.hook_service:
            return

        try:
            self.hook_service.unregister_hook(
                "before_claude_interaction", self._load_relevant_memories
            )
            self.hook_service.unregister_hook(
                "after_claude_interaction", self._save_new_memories
            )
            self.hook_service.unregister_hook(
                "on_interaction_error", self._preserve_memory_state
            )

            self.logger.debug("Memory hooks unregistered successfully")

        except Exception as e:
            self.logger.warning(f"Failed to unregister memory hooks: {e}")

    def is_memory_enabled(self) -> bool:
        """Check if memory functionality is enabled.

        Returns:
            bool: True if memory is enabled and available
        """
        # This would check if memory service is available and configured
        # For now, return False as memory is not yet implemented
        return False

    def get_memory_status(self) -> dict:
        """Get current memory system status.

        Returns:
            dict: Memory system status information
        """
        return {
            "enabled": self.is_memory_enabled(),
            "hooks_registered": self.hook_service is not None,
            "service_available": True,
        }

    def get_hook_status(self) -> Dict[str, Any]:
        """Get status of registered memory hooks.

        Returns:
            Dictionary with hook status information
        """
        return {
            "registered_hooks": self.registered_hooks,
            "hook_service_available": self.hook_service is not None,
            "memory_enabled": self.is_memory_enabled(),
            "total_hooks": len(self.registered_hooks),
            "status": "active" if self.registered_hooks else "inactive",
        }


