{#
    This is a custom template derived from:
    https://github.com/openapi-generators/openapi-python-client/blob/v.0.10.7/openapi_python_client/templates/model.py.jinja
#}
from typing import Any, Dict, Type, TypeVar, Tuple, Optional, BinaryIO, TextIO, Union

{% if model.additional_properties %}
from typing import List

{% endif %}

import attr
{% if model.is_multipart_body %}
import json
{% endif %}

from ..types import UNSET, Unset, OSIDBModel

{% for relative in model.relative_imports %}
{{ relative }}
{% endfor %}


{% if model.additional_properties %}
{% set additional_property_type = 'Any' if model.additional_properties == True else model.additional_properties.get_type_string() %}
{% endif %}

{% set class_name = model.class_info.name %}
{% set module_name = model.class_info.module_name %}

T = TypeVar("T", bound="{{ class_name }}")

@attr.s(auto_attribs=True)
class {{ class_name }}(OSIDBModel):
    """ {{ model.description }} """
    {% for property in model.required_properties + model.optional_properties %}
    {% if property.default is none and property.required %}
    {{ property.to_string() }}
    {% endif %}
    {% endfor %}
    {% for property in model.required_properties + model.optional_properties %}
    {% if property.default is not none or not property.required %}
    {{ property.to_string() }}
    {% endif %}
    {% endfor %}
    {% if model.additional_properties %}
    additional_properties: Dict[str, {{ additional_property_type }}] = attr.ib(init=False, factory=dict)
    {% endif %}

{% macro _to_dict(multipart=False) %}
{% for property in model.required_properties + model.optional_properties %}
{% if property.template %}
{% from "property_templates/" + property.template import transform %}
{{ transform(property, "self." + property.python_name, property.python_name, stringify=multipart) }}
{% elif multipart %}
{{ property.python_name }} = self.{{ property.python_name }} if self.{{ property.python_name }} is UNSET else (None, str(self.{{ property.python_name }}), "text/plain")
{% else %}
{{ property.python_name }} = self.{{ property.python_name }}
{% endif %}
{% endfor %}

field_dict: Dict[str, Any] = {}
{% if model.additional_properties %}
{% if model.additional_properties.template %}
{% from "property_templates/" + model.additional_properties.template import transform %}
for prop_name, prop in self.additional_properties.items():
    {{ transform(model.additional_properties, "prop", "field_dict[prop_name]", stringify=multipart) | indent(4) }}
{% elif multipart %}
field_dict.update({
    key: (None, str(value), "text/plain")
    for key, value in self.additional_properties.items()
})
{% else %}
field_dict.update(self.additional_properties)
{% endif %}
{% endif %}
{% for property in  model.required_properties + model.optional_properties %}
if not isinstance({{ property.python_name }}, Unset):
    field_dict["{{ property.name }}"] = {{ property.python_name }}
{% endfor %}

return field_dict
{% endmacro %}

    def to_dict(self) -> Dict[str, Any]:
        {{ _to_dict() | indent(8) }}

{% if model.is_multipart_body %}
    def to_multipart(self) -> Dict[str, Any]:
        {{ _to_dict(multipart=True) | indent(8) }}
{% endif %}

    @classmethod
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        d = src_dict.copy()
{% for property in model.required_properties + model.optional_properties %}
    {% set property_source = 'd.pop("' + property.name + '", UNSET)' %}
    {% if property.template %}
        {% from "property_templates/" + property.template import construct %}
        {{ construct(property, property_source) | indent(8) }}
    {% else %}
        {{ property.python_name }} = {{ property_source }}
    {% endif %}

{% endfor %}
        {{ module_name }} = cls(
{% for property in model.required_properties + model.optional_properties %}
            {{ property.python_name }}={{ property.python_name }},
{% endfor %}
        )

{% if model.additional_properties %}
    {% if model.additional_properties.template %}
        {% from "property_templates/" + model.additional_properties.template import construct %}
        additional_properties = {}
        for prop_name, prop_dict in d.items():
            {{ construct(model.additional_properties, "prop_dict") | indent(12) }}
            additional_properties[prop_name] = {{ model.additional_properties.python_name }}

        {{ module_name }}.additional_properties = additional_properties
    {% else %}
        {{ module_name }}.additional_properties = d
    {% endif %}
{% endif %}
        return {{ module_name }}

    @staticmethod
    def get_fields():
        return {
            {% for property in model.required_properties + model.optional_properties %}
            "{{ property.name }}": {{ property.get_type_string(no_optional=True) }},
            {% endfor %}
        }

    {% if model.additional_properties %}
    @property
    def additional_keys(self) -> List[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> {{ additional_property_type }}:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: {{ additional_property_type }}) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties
    {% endif %}
