"""
Tests for base class concrete methods in the advanced_search_system.

Each test class creates a minimal concrete subclass that inherits from the ABC,
implements abstract methods as no-ops, then exercises the concrete helper methods.
"""

from unittest.mock import Mock

from local_deep_research.advanced_search_system.findings.base_findings import (
    BaseFindingsRepository,
)
from local_deep_research.advanced_search_system.questions.base_question import (
    BaseQuestionGenerator,
)
from local_deep_research.advanced_search_system.questions.followup.base_followup_question import (
    BaseFollowUpQuestionGenerator,
)


# ---------------------------------------------------------------------------
# Minimal concrete subclasses
# ---------------------------------------------------------------------------


class ConcreteQuestionGenerator(BaseQuestionGenerator):
    def generate_questions(
        self,
        current_knowledge,
        query,
        questions_per_iteration,
        questions_by_iteration,
    ):
        return []


class ConcreteFollowUpQuestionGenerator(BaseFollowUpQuestionGenerator):
    def generate_contextualized_query(
        self, follow_up_query, original_query, past_findings, **kwargs
    ):
        return ""


class ConcreteFindingsRepository(BaseFindingsRepository):
    def add_finding(self, query, finding):
        pass

    def get_findings(self, query):
        return []

    def clear_findings(self, query):
        pass

    def synthesize_findings(
        self, query, sub_queries, findings, accumulated_knowledge
    ):
        return ""


# ---------------------------------------------------------------------------
# Tests
# ---------------------------------------------------------------------------


class TestBaseQuestionGenerator:
    def setup_method(self):
        self.gen = ConcreteQuestionGenerator(model=Mock())

    def test_format_previous_questions(self):
        questions_by_iteration = {
            1: ["What is X?", "What is Y?"],
            2: ["How does Z work?"],
        }
        result = self.gen._format_previous_questions(questions_by_iteration)
        assert "Iteration 1:" in result
        assert "- What is X?" in result
        assert "- What is Y?" in result
        assert "Iteration 2:" in result
        assert "- How does Z work?" in result

    def test_format_previous_questions_empty(self):
        result = self.gen._format_previous_questions({})
        assert result == ""


class TestBaseFollowUpQuestionGenerator:
    def setup_method(self):
        self.gen = ConcreteFollowUpQuestionGenerator(model=Mock())

    def test_init_sets_empty_context(self):
        assert self.gen.follow_up_context == {}

    def test_set_follow_up_context(self):
        ctx = {"past_findings": "some findings", "original_query": "test query"}
        self.gen.set_follow_up_context(ctx)
        assert self.gen.follow_up_context is ctx

    def test_generate_questions_returns_query(self):
        result = self.gen.generate_questions(
            current_knowledge="knowledge",
            query="my query",
            questions_per_iteration=3,
            questions_by_iteration={},
        )
        assert result == ["my query"]


class TestBaseFindingsRepository:
    def test_init_sets_model_and_findings(self):
        model = Mock()
        repo = ConcreteFindingsRepository(model=model)
        assert repo.model is model
        assert repo.findings == {}
