{{ imports }}

from http import HTTPMethod, HTTPStatus

from .api_client import APIClientProtocol, APIClientResponse


class API:
    def __init__(self, *, api_client: APIClientProtocol):
        self._api_client = api_client

{% macro method_parameters(endpoint) -%}
    self
    {%- with parameters=endpoint.method_parameters -%}
        {%- if parameters %}, *
        {%- for parameter in parameters -%}
            , {{ parameter.name }}: {{ parameter.type_hint }}
            {%- if not parameter.required %} = {{ parameter.default_value }}{% endif %}
        {%- endfor %}
        {%- endif %}
    {%- endwith %}
{%- endmacro %}

{% macro method_body(endpoint) -%}
    self._api_client.send_request(
        method=HTTPMethod.{{ endpoint.http_method|upper }},
        {% with url=endpoint.path.url -%}
            path={% if endpoint.path.path_parameters %}f{% endif %}"{{ url }}",
        {%- endwith %}
        {% if endpoint.path.query_parameters -%}
        query={
            {%- for parameter in endpoint.path.query_parameters %}
            "{{ parameter.original_name }}": {{ parameter.name }},
            {%- endfor %}
        },
        {%- endif %}
        {% if endpoint.request_body -%}
            content_type="{{ endpoint.request_body.content_type }}",
            data={{ endpoint.request_body.name }},
            data_type={{ endpoint.request_body.type_hint }},
        {%- endif %}
        {% if endpoint.response.media_type -%}
            accept="{{ endpoint.response.media_type }}",
        {%- endif %}
        {% if endpoint.response.has_content %}
            response_type={{ endpoint.response.type_hint }},
        {% endif %}
        {% if endpoint.response.status_code != 200 -%}
            expected_status_code=HTTPStatus.{{ endpoint.response.status_code.name }},
        {%- endif %}
    )
{%- endmacro %}

{% for endpoint in endpoints %}
    {% if endpoint.response.status_code == 200 %}
    def {{ endpoint.method_name }}_response({{ method_parameters(endpoint) }}) -> APIClientResponse[{{ endpoint.response.type_hint }}]:
        {% if endpoint.docstring %}"""
{{ endpoint.docstring }}
        """{% endif %}
        return {{ method_body(endpoint) }}
    {% endif %}

    def {{ endpoint.method_name }}({{ method_parameters(endpoint) }}) -> {{ endpoint.response.type_hint }}:
        {% if endpoint.docstring %}"""
{{ endpoint.docstring }}
        """{% endif %}
        return {{ method_body(endpoint) }}.data
{% endfor %}
