"""soagila_annihilate — Cryptographic data destruction (SDIJAP)."""

import json
from mcp.types import Tool, TextContent


def get_definition() -> Tool:
    return Tool(
        name="soagila_annihilate",
        description=(
            "IRREVERSIBLE: Cryptographic annihilation of project memory (SDIJAP protocol). "
            "Phase 1: Encryption keys destroyed. Phase 2: Data overwritten with "
            "random bytes (3 passes). Phase 3: Embedding vectors poisoned with noise. "
            "A final Annihilation Receipt is created as proof of destruction. "
            "THIS CANNOT BE UNDONE. Requires confirm=true. Pro/Enterprise only."
        ),
        inputSchema={
            "type": "object",
            "properties": {
                "scope": {
                    "type": "string",
                    "enum": ["project", "all"],
                    "description": "'project' = current project only. 'all' = all SOAGILA data.",
                },
                "confirm": {
                    "type": "boolean",
                    "description": "Must be true to proceed. This is irreversible.",
                },
            },
            "required": ["scope", "confirm"],
        },
    )


async def handle(arguments: dict) -> list[TextContent]:
    scope = arguments.get("scope", "project")
    confirm = arguments.get("confirm", False)

    if not confirm:
        return [TextContent(type="text", text=json.dumps({
            "error": "Annihilation requires confirm=true. This action is IRREVERSIBLE."
        }))]

    from soagila.config import get_settings
    if get_settings().tier == "free":
        return [TextContent(type="text", text=json.dumps({
            "error": "SDIJAP annihilation requires Pro or Enterprise tier.",
            "upgrade_url": "https://soagila.com/pricing",
        }))]

    # Execute SDIJAP annihilation
    from soagila.core.sdijap.annihilator import annihilate
    annihilation = await annihilate(scope=scope)

    result = {
        "status": annihilation.status,
        "scope": annihilation.scope,
        "phases": {
            "1_key_destruction": {"keys_destroyed": annihilation.keys_destroyed},
            "2_data_overwrite": {
                "episodic_destroyed": annihilation.episodic_destroyed,
                "semantic_destroyed": annihilation.semantic_destroyed,
                "relationships_destroyed": annihilation.relationships_destroyed,
                "overwrite_passes": annihilation.overwrite_passes,
            },
            "3_embedding_poisoning": {"embeddings_poisoned": annihilation.embeddings_poisoned},
        },
        "receipts_preserved": annihilation.receipts_preserved,
        "receipt_id": annihilation.receipt_id,
        "duration_ms": annihilation.duration_ms,
        "message": "SDIJAP cryptographic annihilation complete. All data is irrecoverable.",
    }
    return [TextContent(type="text", text=json.dumps(result, indent=2))]
