from __future__ import annotations
{% if data_class.has_primitive_field_of_type((dm.Timestamp, dm.Date)) %}
import datetime{% endif %}
import warnings
{% if data_class.has_dependencies %}from typing import TYPE_CHECKING, Any, ClassVar, Literal,  no_type_check, Optional, Union{% else %}from typing import Any, ClassVar, Literal, no_type_check, Optional, Union{% endif %}{% if not data_class.use_optional_type %}{% endif %}

from cognite.client import data_modeling as dm{% if data_class.has_primitive_field_of_type(dm.TimeSeriesReference) %}
from cognite.client.data_classes import TimeSeries{% if is_pydantic_v2 %} as CogniteTimeSeries{% endif %}{% endif %}{% if data_class.use_pydantic_field %}
{{ data_class.import_pydantic_field }}{% endif %}
from pydantic import {% if is_pydantic_v2 %}field_validator, model_validator{% else %}validator, root_validator{% endif %}

from ._core import (
    DEFAULT_INSTANCE_SPACE,
    DataRecord,
    DataRecordGraphQL,
    DataRecordWrite,
    DomainModel,
    DomainModelWrite,
    DomainModelWriteList,
    DomainModelList,
    DomainRelationWrite,
    GraphQLCore,
    ResourcesWrite,{% if data_class.has_primitive_field_of_type(dm.TimeSeriesReference) and is_pydantic_v2 %}
    TimeSeries,{% endif %}
){% if data_class.implements %}{% for implements in data_class.implements %}
from .{{ implements.file_name }} import {{ implements.read_name }}{% if data_class.is_writable or data_class.is_interface %}, {{ implements.write_name }}{% endif %}{% endfor %}{% endif %}
{% if data_class.has_dependencies %}
if TYPE_CHECKING:{% for dependency_class in data_class.dependencies %}{% if dependency_class.file_name != data_class.file_name %}
    from .{{ dependency_class.file_name }} import {{ dependency_class.read_name }}, {{ dependency_class.graphql_name }}{% if dependency_class.is_writable or dependency_class.is_interface %}, {{ dependency_class.write_name }}{% endif %}{% endif %}{% endfor %}
{% endif %}

__all__ = [
    "{{ data_class.read_name }}",
    {% if data_class.is_writable or data_class.is_interface %}"{{ data_class.write_name }}",
    "{{ data_class.read_name }}Apply",{% endif %}
    "{{ data_class.read_list_name }}",
    {% if data_class.is_writable or data_class.is_interface %}"{{ data_class.write_list_name }}",
    "{{ data_class.read_name }}ApplyList",{% endif %}
    {% if data_class.has_field_of_type(ft.BasePrimitiveField) %}"{{ data_class.field_names }}",{% endif %}
    {% if data_class.has_primitive_field_of_type((dm.Text, dm.CDFExternalIdReference)) %}"{{ data_class.text_field_names }}",{% endif %}
    "{{ data_class.graphql_name }}",
]
{% if data_class.has_primitive_field_of_type((dm.Text, dm.CDFExternalIdReference)) %}

{{ data_class.text_field_names }} = Literal[{{ data_class.text_fields_literals }}]{% endif %}{% if data_class.has_field_of_type(ft.BasePrimitiveField) %}
{{ data_class.field_names }} = Literal[{{ data_class.fields_literals }}]

{{ data_class.properties_dict_name }} = {% raw %}{{% endraw %}{% for field in data_class.fields_of_type(ft.BasePrimitiveField) %}
    "{{ field.name }}": "{{ field.prop_name }}",{% endfor %}
{% raw %}}{% endraw %}{% endif %}

class {{ data_class.graphql_name }}(GraphQLCore{% if is_pydantic_v2 and data_class.has_any_field_model_prefix %}, protected_namespaces=(){% endif %}):
    """This represents the reading version of {{ data_class.doc_name }}, used
    when data is retrieved from CDF using GraphQL.

    It is used when retrieving data from CDF using GraphQL.

    Args:
        space: The space where the node is located.
        external_id: The external id of the {{ data_class.doc_name }}.
        data_record: The data record of the {{ data_class.doc_name }} node.{% for field in data_class %}
        {{ field.name }}: {{ field.argument_documentation }}{% endfor %}
    """
    view_id: ClassVar[dm.ViewId] = dm.ViewId("{{ data_class.view_id.space }}", "{{ data_class.view_id.external_id }}", "{{ data_class.view_id.version }}"){% for field in data_class %}
    {{ field.name }}: {{ field.as_graphql_type_hint() }}{% endfor %}

    @{% if is_pydantic_v2 %}model_validator{% else %}root_validator{% endif %}({% if is_pydantic_v2 %}mode="before"{% else %}pre=True{% endif %})
    def parse_data_record(cls, values: Any) -> Any:
        if not isinstance(values, dict):
            return values
        if "lastUpdatedTime" in values or "createdTime" in values:
            values["dataRecord"] = DataRecordGraphQL(
                created_time=values.pop("createdTime", None),
                last_updated_time=values.pop("lastUpdatedTime", None),
            )
        return values{% if data_class.has_field_of_type(ft.CDFExternalListField) %}
    @{% if is_pydantic_v2 %}field_validator{% else %}validator{% endif %}({% for field in data_class.fields_of_type(ft.CDFExternalListField) %}"{{ field.name }}", {% endfor %}{% if is_pydantic_v2 %}mode="before"{% else %}pre=True{% endif %})
    def clean_list(cls, value: Any) -> Any:
        if isinstance(value, list):
            return [v for v in value if v is not None] or None
        return value
{% endif %}{% if not is_pydantic_v2 and data_class.has_timeseries_fields() %}
    @validator({% for field in data_class.timeseries_fields() %}"{{ field.name }}", {% endfor %}pre=True)
    def parse_timeseries(cls, value: Any) -> Any:
        if isinstance(value, list):
            return [TimeSeries.load(v) if isinstance(v, dict) else v for v in value]
        elif isinstance(value, dict):
            return TimeSeries.load(value)
        return value
{% endif %}{% if data_class.has_field_of_type(ft.BaseConnectionField) %}
    @{% if is_pydantic_v2 %}field_validator{% else %}validator{% endif %}({% for field in data_class.fields_of_type(ft.BaseConnectionField) %}"{{ field.name }}", {% endfor %}{% if is_pydantic_v2 %}mode="before"{% else %}pre=True{% endif %})
    def parse_graphql(cls, value: Any) -> Any:
        if not isinstance(value, dict):
            return value
        if "items" in value:
            return value["items"]
        return value{% endif %}

    # We do the ignore argument type as we let pydantic handle the type checking
    @no_type_check
    def as_read(self) -> {{ data_class.read_name }}:
        """Convert this GraphQL format of {{ data_class.doc_name }} to the reading format."""
        if self.data_record is None:
            raise ValueError("This object cannot be converted to a read format because it lacks a data record.")
        return {{ data_class.read_name }}(
            space=self.space or DEFAULT_INSTANCE_SPACE,
            external_id=self.external_id,
            data_record=DataRecord(
                version=0,
                last_updated_time=self.data_record.last_updated_time,
                created_time=self.data_record.created_time,
            ),{% for field in data_class %}
            {{ field.name }}={{ field.as_read_graphql() }},{% endfor %}
        )

{% if data_class.is_writable or data_class.is_interface %}
    # We do the ignore argument type as we let pydantic handle the type checking
    @no_type_check
    def as_write(self) -> {{ data_class.write_name }}:
        """Convert this GraphQL format of {{ data_class.doc_name }} to the writing format."""
        return {{ data_class.write_name }}(
            space=self.space or DEFAULT_INSTANCE_SPACE,
            external_id=self.external_id,
            data_record=DataRecordWrite(existing_version=0),{% for field in data_class %}
            {{ field.name }}={{ field.as_write_graphql() }},{% endfor %}
        )
{% endif %}

class {{ data_class.read_name }}({{ data_class.read_base_class }}{% if is_pydantic_v2 and data_class.has_any_field_model_prefix %}, protected_namespaces=(){% endif %}):
    """This represents the reading version of {{ data_class.doc_name }}.

    It is used to when data is retrieved from CDF.

    Args:
        space: The space where the node is located.
        external_id: The external id of the {{ data_class.doc_name }}.
        data_record: The data record of the {{ data_class.doc_name }} node.{% for field in data_class %}
        {{ field.name }}: {{ field.argument_documentation }}{% endfor %}
    """
    _view_id: ClassVar[dm.ViewId] = dm.ViewId("{{ data_class.view_id.space }}", "{{ data_class.view_id.external_id }}", "{{ data_class.view_id.version }}")
    {% if not data_class.implements %}
    space: str = DEFAULT_INSTANCE_SPACE{% endif %}
    node_type: Union[dm.DirectRelationReference, None] = {% if data_class.node_type %}dm.DirectRelationReference("{{ data_class.node_type.space }}", "{{ data_class.node_type.external_id }}"){% else %}None{% endif %}{% for field in data_class.non_parent_fields %}
    {{ field.name }}: {{ field.as_read_type_hint() }}{% endfor %}
{% if data_class.is_writable or data_class.is_interface %}
    def as_write(self) -> {{ data_class.write_name }}:
        """Convert this read version of {{ data_class.doc_name }} to the writing version."""
        return {{ data_class.write_name }}(
            space=self.space,
            external_id=self.external_id,
            data_record=DataRecordWrite(existing_version=self.data_record.version),{% for field in data_class %}
            {{ field.name }}={{ field.as_write() }},{% endfor %}
        )

    def as_apply(self) -> {{ data_class.write_name }}:
        """Convert this read version of {{ data_class.doc_name }} to the writing version."""
        warnings.warn(
            "as_apply is deprecated and will be removed in v1.0. Use as_write instead.",
            UserWarning,
            stacklevel=2,
        )
        return self.as_write()


class {{ data_class.write_name }}({{ data_class.write_base_class }}{% if is_pydantic_v2 and data_class.has_any_field_model_prefix %}, protected_namespaces=(){% endif %}):
    """This represents the writing version of {{ data_class.doc_name }}.

    It is used to when data is sent to CDF.

    Args:
        space: The space where the node is located.
        external_id: The external id of the {{ data_class.doc_name }}.
        data_record: The data record of the {{ data_class.doc_name }} node.{% for field in data_class %}
        {{ field.name }}: {{ field.argument_documentation }}{% endfor %}
    """
    _view_id: ClassVar[dm.ViewId] = dm.ViewId("{{ data_class.view_id.space }}", "{{ data_class.view_id.external_id }}", "{{ data_class.view_id.version }}")
{% if not data_class.implements %}
    space: str = DEFAULT_INSTANCE_SPACE{% endif %}
    node_type: Union[dm.DirectRelationReference, None] = {% if data_class.node_type %}dm.DirectRelationReference("{{ data_class.node_type.space }}", "{{ data_class.node_type.external_id }}"){% else %}None{% endif %}{% for field in data_class.non_parent_fields %}
    {{ field.name }}: {{ field.as_write_type_hint() }}{% endfor %}

    def _to_instances_write(
        self,
        cache: set[tuple[str, str]],
        write_none: bool = False,
        allow_version_increase: bool = False,
    ) -> ResourcesWrite:{% if data_class.has_edge_with_property %}{% for dependency_class in data_class.dependencies_edges %}
        from .{{ dependency_class.file_name }} import {{ dependency_class.write_name }}{% endfor %}
{% endif %}
        resources = ResourcesWrite()
        if self.as_tuple_id() in cache:
            return resources{% if not data_class.has_container_fields %}
        cache.add(self.as_tuple_id())

        this_node = dm.NodeApply(
            space=self.space,
            external_id=self.external_id,
            existing_version=None if allow_version_increase else self.data_record.existing_version,{% if data_class.node_type %}
            type=self.node_type,{% endif %}
            sources=None,
        )
        resources.nodes.append(this_node)
        {% endif %}
{% if data_class.has_container_fields %}
        properties: dict[str, Any] = {}
        {% for field in data_class.container_fields %}
        if self.{{ field.name }} is not None{% if field.is_nullable %} or write_none{% endif %}:
            properties["{{ field.prop_name }}"] = {{ field.as_value() }}
        {% endfor %}

        if properties:
            this_node = dm.NodeApply(
                space=self.space,
                external_id=self.external_id,
                existing_version=None if allow_version_increase else self.data_record.existing_version,
                type=self.node_type,
                sources=[
                    dm.NodeOrEdgeData(
                        source=self._view_id,
                        properties=properties,
                )],
            )
            resources.nodes.append(this_node)
            cache.add(self.as_tuple_id())
        {% endif %}

{% for field in data_class.one_to_many_edges_with_properties %}
        for {{ field.variable }} in self.{{ field.name }} or []:
            if isinstance({{ field.variable }}, DomainRelationWrite):
                other_resources = {{ field.variable }}._to_instances_write(
                    cache,
                    self,
                    dm.DirectRelationReference("{{ field.edge_type.space }}", "{{ field.edge_type.external_id }}"),
)
                resources.extend(other_resources)
{% endfor %}{% for field in data_class.one_to_many_edges_without_properties %}
        edge_type = dm.DirectRelationReference("{{ field.edge_type.space }}", "{{ field.edge_type.external_id }}")
        for {{ field.variable }} in self.{{ field.name }} or []:{% if field.edge_direction == 'outwards' %}
            other_resources = DomainRelationWrite.from_edge_to_resources(
                cache,
                start_node=self,
                end_node={{field.variable}},
                edge_type=edge_type,
                write_none=write_none,
                allow_version_increase=allow_version_increase,
            ){% else %}
            other_resources = DomainRelationWrite.from_edge_to_resources(
                cache,
                start_node={{field.variable}},
                end_node=self,
                edge_type=edge_type,
                write_none=write_none,
                allow_version_increase=allow_version_increase,
            ){% endif %}
            resources.extend(other_resources)
{% endfor %}{% for field in data_class.one_to_one_direct_relations_with_source %}
        if isinstance(self.{{ field.name }}, DomainModelWrite):
            other_resources = self.{{ field.name }}._to_instances_write(cache)
            resources.extend(other_resources)
{% endfor %}{% for field in data_class.one_to_many_direct_relations_with_source %}
        for {{ field.variable }} in self.{{ field.name }} or []:
            if isinstance({{ field.variable }}, DomainModelWrite):
                other_resources = {{ field.variable }}._to_instances_write(cache)
                resources.extend(other_resources)
{% endfor %}{% for field in data_class.primitive_fields_of_type(dm.TimeSeriesReference) %}
        if isinstance(self.{{ field.name }}, {% if is_pydantic_v2 %}CogniteTimeSeries{% else %}TimeSeries{% endif %}):
            resources.time_series.append(self.{{ field.name }})
{% endfor %}{% for field in data_class.one_to_one_edge_without_properties %}{% if field.edge_direction == 'outwards' %}
        if self.{{field.name}} is not None:
            other_resources = DomainRelationWrite.from_edge_to_resources(
                cache,
                start_node=self,
                end_node=self.{{ field.name }},
                edge_type=dm.DirectRelationReference("{{ field.edge_type.space }}", "{{ field.edge_type.external_id }}"),
                write_none=write_none,
                allow_version_increase=allow_version_increase,
            )
            resources.extend(other_resources)
    {% else %}
        for {{ field.variable }} in self.{{ field.name }} or []:
            other_resources = DomainRelationWrite.from_edge_to_resources(
                cache,
                start_node={{field.variable}},
                end_node=self,
                edge_type=dm.DirectRelationReference("{{ field.edge_type.space }}", "{{ field.edge_type.external_id }}"),
                write_none=write_none,
                allow_version_increase=allow_version_increase,
            )
            resources.extend(other_resources)
    {% endif %}
{% endfor %}
        return resources


class {{ data_class.read_name }}Apply({{ data_class.write_name }}):
    def __new__(cls, *args, **kwargs) -> {{ data_class.read_name }}Apply:
        warnings.warn(
            "{{ data_class.read_name }}Apply is deprecated and will be removed in v1.0. Use {{ data_class.write_name }} instead."
            "The motivation for this change is that Write is a more descriptive name for the writing version of the"
            "{{ data_class.read_name }}.",
            UserWarning,
            stacklevel=2,
        )
        return super().__new__(cls)

{% endif %}
class {{ data_class.read_list_name }}(DomainModelList[{{ data_class.read_name }}]):
    """List of {{ data_class.doc_list_name }} in the read version."""

    _INSTANCE = {{ data_class.read_name }}
{% if data_class.is_writable or data_class.is_interface %}
    def as_write(self) -> {{ data_class.write_list_name }}:
        """Convert these read versions of {{ data_class.doc_name }} to the writing versions."""
        return {{ data_class.write_list_name }}([node.as_write() for node in self.data])

    def as_apply(self) -> {{ data_class.write_list_name }}:
        """Convert these read versions of primitive nullable to the writing versions."""
        warnings.warn(
            "as_apply is deprecated and will be removed in v1.0. Use as_write instead.",
            UserWarning,
            stacklevel=2,
        )
        return self.as_write()


class {{ data_class.write_list_name }}(DomainModelWriteList[{{ data_class.write_name }}]):
    """List of {{ data_class.doc_list_name }} in the writing version."""

    _INSTANCE = {{ data_class.write_name }}

class {{ data_class.read_name }}ApplyList({{ data_class.write_list_name }}): ...

{% endif %}

def {{ data_class.filter_name }}(
    view_id: dm.ViewId,{% for parm in list_method.parameters %}
    {{ parm.name }}: {{ parm.annotation }} = {{ parm.default }},{% endfor %}
    filter: dm.Filter | None = None,
) -> dm.Filter | None:
    filters: list[dm.Filter] = []{% for filter in list_method.implementations %}
    if {{ filter.condition }}:
        filters.append({{ filter.filter_call }}({{ filter.arguments }})){% endfor %}
    if filter:
        filters.append(filter)
    return dm.filters.And(*filters) if filters else None
