# generated by datamodel-codegen:
#   filename:  additional_properties_value_constraints_annotated.json

from __future__ import annotations

import re
from typing import Annotated, Any, ClassVar

from pydantic import (
    BaseModel,
    ConfigDict,
    Field,
    RootModel,
    TypeAdapter,
    constr,
    model_validator,
)
from typing_extensions import TypeAliasType


class _JsonSchemaRuntimeValidationBase(BaseModel):
    __json_schema_pattern_properties__: ClassVar[tuple[Any, ...]] = ()

    @model_validator(mode='before')
    @classmethod
    def _validate_json_schema_runtime_rules(cls, data: Any) -> Any:
        data = cls._validate_json_schema_pattern_properties(data)
        return data

    @classmethod
    def _validate_json_schema_pattern_properties(cls, data: Any) -> Any:
        if not isinstance(data, dict):
            return data
        values = dict(data)
        for rule in cls.__json_schema_pattern_properties__:
            for key, value in data.items():
                if key in rule['declared_properties']:
                    continue
                if any(
                    re.search(pattern, key) for pattern in rule['rejected_patterns']
                ):
                    raise ValueError(
                        f'Property {key!r} is not allowed by patternProperties'
                    )
                matched = False
                for pattern, value_type in rule['pattern_properties']:
                    if not re.search(pattern, key):
                        continue
                    matched = True
                    value = TypeAdapter(value_type).validate_python(value)
                if matched:
                    values[key] = value
                    continue
                if rule['additional_property_type'] is not None:
                    values[key] = TypeAdapter(
                        rule['additional_property_type']
                    ).validate_python(value)
                    continue
                if not rule['allow_unmatched']:
                    raise ValueError(f'Unexpected property {key!r}')
        return values


NestedMapAdditionalProperty = TypeAliasType(
    "NestedMapAdditionalProperty", Annotated[int, Field(ge=1, le=9)]
)


TitledMapAdditionalProperty = TypeAliasType(
    "TitledMapAdditionalProperty", Annotated[int, Field(ge=1, le=9)]
)


class TitledMap(RootModel[dict[str, TitledMapAdditionalProperty]]):
    root: Annotated[dict[str, TitledMapAdditionalProperty], Field(title='TitledMap')]


TitledRefMapAdditionalProperty = TypeAliasType(
    "TitledRefMapAdditionalProperty", Annotated[int, Field(ge=1, le=9)]
)


LocalTitleRefMapAdditionalProperty = TypeAliasType(
    "LocalTitleRefMapAdditionalProperty",
    Annotated[int, Field(ge=1, le=9, title='LocalValue')],
)


NamedMapAdditionalProperty = TypeAliasType(
    "NamedMapAdditionalProperty", Annotated[int, Field(ge=1, le=9)]
)


NullableValueMapAdditionalProperty = TypeAliasType(
    "NullableValueMapAdditionalProperty", Annotated[int | None, Field(ge=1, le=9)]
)


ClosedArrayValueMapAdditionalProperty = TypeAliasType(
    "ClosedArrayValueMapAdditionalProperty", Annotated[list[Any], Field(max_length=0)]
)


class AllOfValueMap(RootModel[int]):
    root: Annotated[int, Field(ge=1, le=9)]


class TitledAllOfValueMap(RootModel[int]):
    root: Annotated[int, Field(ge=1, le=9, title='TitledPositiveInt')]


class NullableAllOfValueMap(RootModel[int | None]):
    root: Annotated[int | None, Field(ge=1, le=9)] = None


class UnionValueMap(RootModel[int]):
    root: Annotated[int, Field(ge=1, le=9)]


class UnionValueMap1(RootModel[str]):
    root: Annotated[str, Field(max_length=4, min_length=2)]


ObjectValueMapAdditionalProperty = TypeAliasType(
    "ObjectValueMapAdditionalProperty", Annotated[dict[str, str], Field(min_length=1)]
)


class ModeledValueMap(BaseModel):
    model_config = ConfigDict(
        extra='forbid',
    )
    value: Annotated[int, Field(ge=1)]


class NamedValue(RootModel[int]):
    root: Annotated[int, Field(ge=1, le=9, title='NamedValue')]


class ModeledIrrelevantConstraintMap(BaseModel):
    value: str


PayloadAdditionalProperty = TypeAliasType(
    "PayloadAdditionalProperty", Annotated[int, Field(ge=1, le=9)]
)


class PositiveInt(RootModel[int]):
    root: Annotated[int, Field(ge=1)]


class TitledPositiveInt(RootModel[int]):
    root: Annotated[int, Field(ge=1, title='TitledPositiveInt')]


class LocalValue(RootModel[str]):
    root: Annotated[str, Field(title='LocalValue')]


RootMapAdditionalProperty = TypeAliasType(
    "RootMapAdditionalProperty", Annotated[int, Field(ge=1, le=9)]
)


class RootMap(RootModel[dict[str, RootMapAdditionalProperty]]):
    root: Annotated[
        dict[str, RootMapAdditionalProperty], Field(max_length=2, min_length=1)
    ]


class Payload(_JsonSchemaRuntimeValidationBase):
    model_config = ConfigDict(
        extra='allow',
    )

    __json_schema_pattern_properties__: ClassVar[tuple[Any, ...]] = (
        {
            'declared_properties': (
                'allOfValueMap',
                'arrayIrrelevantConstraintMap',
                'booleanIrrelevantConstraintMap',
                'closedArrayValueMap',
                'existingLocalValue',
                'heterogeneousValueMap',
                'localTitleRefMap',
                'modeledIrrelevantConstraintMap',
                'modeledValueMap',
                'namedMap',
                'nestedMap',
                'nullableAllOfValueMap',
                'nullableValueMap',
                'objectValueMap',
                'rootMap',
                'titledAllOfValueMap',
                'titledMap',
                'titledRefMap',
                'titledValueMap',
                'unionValueMap',
            ),
            'rejected_patterns': (),
            'pattern_properties': (('^pattern_', int),),
            'additional_property_type': PayloadAdditionalProperty,
            'allow_unmatched': True,
        },
    )

    existingLocalValue: LocalValue
    nestedMap: dict[str, NestedMapAdditionalProperty]
    titledMap: Annotated[TitledMap, Field(title='TitledMap')]
    titledRefMap: dict[str, TitledRefMapAdditionalProperty]
    localTitleRefMap: dict[str, LocalTitleRefMapAdditionalProperty]
    namedMap: dict[constr(pattern=r'^[a-z]+$'), NamedMapAdditionalProperty]
    rootMap: RootMap
    nullableValueMap: dict[str, NullableValueMapAdditionalProperty]
    closedArrayValueMap: dict[str, ClosedArrayValueMapAdditionalProperty]
    allOfValueMap: dict[str, AllOfValueMap]
    titledAllOfValueMap: dict[str, TitledAllOfValueMap]
    nullableAllOfValueMap: dict[str, NullableAllOfValueMap]
    unionValueMap: dict[str, UnionValueMap | UnionValueMap1]
    objectValueMap: dict[str, ObjectValueMapAdditionalProperty]
    modeledValueMap: dict[str, ModeledValueMap]
    titledValueMap: dict[str, NamedValue]
    modeledIrrelevantConstraintMap: dict[str, ModeledIrrelevantConstraintMap]
    arrayIrrelevantConstraintMap: dict[str, list[int]]
    heterogeneousValueMap: dict[str, int | str]
    booleanIrrelevantConstraintMap: dict[str, bool]
