from types import SimpleNamespace
from unittest.mock import MagicMock

from app.services.knowledge.index_runtime import (
    KnowledgeBaseIndexInfo,
    build_kb_index_info,
    get_kb_index_info,
    get_kb_index_info_by_record,
)


def test_build_kb_index_info_uses_creator_for_default_namespace() -> None:
    # Even in the personal namespace, runtime ownership follows the KB record.
    db = MagicMock()
    knowledge_base = SimpleNamespace(
        namespace="default",
        user_id=42,
        json={"spec": {"summaryEnabled": True}},
    )

    kb_info = build_kb_index_info(
        db=db,
        knowledge_base=knowledge_base,
        current_user_id=7,
    )

    assert kb_info == KnowledgeBaseIndexInfo(
        index_owner_user_id=42,
        summary_enabled=True,
    )


def test_build_kb_index_info_uses_creator_for_group_namespace() -> None:
    # Group collaborators can trigger indexing, but storage ownership stays with the KB creator.
    db = MagicMock()
    knowledge_base = SimpleNamespace(
        namespace="team-a",
        user_id=42,
        json={"spec": {"summaryEnabled": False}},
    )

    kb_info = build_kb_index_info(
        db=db,
        knowledge_base=knowledge_base,
        current_user_id=7,
    )

    assert kb_info == KnowledgeBaseIndexInfo(
        index_owner_user_id=42,
        summary_enabled=False,
    )


def test_build_kb_index_info_uses_creator_for_organization_namespace() -> None:
    # Organization-scoped KBs keep the same invariant: runtime uses the persisted owner scope.
    db = MagicMock()
    knowledge_base = SimpleNamespace(
        namespace="org-a",
        user_id=42,
        json={"spec": {"summaryEnabled": True}},
    )

    kb_info = build_kb_index_info(
        db=db,
        knowledge_base=knowledge_base,
        current_user_id=7,
    )

    assert kb_info == KnowledgeBaseIndexInfo(
        index_owner_user_id=42,
        summary_enabled=True,
    )


def test_get_kb_index_info_by_record_delegates_to_index_resolution() -> None:
    db = MagicMock()
    knowledge_base = SimpleNamespace(
        namespace="default",
        user_id=42,
        json={"spec": {"summaryEnabled": True}},
    )

    kb_info = get_kb_index_info_by_record(
        db=db,
        knowledge_base=knowledge_base,
        current_user_id=7,
    )

    assert kb_info == KnowledgeBaseIndexInfo(
        index_owner_user_id=42,
        summary_enabled=True,
    )


def test_get_kb_index_info_falls_back_for_non_integer_id() -> None:
    db = MagicMock()

    kb_info = get_kb_index_info(
        db=db,
        knowledge_base_id="not-an-int",
        current_user_id=7,
    )

    db.query.assert_not_called()
    assert kb_info == KnowledgeBaseIndexInfo(
        index_owner_user_id=7,
        summary_enabled=False,
    )


def test_get_kb_index_info_falls_back_when_kb_is_missing() -> None:
    db = MagicMock()
    db.query.return_value.filter.return_value.first.return_value = None

    kb_info = get_kb_index_info(
        db=db,
        knowledge_base_id="9",
        current_user_id=7,
    )

    assert kb_info == KnowledgeBaseIndexInfo(
        index_owner_user_id=7,
        summary_enabled=False,
    )
