from __future__ import annotations

from typing import Any, List

from infrahub_sync.plugin_loader import PluginLoader
# Load model class dynamically at runtime (honor adapters_path, safe fallback)
try:
    _loader = PluginLoader.from_env_and_args(adapter_paths={{ config.adapters_path or [] }})
{% if adapter.adapter %}
    _spec = "{{ adapter.adapter.split(':')[0] }}"
{% else %}
    _spec = "{{ adapter.name }}"
{% endif %}
    _ModelBaseClass = _loader.resolve(_spec, default_class_candidates=("Model",))
except Exception:
    # Fallback: use DiffSyncModel to avoid import-time failure
    from diffsync import DiffSyncModel as _FallbackModel
    _ModelBaseClass = _FallbackModel

# -------------------------------------------------------
# AUTO-GENERATED FILE, DO NOT MODIFY
#  This file has been generated with the command `infrahub-sync generate`
#  All modifications will be lost the next time you reexecute this command
# -------------------------------------------------------
{%- for nodekind, node in schema.items() -%}
{%-  if node | get_identifiers(config) and config | has_node(node.kind) %}
class {{ nodekind }}(_ModelBaseClass):
    _modelname = "{{ node.kind }}"
    _identifiers = {{ node | get_identifiers(config) | list_to_set }}
    _attributes = {{ node | get_attributes(config) | list_to_set }}

{%-      for attr in node.attributes -%}
{%-          if config | has_field(node.kind, attr.name) %}
    {{ attr.name }}: {{ attr | get_kind }}
{%-          endif -%}
{%-      endfor -%}
{%-      for rel in node.relationships -%}
{%-          if config | has_field(node.kind, rel.name) %}
    {{ rel.name }}: {{ rel | get_kind }}
{%-          endif -%}
{%-      endfor %}

    local_id: str | None = None
    local_data: Any | None = None
{%  endif %}
{%- endfor %}
