# coding: utf-8

"""
    (v2.0) Dynamics 365 Business Central

    (v2.0) Business Central Standard APIs

    The version of the OpenAPI document: 2.0.0
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


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

from datetime import datetime
from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictFloat, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional, Union
from typing_extensions import Annotated
from pybusinesscentral.model.default_dimensions import DefaultDimensions
from pybusinesscentral.model.item_category import ItemCategory
from pybusinesscentral.model.picture import Picture
from pybusinesscentral.model.unitofmeasuretype import Unitofmeasuretype
from typing import Optional, Set
from typing_extensions import Self

class Item(BaseModel):
    """
    Item
    """ # noqa: E501
    id: Optional[StrictStr] = Field(default=None, description="(v1.0) The id property for the Dynamics 365 Business Central item entity")
    number: Optional[Annotated[str, Field(strict=True, max_length=20)]] = Field(default=None, description="(v1.0) The number property for the Dynamics 365 Business Central item entity")
    display_name: Optional[Annotated[str, Field(strict=True, max_length=100)]] = Field(default=None, description="(v1.0) The displayName property for the Dynamics 365 Business Central item entity", alias="displayName")
    type: Optional[StrictStr] = Field(default=None, description="(v1.0) The type property for the Dynamics 365 Business Central item entity")
    item_category_id: Optional[StrictStr] = Field(default=None, description="(v1.0) The itemCategoryId property for the Dynamics 365 Business Central item entity", alias="itemCategoryId")
    item_category_code: Optional[Annotated[str, Field(strict=True, max_length=20)]] = Field(default=None, description="(v1.0) The itemCategoryCode property for the Dynamics 365 Business Central item entity", alias="itemCategoryCode")
    blocked: Optional[StrictBool] = Field(default=None, description="(v1.0) The blocked property for the Dynamics 365 Business Central item entity")
    base_unit_of_measure_id: Optional[StrictStr] = Field(default=None, description="(v1.0) The baseUnitOfMeasureId property for the Dynamics 365 Business Central item entity", alias="baseUnitOfMeasureId")
    base_unit_of_measure: Optional[Unitofmeasuretype] = Field(default=None, alias="baseUnitOfMeasure")
    gtin: Optional[Annotated[str, Field(strict=True, max_length=14)]] = Field(default=None, description="(v1.0) The gtin property for the Dynamics 365 Business Central item entity")
    inventory: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="(v1.0) The inventory property for the Dynamics 365 Business Central item entity")
    unit_price: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="(v1.0) The unitPrice property for the Dynamics 365 Business Central item entity", alias="unitPrice")
    price_includes_tax: Optional[StrictBool] = Field(default=None, description="(v1.0) The priceIncludesTax property for the Dynamics 365 Business Central item entity", alias="priceIncludesTax")
    unit_cost: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="(v1.0) The unitCost property for the Dynamics 365 Business Central item entity", alias="unitCost")
    tax_group_id: Optional[StrictStr] = Field(default=None, description="(v1.0) The taxGroupId property for the Dynamics 365 Business Central item entity", alias="taxGroupId")
    tax_group_code: Optional[Annotated[str, Field(strict=True, max_length=20)]] = Field(default=None, description="(v1.0) The taxGroupCode property for the Dynamics 365 Business Central item entity", alias="taxGroupCode")
    last_modified_date_time: Optional[datetime] = Field(default=None, description="(v1.0) The lastModifiedDateTime property for the Dynamics 365 Business Central item entity", alias="lastModifiedDateTime")
    picture: Optional[List[Picture]] = None
    default_dimensions: Optional[List[DefaultDimensions]] = Field(default=None, alias="defaultDimensions")
    item_category: Optional[ItemCategory] = Field(default=None, alias="itemCategory")
    __properties: ClassVar[List[str]] = ["id", "number", "displayName", "type", "itemCategoryId", "itemCategoryCode", "blocked", "baseUnitOfMeasureId", "baseUnitOfMeasure", "gtin", "inventory", "unitPrice", "priceIncludesTax", "unitCost", "taxGroupId", "taxGroupCode", "lastModifiedDateTime", "picture", "defaultDimensions", "itemCategory"]

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
    )


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

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
        return json.dumps(self.to_dict())

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

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        excluded_fields: Set[str] = set([
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of base_unit_of_measure
        if self.base_unit_of_measure:
            _dict['baseUnitOfMeasure'] = self.base_unit_of_measure.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each item in picture (list)
        _items = []
        if self.picture:
            for _item_picture in self.picture:
                if _item_picture:
                    _items.append(_item_picture.to_dict())
            _dict['picture'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in default_dimensions (list)
        _items = []
        if self.default_dimensions:
            for _item_default_dimensions in self.default_dimensions:
                if _item_default_dimensions:
                    _items.append(_item_default_dimensions.to_dict())
            _dict['defaultDimensions'] = _items
        # override the default output from pydantic by calling `to_dict()` of item_category
        if self.item_category:
            _dict['itemCategory'] = self.item_category.to_dict()
        # set to None if number (nullable) is None
        # and model_fields_set contains the field
        if self.number is None and "number" in self.model_fields_set:
            _dict['number'] = None

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of Item from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "id": obj.get("id"),
            "number": obj.get("number"),
            "displayName": obj.get("displayName"),
            "type": obj.get("type"),
            "itemCategoryId": obj.get("itemCategoryId"),
            "itemCategoryCode": obj.get("itemCategoryCode"),
            "blocked": obj.get("blocked"),
            "baseUnitOfMeasureId": obj.get("baseUnitOfMeasureId"),
            "baseUnitOfMeasure": Unitofmeasuretype.from_dict(obj["baseUnitOfMeasure"]) if obj.get("baseUnitOfMeasure") is not None else None,
            "gtin": obj.get("gtin"),
            "inventory": obj.get("inventory"),
            "unitPrice": obj.get("unitPrice"),
            "priceIncludesTax": obj.get("priceIncludesTax"),
            "unitCost": obj.get("unitCost"),
            "taxGroupId": obj.get("taxGroupId"),
            "taxGroupCode": obj.get("taxGroupCode"),
            "lastModifiedDateTime": obj.get("lastModifiedDateTime"),
            "picture": [Picture.from_dict(_item) for _item in obj["picture"]] if obj.get("picture") is not None else None,
            "defaultDimensions": [DefaultDimensions.from_dict(_item) for _item in obj["defaultDimensions"]] if obj.get("defaultDimensions") is not None else None,
            "itemCategory": ItemCategory.from_dict(obj["itemCategory"]) if obj.get("itemCategory") is not None else None
        })
        return _obj


