"""
Tests for {{ api_name }} service.

Generated from OpenAPI spec version {{ spec_version }}.
Uses property-based testing with hypothesis-jsonschema.
"""

from unittest.mock import Mock
from uuid import uuid4

import pook
import pytest
from hypothesis import HealthCheck, given, settings
from pydantic import TypeAdapter
from typing import Union

from satvu import SatVuSDK, create_http_client
from satvu.http.errors import ClientError, ServerError, HttpError
from .test_schemas import (
    get_response_schema,
    get_request_body_schema,
    get_parameter_schema,
    has_request_body,
    has_parameter,
    get_response_strategy,
    get_request_body_strategy,
    get_parameter_strategy,
)
{%- set all_imports = [] %}
{%- for endpoint_data in endpoints %}
{%- for relative in endpoint_data.endpoint.relative_imports %}
{%- if '.types import' not in relative %}
{%- set fixed_import = relative.replace('from ' + api_name + '.', 'from satvu.services.' + api_name + '.') %}
{%- set _ = all_imports.append(fixed_import) %}
{%- endif %}
{%- endfor %}
{%- for response in endpoint_data.endpoint.responses %}
{%- if response.prop %}
{%- for relative in response.prop.get_imports(prefix=api_name + '.') %}
{%- if '.types import' not in relative %}
{%- set fixed_import = relative.replace('from ' + api_name + '.', 'from satvu.services.' + api_name + '.') %}
{%- set _ = all_imports.append(fixed_import) %}
{%- endif %}
{%- endfor %}
{%- endif %}
{%- endfor %}
{%- if endpoint_data.endpoint.bodies %}
{%- for body in endpoint_data.endpoint.bodies %}
{%- if body.prop %}
{%- for relative in body.prop.get_imports(prefix=api_name + '.') %}
{%- if '.types import' not in relative %}
{%- set fixed_import = relative.replace('from ' + api_name + '.', 'from satvu.services.' + api_name + '.') %}
{%- set _ = all_imports.append(fixed_import) %}
{%- endif %}
{%- endfor %}
{%- endif %}
{%- endfor %}
{%- endif %}
{%- endfor %}
{%- for import in all_imports | unique | sort %}

{{ import }}
{%- endfor %}

{% from "macros/test_success.jinja" import success_test %}
{% from "macros/test_error.jinja" import error_test_with_schema, error_test_static %}
{% from "macros/test_no_content.jinja" import no_content_test %}

@pytest.mark.parametrize("backend", ["stdlib", "httpx", "urllib3", "requests"])
class Test{{ service_class_name }}:
    """Property-based tests for {{ service_class_name }}."""

    @pytest.fixture(autouse=True)
    def setup(self, backend):
        """Set up test fixtures before each test method."""
        # Mock the auth token
        mock_get_token = Mock(return_value="test_token")

        # Construct base URL for the {{ api_name }} service
        # We need to match how SDKClient builds its base_url in __init__
        subdomain = "api"
        env_part = "qa."
        base_path = "{{ base_path }}"
        self.base_url = f"https://{subdomain}.{env_part}satellitevu.com{base_path}"

        # Create HTTP client with specified backend
        http_client = create_http_client(
            backend=backend,
            base_url=self.base_url,
            get_token=mock_get_token,
        )

        self.sdk = SatVuSDK(
            client_id="test_client_id",
            client_secret="test_client_secret",  # pragma: allowlist secret
            http_client=http_client,
            env="qa",
        )

        # Override the token getter with our mock
        self.sdk.{{ api_name }}._get_token = mock_get_token

{% for endpoint_data in endpoints %}
{%- set endpoint = endpoint_data.endpoint %}
{# ---- 2xx Success Response Tests ---- #}
{%- for status_code, response_info in endpoint_data.responses.items() %}
{{ success_test(endpoint, status_code, response_info, api_name) }}
{% endfor %}
{# ---- 4xx/5xx Error Response Tests ---- #}
{% for status_code, error_info in endpoint_data.error_responses.items() %}
{%- set exception_type = "ClientError" if status_code | int < 500 else "ServerError" %}
{% if error_info.has_schema %}
{{ error_test_with_schema(endpoint, status_code, exception_type, api_name) }}
{% else %}
{{ error_test_static(endpoint, status_code, exception_type, api_name) }}
{% endif %}
{% endfor %}
{# ---- 204 No Content Tests ---- #}
{%- if endpoint_data.has_204 %}
{{ no_content_test(endpoint, api_name) }}
{%- endif %}
{%- endfor %}
