"""
333_APPS/L4_TOOLS/mcp/tools/canonical_trinity.py

MIRROR FILE — Documentation reference only.
Canonical source: codebase/mcp/tools/canonical_trinity.py (v55.2, 9-tool registry)

The 7 Legacy Tools of arifOS (v53 AAA Framework)
These were split into 9 explicit tools in v55. See codebase/mcp/core/tool_registry.py.
"""

import logging
from typing import Any, Dict, List, Optional

from aaa_mcp.bridge import (
    bridge_atlas_router,
    bridge_reality_check_router,
    bridge_trinity_loop_router,
)
from codebase.kernel import get_kernel_manager

logger = logging.getLogger(__name__)


# ==============================================================================
# 1. _init_ (The Gate)
# ==============================================================================
async def mcp_init(
    action: str = "init", query: str = "", session_id: Optional[str] = None, **kwargs
) -> Dict[str, Any]:
    """
    _init_: The 7-Step Thermodynamic Ignition Sequence.
    """
    from codebase.init import mcp_000_init

    return await mcp_000_init(action=action, query=query, session_id=session_id, **kwargs)


# ==============================================================================
# 2. _agi_ (The Mind)
# ==============================================================================
async def mcp_agi(
    action: str = "full", query: str = "", session_id: Optional[str] = None, **kwargs
) -> Dict[str, Any]:
    """
    _agi_: Mind Engine (Δ) - Logic, Sense, Think, Map.
    """
    try:
        kernel = get_kernel_manager().get_agi()
        return await kernel.execute(action, {"query": query, "session_id": session_id, **kwargs})
    except Exception as e:
        logger.error("mcp_agi execute failed: %s", e, exc_info=True)
        return {"verdict": "VOID", "session_id": session_id, "message": "Internal processing error"}


# ==============================================================================
# 3. _asi_ (The Heart)
# ==============================================================================
async def mcp_asi(
    action: str = "full",
    query: str = "",
    reasoning: str = "",
    session_id: Optional[str] = None,
    **kwargs,
) -> Dict[str, Any]:
    """
    _asi_: Heart Engine (Ω) - Empathy, Safety, Alignment.
    """
    try:
        kernel = get_kernel_manager().get_asi()
        context = kwargs.get("context", {})
        if reasoning:
            context["reasoning"] = reasoning
        return await kernel.execute(
            action,
            {"text": query, "query": query, "session_id": session_id, "context": context, **kwargs},
        )
    except Exception as e:
        logger.error("mcp_asi execute failed: %s", e, exc_info=True)
        return {"verdict": "VOID", "session_id": session_id, "message": "Internal processing error"}


# ==============================================================================
# 4. _apex_ (The Soul)
# ==============================================================================
async def mcp_apex(
    action: str = "decide",
    query: str = "",
    response: str = "",
    verdict: str = "",
    session_id: Optional[str] = None,
    **kwargs,
) -> Dict[str, Any]:
    """
    _apex_: Soul Engine (Ψ) - Judgment, Verdict, Proof.
    """
    try:
        kernel = get_kernel_manager().get_apex()
        kwargs["pre_verdict"] = verdict
        return await kernel.execute(
            action, {"query": query, "response": response, "session_id": session_id, **kwargs}
        )
    except Exception as e:
        logger.error("mcp_apex execute failed: %s", e, exc_info=True)
        return {"verdict": "VOID", "session_id": session_id, "message": "Internal processing error"}


# ==============================================================================
# 5. _vault_ (The Seal)
# ==============================================================================
async def mcp_vault(
    action: str = "seal",
    verdict: str = "SEAL",
    decision_data: Optional[Dict] = None,
    target: str = "seal",
    session_id: Optional[str] = None,
    **kwargs,
) -> Dict[str, Any]:
    """
    _vault_: Immutable Ledger - Seal, List, Read.
    """
    try:
        kernel = get_kernel_manager().get_apex()
        return await kernel.execute(
            "seal" if action == "seal" else action,
            {
                "session_id": session_id,
                "verdict": verdict,
                "data": decision_data,
                "target_ledger": target,
                **kwargs,
            },
        )
    except Exception as e:
        logger.error("mcp_vault execute failed: %s", e, exc_info=True)
        return {"verdict": "VOID", "session_id": session_id, "message": "Internal processing error"}


# ==============================================================================
# 6. _trinity_ (The Loop)
# ==============================================================================
async def mcp_trinity(
    query: str = "", session_id: Optional[str] = None, **kwargs
) -> Dict[str, Any]:
    """
    _trinity_: Full Metabolic Loop (AGI->ASI->APEX->VAULT).
    """
    return await bridge_trinity_loop_router(query=query, session_id=session_id, **kwargs)


# ==============================================================================
# 7. _reality_ (The Ground)
# ==============================================================================
async def mcp_reality(
    query: str = "", session_id: Optional[str] = None, **kwargs
) -> Dict[str, Any]:
    """
    _reality_: External Fact-Checking & Grounding.
    """
    return await bridge_reality_check_router(query=query, session_id=session_id, **kwargs)
