# coding: utf-8

"""
    LUSID API

    FINBOURNE Technology  # noqa: E501

    Contact: info@finbourne.com
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""


from __future__ import annotations
import pprint
import re  # noqa: F401
import json


from typing import List, Dict, Optional, Any, Union, TYPE_CHECKING
from typing_extensions import Annotated
from pydantic.v1 import BaseModel, StrictStr, StrictInt, StrictBool, StrictFloat, StrictBytes, Field, validator, ValidationError, conlist, constr
from datetime import datetime
from lusid.models.allocation_group_class_definition import AllocationGroupClassDefinition
from lusid.models.apportionment_method_property import ApportionmentMethodProperty

class AllocationGroupDefinition(BaseModel):
    """
    AllocationGroupDefinition
    """
    classes: Optional[List[AllocationGroupClassDefinition]] = Field(default=None, description="An optional list of share classes that belong to this group. Each entry must reference a ShareClass already present on the Fund. You can provide this or the Formula, but not both.")
    code:  StrictStr = Field(...,alias="code", description="The unique code for the Allocation Group. Must be unique within the Fund.") 
    name:  StrictStr = Field(...,alias="name", description="The display name of the Allocation Group.") 
    description:  Optional[StrictStr] = Field(None,alias="description", description="An optional description for the Allocation Group.") 
    share_class_short_code:  StrictStr = Field(...,alias="shareClassShortCode", description="The short code that identifies the Allocation Group.") 
    apportionment_method_property: Optional[ApportionmentMethodProperty] = Field(default=None, alias="apportionmentMethodProperty")
    formula:  Optional[StrictStr] = Field(None,alias="formula", description="An optional filter expression used to define which classes belong to this group, based on fund grouping criteria. You can provide this or the Classes, but not both.") 
    __properties = ["classes", "code", "name", "description", "shareClassShortCode", "apportionmentMethodProperty", "formula"]

    class Config:
        """Pydantic configuration"""
        allow_population_by_field_name = True
        validate_assignment = True

    def __str__(self):
        """For `print` and `pprint`"""
        return pprint.pformat(self.dict(by_alias=False))

    def __repr__(self):
        """For `print` and `pprint`"""
        return self.to_str()

    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.dict(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> AllocationGroupDefinition:
        """Create an instance of AllocationGroupDefinition from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self):
        """Returns the dictionary representation of the model using alias"""
        _dict = self.dict(by_alias=True,
                          exclude={
                          },
                          exclude_none=True)
        # override the default output from pydantic by calling `to_dict()` of each item in classes (list)
        _items = []
        if self.classes:
            for _item in self.classes:
                if _item:
                    _items.append(_item.to_dict())
            _dict['classes'] = _items
        # override the default output from pydantic by calling `to_dict()` of apportionment_method_property
        if self.apportionment_method_property:
            _dict['apportionmentMethodProperty'] = self.apportionment_method_property.to_dict()
        # set to None if classes (nullable) is None
        # and __fields_set__ contains the field
        if self.classes is None and "classes" in self.__fields_set__:
            _dict['classes'] = None

        # set to None if description (nullable) is None
        # and __fields_set__ contains the field
        if self.description is None and "description" in self.__fields_set__:
            _dict['description'] = None

        # set to None if formula (nullable) is None
        # and __fields_set__ contains the field
        if self.formula is None and "formula" in self.__fields_set__:
            _dict['formula'] = None

        return _dict

    @classmethod
    def from_dict(cls, obj: dict) -> AllocationGroupDefinition:
        """Create an instance of AllocationGroupDefinition from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return AllocationGroupDefinition.parse_obj(obj)

        _obj = AllocationGroupDefinition.parse_obj({
            "classes": [AllocationGroupClassDefinition.from_dict(_item) for _item in obj.get("classes")] if obj.get("classes") is not None else None,
            "code": obj.get("code"),
            "name": obj.get("name"),
            "description": obj.get("description"),
            "share_class_short_code": obj.get("shareClassShortCode"),
            "apportionment_method_property": ApportionmentMethodProperty.from_dict(obj.get("apportionmentMethodProperty")) if obj.get("apportionmentMethodProperty") is not None else None,
            "formula": obj.get("formula")
        })
        return _obj

AllocationGroupDefinition.update_forward_refs()
