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

from attrs import define as _attrs_define
from attrs import field as _attrs_field
from attrs import fields as _attrs_fields
{% if model.is_multipart_body %}
import json
{% endif %}

{# CHANGE START (4) - add base class for OSIDB models #}
from ..types import UNSET, Unset, OSIDBModel
{# CHANGE END (4) #}

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

{% for lazy_import in model.lazy_imports %}
{% if loop.first %}
if TYPE_CHECKING:
{% endif %}
  {{ lazy_import }}
{% endfor %}


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

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

{% from "helpers.jinja" import safe_docstring %}

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

{% macro class_docstring_content(model) %}
    {% if model.title %}{{ model.title | wordwrap(116) }}

    {% endif -%}
    {%- if model.description %}{{ model.description | wordwrap(116) }}

    {% endif %}
    {% if not model.title and not model.description %}
    {# Leave extra space so that a section doesn't start on the first line #}

    {% endif %}
    {% if model.example %}
    Example:
        {{ model.example | string | wordwrap(112) | indent(12) }}

    {% endif %}
    {% if model.required_properties or model.optional_properties %}
    Attributes:
    {% for property in model.required_properties + model.optional_properties %}
        {{ property.to_docstring() | wordwrap(112) | indent(12) }}
    {% endfor %}{% endif %}
{% endmacro %}

@_attrs_define
{# CHANGE START (5) - add base class for OSIDB models #}
class {{ class_name }}(OSIDBModel):
{# CHANGE END (5) #}
    {{ safe_docstring(class_docstring_content(model)) | indent(4) }}

    {% 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 }}] = _attrs_field(init=False, factory=dict)
    {% endif %}

{% macro _to_dict(multipart=False) %}
{% for property in model.required_properties + model.optional_properties %}
{% import "property_templates/" + property.template as prop_template %}
{% if multipart %}
{{ prop_template.transform_multipart(property, "self." + property.python_name, property.python_name) }}
{% elif prop_template.transform %}
{{ prop_template.transform(property=property, source="self." + property.python_name, destination=property.python_name) }}
{% else %}
{{ property.python_name }} = self.{{ property.python_name }}
{% endif %}

{% endfor %}

field_dict: dict[str, Any] = {}
{% if model.additional_properties %}
{% import "property_templates/" + model.additional_properties.template as prop_template %}
{% if multipart %}
for prop_name, prop in self.additional_properties.items():
    {{ prop_template.transform_multipart(model.additional_properties, "prop", "field_dict[prop_name]") | indent(4) }}
{% elif prop_template.transform %}
for prop_name, prop in self.additional_properties.items():
    {{ prop_template.transform(model.additional_properties, "prop", "field_dict[prop_name]", declare_type=false) | indent(4) }}
{% else %}
field_dict.update(self.additional_properties)
{% endif %}
{% endif %}
{# CHANGE START (1) - make all the fields treated as non-required #}
{# {% if model.required_properties | length > 0 or model.optional_properties | length > 0 %}
field_dict.update({
    {% for property in model.required_properties + model.optional_properties %}
    {% if property.required %}
    "{{ property.name }}": {{ property.python_name }},
    {% endif %}
    {% endfor %}
})
{% endif %} #}
{# {% if not property.required %} #}
{% for property in model.required_properties + model.optional_properties %}
{# CHANGE START (7) - Check for Unset instance #}
if not isinstance({{ property.python_name }}, Unset):
{# CHANGE END (7) #}
    field_dict["{{ property.name }}"] = {{ property.python_name }}
{# {% endif %} #}
{# CHANGE END (1) #}
{% endfor %}

return field_dict
{% endmacro %}

    def to_dict(self) -> dict[str, Any]:
    {% for lazy_import in model.lazy_imports %}
        {{ lazy_import }}
    {% endfor %}
        {{ _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:
    {% for lazy_import in model.lazy_imports %}
        {{ lazy_import }}
    {% endfor %}
{% if (model.required_properties or model.optional_properties or model.additional_properties) %}
        d = src_dict.copy()
{% for property in model.required_properties + model.optional_properties %}
{# CHANGE START (2) - make all the properties treated as non-required #}
    {# {% if property.required %}
        {% set property_source = 'd.pop("' + property.name + '")' %}
    {% else %} #}
        {% set property_source = 'd.pop("' + property.name + '", UNSET)' %}
    {# {% endif %} #}
{# CHANGE END (2) #}
    {% import "property_templates/" + property.template as prop_template %}
    {% if prop_template.construct %}
        {{ prop_template.construct(property, property_source) | indent(8) }}
    {% else %}
        {{ property.python_name }} = {{ property_source }}
    {% endif %}

{% endfor %}
{% endif %}
        {{ 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 %}{# Can be a bool instead of an object #}
        {% import "property_templates/" + model.additional_properties.template as prop_template %}

{% if model.additional_properties.lazy_imports %}
    {% for lazy_import in model.additional_properties.lazy_imports %}
        {{ lazy_import }}
    {% endfor %}
{% endif %}
    {% else %}
        {% set prop_template = None %}
    {% endif %}
    {% if prop_template and prop_template.construct %}
        additional_properties = {}
        for prop_name, prop_dict in d.items():
            {{ prop_template.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 }}

    {# CHANGE START (3) - add new class method for fields obtain #}
    @classmethod
    def get_fields(cls):
        return {f.name : f.type for f in _attrs_fields(cls)}
    {# CHANGE END (3) #}

    {# CHANGE START (4) - add new classmethod for initializing empty model with Unset values  #}
    @classmethod
    def new(cls):
        return cls.from_dict({})

    {# CHANGE END (4) #}

    {# CHANGE START (6) - add classmethod for creating model from another model instance #}
    @classmethod
    def from_model(cls: type[T], model: "OSIDBModel") -> T:
        return cls.from_dict(model.to_dict())

    {# CHANGE END (6) #}

    {% 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 %}
