"""Zombie Agent Configuration — reads all config from environment variables."""

import json
import os

from a2a.types import AgentCard


class ZombieConfig:
    """Centralized configuration for the zombie agent."""

    # Agent Identity
    ZOMBIE_MODE: str = os.getenv("ZOMBIE_MODE", "idle")
    AGENT_NAME: str = os.getenv("AGENT_NAME", "zombie")
    AGENT_PORT: int = int(os.getenv("AGENT_PORT", "11440"))
    AGENT_DISPLAY_NAME: str = AGENT_NAME
    AGENT_DESCRIPTION: str = f"Ephemeral agent: {AGENT_NAME}"

    # Task
    SYSTEM_PROMPT: str = os.getenv("SYSTEM_PROMPT", "You are a generic ABI agent.")
    WORKSPACE: str = "/app/workspace"

    # Tools (JSON list of MCP tool names)
    _TOOLS_JSON: str = os.getenv("TOOLS", "[]")
    try:
        TOOL_NAMES: list = json.loads(_TOOLS_JSON)
    except json.JSONDecodeError:
        TOOL_NAMES: list = []

    # Artifacts (JSON list of MinIO keys to download)
    _ARTIFACT_KEYS_JSON: str = os.getenv("ARTIFACT_KEYS", "[]")
    try:
        ARTIFACT_KEYS: list = json.loads(_ARTIFACT_KEYS_JSON)
    except json.JSONDecodeError:
        ARTIFACT_KEYS: list = []

    # LLM
    MODEL_NAME: str = os.getenv("MODEL_NAME", "qwen2.5:3b")
    OLLAMA_HOST: str = os.getenv("OLLAMA_HOST", "http://localhost:11434")
    LLM_PROVIDER: str = os.getenv("LLM_PROVIDER", "ollama")
    LLM_CONFIG: dict = {
        "provider": LLM_PROVIDER,
        "model": MODEL_NAME,
        "temperature": float(os.getenv("LLM_TEMPERATURE", "0.1")),
        "base_url": os.getenv("LLM_BASE_URL", OLLAMA_HOST),
        "api_key": os.getenv("LLM_API_KEY", ""),
    }

    # Logging
    LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
    LOG_TO_ARTIFACT_STORE: bool = os.getenv("LOG_TO_ARTIFACT_STORE", "true").lower() == "true"
    LOG_AGENT_NAME: str = os.getenv("LOG_AGENT_NAME", AGENT_NAME)
    LOG_BUCKET: str = os.getenv("LOG_BUCKET", "abi-logs")

    # Agent Card (built at import time)
    AGENT_CARD: str = "ephemeral"

    @classmethod
    def build_agent_card(cls) -> AgentCard:
        """Build A2A AgentCard from config."""
        return AgentCard(**{
            "name": cls.AGENT_NAME,
            "description": cls.AGENT_DESCRIPTION,
            "url": f"http://{cls.AGENT_NAME}:{cls.AGENT_PORT}",
            "version": "1.0.0",
            "capabilities": {
                "streaming": "True",
                "pushNotifications": "False",
                "stateTransitionHistory": "False",
            },
            "defaultInputModes": ["text/plain"],
            "defaultOutputModes": ["text/plain"],
            "skills": [{
                "id": "execute_task",
                "name": f"Execute ({cls.AGENT_NAME})",
                "description": cls.SYSTEM_PROMPT[:200],
                "tags": cls.TOOL_NAMES,
            }],
        })


config = ZombieConfig()
