"""
Generate Pandera Schemas from all autogenerated Pydantic Models

These classes have Schema as a postfix, so Ribasim.models.PumpStatic
becomes Ribasim.schemas.PumpStaticSchema.
"""
import inspect
import sys

import pandera as pa
from pandera.engines.pandas_engine import PydanticModel

from ribasim import models


def gen_schema(name, cls):
    cname = f"{name}Schema"
    ctype = type(
        cname,
        (pa.DataFrameModel,),
        {
            "Config": type(
                f"{cname}.Config",
                (),
                {
                    "dtype": PydanticModel(cls),
                    "coerce": True,
                    "add_missing_columns": True,
                },
            )
        },
    )
    setattr(sys.modules[__name__], cname, ctype)


for name, cls in inspect.getmembers(models, inspect.isclass):
    gen_schema(name, cls)
