"""Pathway Loader — dict-based Pathway and Node construction utilities.

Pathway.model_validate() handles all input normalization (node IDs from dict keys,
config flattening, field aliases, connection formats) via its model_validator.
"""

import logging
from typing import Any

from core.common import JsonObject
from core.pathways.nodes.base import NodeBase
from core.pathways.pathway import Node, Pathway

logger = logging.getLogger(__name__)


def node_from_dict(node_dict: JsonObject | dict[str, Any]) -> NodeBase:
    """Create a typed node from a dictionary.

    Uses Pydantic's discriminated union on the ``type`` field.
    """
    from pydantic import TypeAdapter

    adapter = TypeAdapter(Node)
    return adapter.validate_python(node_dict)


def pathway_from_dict(data: JsonObject | dict[str, Any]) -> Pathway:
    """Create a Pathway from a dictionary.

    Pathway._normalize_input model_validator handles all dict quirks:
    node ID injection, config flattening, field aliases, string connections.
    """
    return Pathway.model_validate(data)


__all__ = [
    "node_from_dict",
    "pathway_from_dict",
]
