{% extends "python.j2" %}
{% import "macros.j2" as fn %}
{% block pylint %}
# pylint: skip-file
# flake8: noqa
{% endblock %}

{% block imports %}
from azure.cli.core.aaz import *
{% endblock %}

{% block code %}
{%- for client in leaf.iter_clients() %}

@register_client({{ client.registered_name|constant_convert }})
class {{ client.cls_name }}(AAZBaseClient):

    {%- if client.endpoints.type == "template" %}
    _CLOUD_HOST_TEMPLATES = {
        {%- for cloud, template in client.endpoints.iter_hosts() %}
        CloudNameEnum.{{cloud}}: {{ template|constant_convert }},
        {%- endfor %}
    }
    {%- if client.endpoints.cloud_metadata %}
    _CLOUD_HOST_METADATA_INDEX = {{ client.endpoints.cloud_metadata.selector_index|constant_convert }}
    {%- if client.endpoints.cloud_metadata.prefix_template %}
    _CLOUD_HOST_METADATA_PREFIX_TEMPLATE = {{ client.endpoints.cloud_metadata.prefix_template|constant_convert }}
    {%- endif %}
    {%- endif %}
    {%- elif client.endpoints.type == "http-operation" %}
    {%- endif %}

    _AAD_CREDENTIAL_SCOPES = [
        {%- for scope in client.aad_scopes %}
        {{ scope|constant_convert }},
        {%- endfor %}
    ]

    @classmethod
    def _build_base_url(cls, ctx, **kwargs):
        {%- if client.endpoints.type == "template" %}
        {%- if client.endpoints.cloud_metadata %}
        {%- if client.endpoints.cloud_metadata.prefix_template %}
        endpoint = None
        suffix = cls.get_cloud_suffix(ctx, cls._CLOUD_HOST_METADATA_INDEX)
        if suffix:
            endpoint = cls._CLOUD_HOST_METADATA_PREFIX_TEMPLATE + suffix
        {%- else %}
        endpoint = cls.get_cloud_endpoint(ctx, cls._CLOUD_HOST_METADATA_INDEX)
        {%- endif %}
        {%- else %}
        endpoint = None
        {%- endif %}
        if not endpoint:
            endpoint = cls._CLOUD_HOST_TEMPLATES.get(ctx.cli_ctx.cloud.name, None)
        return endpoint
        {%- elif client.endpoints.type == "http-operation" %}
        cls._fetch_endpoint(ctx, **kwargs)
        endpoint = ctx.selectors.{{client.endpoints.selector.name}}.required().to_serialized_data()
        if not isinstance(endpoint, str):
            raise ValueError(f"Invalid host value type: '{type(endpoint)}' expected 'str'")
        return endpoint
        {%- endif %}

    @classmethod
    def _build_configuration(cls, ctx, credential, **kwargs):
        return AAZClientConfiguration(
            credential=credential,
            credential_scopes=cls._AAD_CREDENTIAL_SCOPES,
            **kwargs
        )

    {%- if client.endpoints.type == "http-operation" %}

    @classmethod
    def _fetch_endpoint(cls, ctx, **kwargs):
        cls.{{ client.endpoints.selector.cls_name }}(ctx=ctx, name={{client.endpoints.selector.name|constant_convert}})
        cls.{{ client.endpoints.operation.name }}(ctx=ctx)()
    {{ fn.generate_json_selector(client.endpoints.selector) }}
    {{ fn.generate_http_operation(client.endpoints.operation, client.endpoints.client_name, client.helper_cls_name, false) }}
    {%- endif %}

{{ fn.generate_helper_cls(client) }}
{{ "" }}
{%- endfor %}

__all__ = [
    {%- for client in leaf.iter_clients() %}
    {{ client.cls_name|constant_convert }},
    {%- endfor %}
]
{% endblock %}