"""JSON class for data wrangling JSON objects."""

from importlib import import_module
from typing import Any, Optional

from pydantic import BaseModel


class BaseModel(BaseModel):
    """Base class for data wrangling tasks with JSON."""

    @classmethod
    def from_dict(cls, data: Optional[dict[str, Any]] = None):
        """Instantiates the class from a dict."""

        if data is None:
            return None

        return cls(**data)

    def to_dict(self) -> dict[str, Any]:
        """Converts the class to a dict."""

        return self.model_dump(mode="json", exclude_none=True, by_alias=True)


def from_dict(data: dict[str, Any]) -> Any:
    """
    Load a data model instance from a dict with associated class info.

    Parameters
    ----------
    data : dict[str, Any]
        The data to load.

    Returns
    -------
    Any
        The loaded data model instance.
    """

    module = import_module(data["class"]["module"])
    cls = getattr(module, data["class"]["name"])

    return cls.from_dict(data["attributes"])
