from typing import Dict
from typing import Any


def _make_pod_envconfig(
    config: Dict[str, Any], relation_state: Dict[str, Any]
) -> Dict[str, Any]:
    """Generate pod environment configuration.

    Args:
        config (Dict[str, Any]): configuration information.
        relation_state (Dict[str, Any]): relation state information.

    Returns:
        Dict[str, Any]: pod environment configuration.
    """
    envconfig = {
        # General configuration
        "ALLOW_ANONYMOUS_LOGIN": "yes",
        "OSMNBI_SERVER_ENABLE_TEST": config["enable_test"],
        "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public",
        # Kafka configuration
        "OSMNBI_MESSAGE_HOST": relation_state["message_host"],
        "OSMNBI_MESSAGE_DRIVER": "kafka",
        "OSMNBI_MESSAGE_PORT": relation_state["message_port"],
        # Database configuration
        "OSMNBI_DATABASE_DRIVER": "mongo",
        "OSMNBI_DATABASE_URI": relation_state["database_uri"],
        "OSMNBI_DATABASE_COMMONKEY": config["database_commonkey"],
        # Storage configuration
        "OSMNBI_STORAGE_DRIVER": "mongo",
        "OSMNBI_STORAGE_PATH": "/app/storage",
        "OSMNBI_STORAGE_COLLECTION": "files",
        "OSMNBI_STORAGE_URI": relation_state["database_uri"],
        # Prometheus configuration
        "OSMNBI_PROMETHEUS_HOST": relation_state["prometheus_host"],
        "OSMNBI_PROMETHEUS_PORT": relation_state["prometheus_port"],
        # Log configuration
        "OSMNBI_LOG_LEVEL": config["log_level"],
    }

    if config["auth_backend"] == "internal":
        envconfig["OSMNBI_AUTHENTICATION_BACKEND"] = "internal"
    elif config["auth_backend"] == "keystone":
        envconfig.update(
            {
                "OSMNBI_AUTHENTICATION_BACKEND": "keystone",
                "OSMNBI_AUTHENTICATION_AUTH_URL": relation_state["keystone_host"],
                "OSMNBI_AUTHENTICATION_AUTH_PORT": relation_state["keystone_port"],
                "OSMNBI_AUTHENTICATION_USER_DOMAIN_NAME": relation_state[
                    "keystone_user_domain_name"
                ],
                "OSMNBI_AUTHENTICATION_PROJECT_DOMAIN_NAME": relation_state[
                    "keystone_project_domain_name"
                ],
                "OSMNBI_AUTHENTICATION_SERVICE_USERNAME": relation_state[
                    "keystone_username"
                ],
                "OSMNBI_AUTHENTICATION_SERVICE_PASSWORD": relation_state[
                    "keystone_password"
                ],
                "OSMNBI_AUTHENTICATION_SERVICE_PROJECT": relation_state[
                    "keystone_service"
                ],
            }
        )
    else:
        raise ValueError("auth_backend needs to be either internal or keystone")

    return envconfig