"""
MISSING_SECRETS provider.

Lists secrets that still need to be configured, separated by
required vs optional.
"""

import logging
from typing import Optional, Dict

from elizaos.runtime import AgentRuntime
from elizaos.types import Provider, Message


logger = logging.getLogger(__name__)


class MissingSecretsProvider(Provider):
    """
    Provider that shows what secrets are still needed.

    Provides structured information about missing required and optional
    secrets to help the LLM guide the user through configuration.
    """

    name = "MISSING_SECRETS"
    description = "Lists secrets that still need to be configured"

    async def get(self, runtime: AgentRuntime, message: Message, state: Dict) -> Optional[str]:
        room = await runtime.get_room(message.room_id) if hasattr(message, "room_id") else None
        if not room:
            return None

        world_id = getattr(room, "world_id", None)
        if not world_id:
            return None

        world = await runtime.get_world(world_id)
        if not world:
            return None

        metadata = getattr(world, "metadata", None)
        if not isinstance(metadata, dict):
            return None

        settings = metadata.get("settings")
        if not settings or not isinstance(settings, dict):
            return None

        missing_required = []
        missing_optional = []

        for key, setting in settings.items():
            if not isinstance(setting, dict):
                continue
            if setting.get("value") is not None:
                continue

            info = {
                "key": key,
                "name": setting.get("name", key),
                "description": setting.get("usageDescription") or setting.get("description", ""),
            }

            if setting.get("required", False):
                missing_required.append(info)
            else:
                missing_optional.append(info)

        if not missing_required and not missing_optional:
            return "All secrets are configured."

        output_parts = []
        if missing_required:
            lines = [f"- {s['key']}: {s['description']}" for s in missing_required]
            output_parts.append(f"Missing required secrets:\n" + "\n".join(lines))

        if missing_optional:
            lines = [f"- {s['key']}: {s['description']}" for s in missing_optional]
            output_parts.append(f"Missing optional secrets:\n" + "\n".join(lines))

        return "\n\n".join(output_parts)
