from __future__ import annotations
{%  if has_datetime %}
from datetime import datetime{% endif %}
{% if has_circular_imports %}from typing import TYPE_CHECKING, ClassVar, Optional, Union{% else %}from typing import ClassVar, Optional{% endif %}

from cognite.client import data_modeling as dm{% if  has_alias %}
from pydantic import Field{% endif %}

from ._core import CircularModelApply, DomainModel, InstancesApply, TypeList
{% if has_circular_imports %}
{{ circular_imports }}{% endif %}
__all__ = ["{{ view_name }}", "{{ view_name }}Apply", "{{ view_name }}List"]


class {{ view_name }}(DomainModel):
    space: ClassVar[str] = "{{ view_space }}"
    {{ read_fields }}


class {{ view_name }}Apply(CircularModelApply):
    space: ClassVar[str] = "{{ view_space }}"
    {{ write_fields }}

    def _to_instances_apply(self, cache: set[str]) -> InstancesApply:
        if self.external_id in cache:
            return InstancesApply([], [])
        node_data = {{ sources }}{% if add_direct_relations %}{{ add_direct_relations }}{% endif %}
        this_node = dm.NodeApply(
            space=self.space,
            external_id=self.external_id,
            existing_version=self.existing_version,
            sources=[node_data],
        )
        nodes = [this_node]
        edges = []
{%  if add_edges %}
{{ add_edges }}
{% endif %}
        return InstancesApply(nodes, edges)
{% if create_edges %}
{{ create_edges }}
{% endif %}

class {{ view_name }}List(TypeList[{{ view_name }}]):
    _NODE = {{ view_name }}
