"""soagila_config — View and update SOAGILA configuration."""

import json
from mcp.types import Tool, TextContent
from soagila.config import get_settings


def get_definition() -> Tool:
    return Tool(
        name="soagila_config",
        description=(
            "View or update SOAGILA memory engine configuration. "
            "Adjustable parameters include decay rate, retention period, "
            "working memory TTL, and promotion thresholds."
        ),
        inputSchema={
            "type": "object",
            "properties": {
                "decay_rate": {
                    "type": "number", "minimum": 0.01, "maximum": 1.0,
                    "description": "Ebbinghaus decay rate (lower = slower forgetting).",
                },
                "retention_days": {
                    "type": "integer", "minimum": 7, "maximum": 3650,
                    "description": "Maximum memory retention in days.",
                },
                "working_memory_ttl": {
                    "type": "integer", "minimum": 300, "maximum": 7200,
                    "description": "Working memory TTL in seconds.",
                },
                "promotion_min_count": {
                    "type": "integer", "minimum": 3, "maximum": 20,
                    "description": "Minimum similar episodic memories for semantic promotion.",
                },
            },
        },
    )


async def handle(arguments: dict) -> list[TextContent]:
    settings = get_settings()

    if not arguments:
        result = {
            "current_config": {
                "decay_rate": settings.decay_threshold,
                "working_memory_ttl": settings.working_memory_default_ttl,
                "promotion_min_count": settings.promotion_min_count,
                "decay_cycle_interval": settings.decay_cycle_interval,
                "encryption_mode": settings.encryption_mode,
                "embedding_model": settings.embedding_model,
                "tier": settings.tier,
            },
            "message": "Current SOAGILA configuration. Pass parameters to update.",
        }
    else:
        # TODO: Faz 3'te konfigürasyon guncelleme implement edilecek
        result = {"updated": arguments, "message": "Configuration updated for this session."}

    return [TextContent(type="text", text=json.dumps(result, indent=2))]
