{#- Helper macros for test generation -#}

{#- Generate path parameter variables and build URL path -#}
{%- macro generate_path(endpoint) %}
{% if endpoint.path_parameters %}
{% for param in endpoint.path_parameters %}
        {{ param.python_name }} = uuid4()
{% endfor %}
        path = "{{ endpoint.path }}".format(
{% for param in endpoint.path_parameters %}
            {{ param.python_name }}={{ param.python_name }}{% if not loop.last %},{% endif %}

{% endfor %}
        )
{% else %}
        path = "{{ endpoint.path }}"
{% endif %}
        url = f"{self.base_url}{path}"
{%- endmacro %}

{#- Parse request body data into Pydantic model -#}
{%- macro parse_body(endpoint) %}
{% if endpoint.bodies -%}
{%- set body_type_string = endpoint.bodies[0].prop.get_type_string() -%}
{%- if 'Union[' in body_type_string %}
{%- set union_classes = body_type_string.replace('Union[', '').replace(']', '').replace("'", "").split(', ') %}
        # For Union types, use TypeAdapter to validate
        body_adapter = TypeAdapter(Union[{{ union_classes | join(', ') }}])
        body = body_adapter.validate_python(body_data)
{% elif 'list[' in body_type_string %}
        # For list types, use TypeAdapter to validate
        body_adapter = TypeAdapter({{ body_type_string }})
        body = body_adapter.validate_python(body_data)
{% else -%}
{%- set body_class_name = body_type_string.split('[')[0] %}
        # Parse body_data into Pydantic model
        body = {{ body_class_name }}.model_validate(body_data)
{% endif -%}
{% endif %}
{%- endmacro %}

{#- Generate service method call arguments -#}
{%- macro method_call_args(endpoint, use_test_values=false) %}
{% for param in endpoint.path_parameters %}
            {{ param.python_name }}={{ param.python_name }},
{% endfor %}
{% if endpoint.bodies %}
{%- if endpoint.bodies | length == 1 and endpoint.bodies[0].prop.get_type_string().startswith("list[") %}
            items=body,
{% else %}
            body=body,
{% endif %}
{% endif %}
{% for param in endpoint.query_parameters %}
{% if param.required %}
{% if use_test_values %}
            {{ param.python_name }}="test_value",
{% else %}
            {{ param.python_name }}={{ param.python_name }},
{% endif %}
{% endif %}
{% endfor %}
{%- endmacro %}

{#- Hypothesis @given decorator parameters for response and body -#}
{#- Uses strategy functions that load from example cache with fallback to from_schema() -#}
{%- macro given_params(endpoint, status_code, include_response=true) %}
{%- if include_response %}
        response_data=get_response_strategy("{{ endpoint.path }}", "{{ endpoint.method }}", "{{ status_code }}"),
{%- endif %}
{%- if endpoint.bodies %}
        body_data=get_request_body_strategy("{{ endpoint.path }}", "{{ endpoint.method }}"),
{%- endif %}
{%- for param in endpoint.query_parameters %}
{%- if param.required %}
        {{ param.python_name }}=get_parameter_strategy("{{ endpoint.path }}", "{{ endpoint.method }}", "{{ param.name }}"),
{%- endif %}
{%- endfor %}
{%- endmacro %}

{#- Test function parameters (after self, backend) -#}
{%- macro test_params(endpoint, include_response=true) %}
{%- if include_response %}
        response_data,
{%- endif %}
{%- if endpoint.bodies %}
        body_data,
{%- endif %}
{%- for param in endpoint.query_parameters %}
{%- if param.required %}
        {{ param.python_name }},
{%- endif %}
{%- endfor %}
{%- endmacro %}
