"""
Pydantic compatibility layer for v1 and v2.
Ensure it works with both pydantic v1 and v2.
"""

try:
    from pydantic import VERSION as PYDANTIC_VERSION
    PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")
except ImportError:
    PYDANTIC_V2 = False

if PYDANTIC_V2:
    from pydantic import BaseModel, Field, field_validator, model_validator
    from pydantic import ConfigDict
    
    def get_model_dump(model: BaseModel, **kwargs):
        """Get dict representation of model"""
        return model.model_dump(**kwargs)
    
    def get_model_dump_json(model: BaseModel, **kwargs):
        """Get JSON string representation of model"""
        return model.model_dump_json(**kwargs)
    
    def get_config_dict(**kwargs) -> ConfigDict:
        """Create Pydantic v2 config"""
        return ConfigDict(**kwargs)
    
    class CompatBaseModel(BaseModel):
        """Base model with v2 config"""
        model_config = ConfigDict(
            use_enum_values=True,
            json_encoders={}  # Not used in v2, but kept for compatibility
        )

else:
    from pydantic import BaseModel, Field, validator, root_validator
    from pydantic import Extra
    
    # Alias v2 names to v1
    field_validator = validator
    model_validator = root_validator
    
    def get_model_dump(model: BaseModel, **kwargs):
        """Get dict representation of model"""
        mode = kwargs.pop('mode', None)
        if mode == 'json':
            return model.dict(**kwargs)
        return model.dict(**kwargs)
    
    def get_model_dump_json(model: BaseModel, **kwargs):
        """Get JSON string representation of model"""
        return model.json(**kwargs)
    
    def get_config_dict(**kwargs):
        """Create Pydantic v1 config dict"""
        return kwargs
    
    class CompatBaseModel(BaseModel):
        """Base model with v1 config"""
        class Config:
            use_enum_values = True
            json_encoders = {}


__all__ = [
    'PYDANTIC_V2',
    'BaseModel',
    'Field',
    'field_validator',
    'model_validator',
    'get_model_dump',
    'get_model_dump_json',
    'get_config_dict',
    'CompatBaseModel',
]