"""Generated GraphQL client for {{ client_name }}.

Provides typed methods for GraphQL operations.
"""

from __future__ import annotations
{%- if operations %}

from typing import Any
{%- endif %}

from ..models import *
from .base_client import GraphQLClient


class {{ client_name }}Client:
    """GraphQL client for {{ client_name }} operations."""

    def __init__(self, client: GraphQLClient):
        self.client = client
{%- for item in operations %}
    {#- item is a dict with 'op' (IROperation) and 'method_name' (Python method name) -#}
    {%- set op = item.op -%}
    {%- set method_name = item.method_name -%}
    {#- Sort arguments: required first, optional last -#}
    {%- set required_args = op.arguments | selectattr('is_optional', 'false') | list -%}
    {%- set optional_args = op.arguments | selectattr('is_optional', 'true') | list -%}
    {%- set sorted_args = required_args + optional_args %}

    def {{ method_name }}(
        self,
        {%- for arg in sorted_args %}
        {{ arg.name | snake_case | safe_param }}: {% if arg.is_list %}list[{{ arg.type_name }}]{% else %}{{ arg.type_name }}{% endif %}{% if arg.is_optional %} | None = None{% endif %},
        {%- endfor %}
    ) -> {% if op.is_return_list %}list[{{ op.return_type }}]{% else %}{{ op.return_type }}{% endif %}{% if op.is_return_optional %} | None{% endif %}:
        """{{ (op.description or op.name) | truncate(86, True, '...', 0) }}"""
        {%- set field_selection = op.return_type | expand_fields %}
        query = """
        {{ op.operation_type }} {{ op.name | pascal_case }}(
            {%- for arg in op.arguments %}
            ${{ arg.name }}: {% if arg.is_list %}[{% endif %}{{ arg.type_name }}{% if not arg.is_optional %}!{% endif %}{% if arg.is_list %}]{% endif %}{% if not loop.last %},{% endif %}
            {%- endfor %}
        ) {
            {{ op.name }}(
                {%- for arg in op.arguments %}
                {{ arg.name }}: ${{ arg.name }}{% if not loop.last %},{% endif %}
                {%- endfor %}
            ){% if field_selection %} {
                {{ field_selection }}
            }{% endif %}
        }
        """
        variables: dict[str, Any] = {
            {%- for arg in op.arguments %}
            "{{ arg.name }}": {{ arg.name | snake_case | safe_param }},
            {%- endfor %}
        }
        # Remove None values
        variables = {k: v for k, v in variables.items() if v is not None}
        result = self.client.execute(query, variables)
        data = result.get("{{ op.name }}")
{%- if op.is_return_optional %}
        if data is None:
            return None
{%- endif %}
{#- Scalar types don't have model_validate - return data directly -#}
{%- set scalar_types = ['String', 'Int', 'Float', 'Boolean', 'ID', 'MacAddress', 'IPAddress', 'IPRange', 'IPSubnet', 'NetworkSubnet', 'Port', 'PortRange', 'DateTime', 'Date', 'Time', 'Timestamp', 'Url', 'Email', 'Asn', 'Json', 'Object', 'Map'] %}
{%- if op.return_type in scalar_types %}
{%- if op.is_return_list %}
        return data if data is not None else []
{%- else %}
        return data
{%- endif %}
{%- else %}
{%- if op.is_return_list %}
        return [{{ op.return_type }}.model_validate(item) for item in data] if data is not None else []
{%- else %}
        return {{ op.return_type }}.model_validate(data)
{%- endif %}
{%- endif %}
{%- endfor %}
