"""Tests that hand-written api/*.py classes properly inherit from generated bases."""

from __future__ import annotations

import pytest

from goodmem._base import AsyncCRUDNamespace, CRUDNamespace


# ---------------------------------------------------------------------------
# Inheritance chain assertions
# ---------------------------------------------------------------------------

class TestEmbeddersInheritance:
    def test_inherits_from_generated_base(self):
        from goodmem.api.embedders import EmbeddersAPI, AsyncEmbeddersAPI
        from goodmem._generated.api.embedders_base import EmbeddersAPIBase, AsyncEmbeddersAPIBase
        assert issubclass(EmbeddersAPI, EmbeddersAPIBase)
        assert issubclass(AsyncEmbeddersAPI, AsyncEmbeddersAPIBase)

    def test_full_chain_to_crud_namespace(self):
        from goodmem.api.embedders import EmbeddersAPI
        assert issubclass(EmbeddersAPI, CRUDNamespace)

    def test_create_is_overridden(self):
        """EmbeddersAPI overrides create() to apply domain defaults."""
        from goodmem.api.embedders import EmbeddersAPI
        from goodmem._generated.api.embedders_base import EmbeddersAPIBase
        # create should be in EmbeddersAPI's own dict (not just inherited)
        assert "create" in EmbeddersAPI.__dict__

    def test_get_is_inherited_not_overridden(self):
        """EmbeddersAPI does not override get() — it's inherited from the base."""
        from goodmem.api.embedders import EmbeddersAPI
        assert "get" not in EmbeddersAPI.__dict__


class TestRerankersInheritance:
    def test_inherits_from_generated_base(self):
        from goodmem.api.rerankers import RerankersAPI, AsyncRerankersAPI
        from goodmem._generated.api.rerankers_base import RerankersAPIBase, AsyncRerankersAPIBase
        assert issubclass(RerankersAPI, RerankersAPIBase)
        assert issubclass(AsyncRerankersAPI, AsyncRerankersAPIBase)

    def test_full_chain_to_crud_namespace(self):
        from goodmem.api.rerankers import RerankersAPI
        assert issubclass(RerankersAPI, CRUDNamespace)


class TestLlmsInheritance:
    def test_inherits_from_generated_base(self):
        from goodmem.api.llms import LlmsAPI, AsyncLlmsAPI
        from goodmem._generated.api.llms_base import LlmsAPIBase, AsyncLlmsAPIBase
        assert issubclass(LlmsAPI, LlmsAPIBase)
        assert issubclass(AsyncLlmsAPI, AsyncLlmsAPIBase)

    def test_full_chain_to_crud_namespace(self):
        from goodmem.api.llms import LlmsAPI
        assert issubclass(LlmsAPI, CRUDNamespace)


class TestSpacesInheritance:
    def test_inherits_from_generated_base(self):
        from goodmem.api.spaces import SpacesAPI, AsyncSpacesAPI
        from goodmem._generated.api.spaces_base import SpacesAPIBase, AsyncSpacesAPIBase
        assert issubclass(SpacesAPI, SpacesAPIBase)
        assert issubclass(AsyncSpacesAPI, AsyncSpacesAPIBase)

    def test_full_chain_to_crud_namespace(self):
        from goodmem.api.spaces import SpacesAPI
        assert issubclass(SpacesAPI, CRUDNamespace)

    def test_create_is_overridden(self):
        from goodmem.api.spaces import SpacesAPI
        assert "create" in SpacesAPI.__dict__


class TestMemoriesInheritance:
    def test_inherits_from_generated_base(self):
        from goodmem.api.memories import MemoriesAPI, AsyncMemoriesAPI
        from goodmem._generated.api.memories_base import MemoriesAPIBase, AsyncMemoriesAPIBase
        assert issubclass(MemoriesAPI, MemoriesAPIBase)
        assert issubclass(AsyncMemoriesAPI, AsyncMemoriesAPIBase)

    def test_full_chain_to_crud_namespace(self):
        from goodmem.api.memories import MemoriesAPI
        assert issubclass(MemoriesAPI, CRUDNamespace)

    def test_create_is_overridden(self):
        """MemoriesAPI overrides create() to support file_path and content-type inference."""
        from goodmem.api.memories import MemoriesAPI
        assert "create" in MemoriesAPI.__dict__

    def test_retrieve_is_overridden(self):
        """MemoriesAPI overrides retrieve() to support space_ids and post-processor shortcuts."""
        from goodmem.api.memories import MemoriesAPI
        assert "retrieve" in MemoriesAPI.__dict__

    def test_batch_create_is_overridden(self):
        """MemoriesAPI overrides batch_create() to auto-infer content_type."""
        from goodmem.api.memories import MemoriesAPI
        assert "batch_create" in MemoriesAPI.__dict__

    def test_batch_get_inherited(self):
        """MemoriesAPI does NOT override batch_get — pure structural, no domain logic."""
        from goodmem.api.memories import MemoriesAPI
        assert "batch_get" not in MemoriesAPI.__dict__

    def test_retrieve_with_body_helper_accessible(self):
        """MemoriesAPI inherits _retrieve_with_body from the base for DRY streaming logic."""
        from goodmem.api.memories import MemoriesAPI
        assert hasattr(MemoriesAPI, "_retrieve_with_body")


# ---------------------------------------------------------------------------
# Thin subclasses (no domain overrides) — nothing should be overridden
# ---------------------------------------------------------------------------

class TestApikeysInheritance:
    def test_inherits_from_generated_base(self):
        from goodmem.api.apikeys import ApikeysAPI, AsyncApikeysAPI
        from goodmem._generated.api.apikeys_base import ApikeysAPIBase, AsyncApikeysAPIBase
        assert issubclass(ApikeysAPI, ApikeysAPIBase)
        assert issubclass(AsyncApikeysAPI, AsyncApikeysAPIBase)

    def test_no_overrides(self):
        """ApikeysAPI is a thin wrapper with no method overrides."""
        from goodmem.api.apikeys import ApikeysAPI
        own_methods = {k for k, v in ApikeysAPI.__dict__.items() if callable(v) and not k.startswith("__")}
        assert own_methods == set(), f"Unexpected overrides: {own_methods}"


class TestUsersInheritance:
    def test_inherits_from_generated_base(self):
        from goodmem.api.users import UsersAPI, AsyncUsersAPI
        from goodmem._generated.api.users_base import UsersAPIBase, AsyncUsersAPIBase
        assert issubclass(UsersAPI, UsersAPIBase)
        assert issubclass(AsyncUsersAPI, AsyncUsersAPIBase)

    def test_methods_inherited(self):
        from goodmem.api.users import UsersAPI
        assert hasattr(UsersAPI, "me")
        assert hasattr(UsersAPI, "get")

    def test_no_method_overrides(self):
        from goodmem.api.users import UsersAPI
        own_methods = {k for k, v in UsersAPI.__dict__.items() if callable(v) and not k.startswith("__")}
        assert own_methods == set(), f"Unexpected overrides: {own_methods}"


class TestOcrInheritance:
    def test_inherits_from_generated_base(self):
        from goodmem.api.ocr import OcrAPI, AsyncOcrAPI
        from goodmem._generated.api.ocr_base import OcrAPIBase, AsyncOcrAPIBase
        assert issubclass(OcrAPI, OcrAPIBase)
        assert issubclass(AsyncOcrAPI, AsyncOcrAPIBase)

    def test_no_method_overrides(self):
        from goodmem.api.ocr import OcrAPI
        own_methods = {k for k, v in OcrAPI.__dict__.items() if callable(v) and not k.startswith("__")}
        assert own_methods == set(), f"Unexpected overrides: {own_methods}"


class TestSystemInheritance:
    def test_inherits_from_generated_base(self):
        from goodmem.api.system import SystemAPI, AsyncSystemAPI
        from goodmem._generated.api.system_base import SystemAPIBase, AsyncSystemAPIBase
        assert issubclass(SystemAPI, SystemAPIBase)
        assert issubclass(AsyncSystemAPI, AsyncSystemAPIBase)

    def test_no_method_overrides(self):
        from goodmem.api.system import SystemAPI
        own_methods = {k for k, v in SystemAPI.__dict__.items() if callable(v) and not k.startswith("__")}
        assert own_methods == set(), f"Unexpected overrides: {own_methods}"


class TestAdminInheritance:
    def test_inherits_from_generated_base(self):
        from goodmem.api.admin import AdminAPI, AsyncAdminAPI
        from goodmem._generated.api.admin_base import AdminAPIBase, AsyncAdminAPIBase
        assert issubclass(AdminAPI, AdminAPIBase)
        assert issubclass(AsyncAdminAPI, AsyncAdminAPIBase)

    def test_no_method_overrides(self):
        from goodmem.api.admin import AdminAPI
        own_methods = {k for k, v in AdminAPI.__dict__.items() if callable(v) and not k.startswith("__")}
        assert own_methods == set(), f"Unexpected overrides: {own_methods}"
