"""
Tests for Brynq API Server — OpenAI endpoint registry and schema generation.

Migrated from rest_api.py tests to api_server.py after API consolidation.
"""

import json
import pytest


class TestEndpointRegistry:
    def test_endpoints_registered(self):
        from brynq.api_server import _OPENAI_ENDPOINT_REGISTRY
        assert len(_OPENAI_ENDPOINT_REGISTRY) > 0

    def test_all_have_method(self):
        from brynq.api_server import _OPENAI_ENDPOINT_REGISTRY
        for ep in _OPENAI_ENDPOINT_REGISTRY:
            assert ep["method"] in ("GET", "POST", "PUT", "DELETE"), f"Bad method on {ep['path']}"

    def test_all_have_path(self):
        from brynq.api_server import _OPENAI_ENDPOINT_REGISTRY
        for ep in _OPENAI_ENDPOINT_REGISTRY:
            assert ep["path"].startswith("/"), f"Bad path: {ep['path']}"

    def test_all_have_description(self):
        from brynq.api_server import _OPENAI_ENDPOINT_REGISTRY
        for ep in _OPENAI_ENDPOINT_REGISTRY:
            assert ep.get("description"), f"Missing description on {ep['path']}"

    def test_groups_exist(self):
        from brynq.api_server import _OPENAI_ENDPOINT_REGISTRY
        groups = set(ep.get("group", "") for ep in _OPENAI_ENDPOINT_REGISTRY)
        assert "messaging" in groups
        assert "tasks" in groups

    def test_core_endpoints_present(self):
        from brynq.api_server import _OPENAI_ENDPOINT_REGISTRY
        paths = [ep["path"] for ep in _OPENAI_ENDPOINT_REGISTRY]
        assert "/channels" in paths or any("/channels" in p for p in paths)
        assert "/inbox" in paths
        assert "/tasks" in paths or any(p == "/tasks" for p in paths)
        assert "/health" in paths


class TestOpenAIToolsSchema:
    def test_schema_returns_list(self):
        from brynq.api_server import get_openai_tools_schema
        schema = get_openai_tools_schema()
        assert isinstance(schema, list)
        assert len(schema) > 0

    def test_schema_format(self):
        from brynq.api_server import get_openai_tools_schema
        schema = get_openai_tools_schema()
        for tool in schema:
            assert tool["type"] == "function"
            assert "function" in tool
            func = tool["function"]
            assert "name" in func
            assert "description" in func
            assert "parameters" in func
            assert func["parameters"]["type"] == "object"

    def test_schema_is_json_serializable(self):
        from brynq.api_server import get_openai_tools_schema
        schema = get_openai_tools_schema()
        dumped = json.dumps(schema)
        assert len(dumped) > 0


class TestRegisterFunction:
    def test_register_returns_entry(self):
        from brynq.api_server import _register_openai_endpoint, _OPENAI_ENDPOINT_REGISTRY
        initial_count = len(_OPENAI_ENDPOINT_REGISTRY)
        entry = _register_openai_endpoint("GET", "/test_endpoint_xyz", "Test endpoint", group="test")
        assert entry["method"] == "GET"
        assert entry["path"] == "/test_endpoint_xyz"

    def test_register_with_params(self):
        from brynq.api_server import _register_openai_endpoint
        entry = _register_openai_endpoint("GET", "/test_params_xyz", "With params",
                          parameters=[{"name": "q", "in": "query", "type": "string"}],
                          group="test")
        assert "parameters" in entry
        assert entry["parameters"][0]["name"] == "q"

    def test_register_with_body(self):
        from brynq.api_server import _register_openai_endpoint
        entry = _register_openai_endpoint("POST", "/test_body_xyz", "With body",
                          body_schema={"type": "object", "properties": {"msg": {"type": "string"}}},
                          group="test")
        assert "body_schema" in entry
