"""Tests for mock summarization provider.

Uses shared helpers from tests/infrastructure/summarization/conftest.py:
- create_test_segment: Creates Segment instances for testing
"""

from __future__ import annotations

import pytest

from noteflow.domain.summarization import SummarizationRequest
from noteflow.domain.value_objects import MeetingId
from noteflow.infrastructure.summarization import MockSummarizer

from .conftest import create_test_segment


class TestMockSummarizer:
    """Tests for MockSummarizer."""

    @pytest.fixture
    def summarizer(self) -> MockSummarizer:
        """Create MockSummarizer instance."""
        return MockSummarizer(latency_ms=0.0)

    def test_mock_provider_name(self, summarizer: MockSummarizer) -> None:
        """Provider name should be 'mock'."""
        assert summarizer.provider_name == "mock", "provider_name should be 'mock'"

    def test_is_available(self, summarizer: MockSummarizer) -> None:
        """Mock provider should always be available."""
        assert summarizer.is_available is True, "mock provider should always be available"

    def test_requires_cloud_consent(self, summarizer: MockSummarizer) -> None:
        """Mock provider should not require cloud consent."""
        assert summarizer.requires_cloud_consent is False, "mock provider should not require consent"

    @pytest.mark.asyncio
    async def test_mock_summarize_returns_result(
        self,
        summarizer: MockSummarizer,
        meeting_id: MeetingId,
    ) -> None:
        """Summarize should return a SummarizationResult."""
        segments = [
            create_test_segment(0, "First segment text.", 0.0, 5.0),
            create_test_segment(1, "Second segment text.", 5.0, 10.0),
        ]
        request = SummarizationRequest(meeting_id=meeting_id, segments=segments)

        result = await summarizer.summarize(request)

        assert result.provider_name == "mock", "result should have mock provider_name"
        assert result.model_name == "mock-1.0", "result should have mock-1.0 model_name"
        assert result.summary.meeting_id == meeting_id, "summary meeting_id should match request"

    @pytest.mark.asyncio
    async def test_summarize_generates_executive_summary(
        self,
        summarizer: MockSummarizer,
        meeting_id: MeetingId,
    ) -> None:
        """Summarize should generate executive summary with segment count."""
        segments = [
            create_test_segment(0, "Hello", 0.0, 5.0),
            create_test_segment(1, "World", 5.0, 10.0),
            create_test_segment(2, "Test", 10.0, 15.0),
        ]
        request = SummarizationRequest(meeting_id=meeting_id, segments=segments)

        result = await summarizer.summarize(request)

        assert "3 segments" in result.summary.executive_summary, "executive_summary should mention segment count"
        assert "15.0 seconds" in result.summary.executive_summary, "executive_summary should mention duration"

    @pytest.mark.asyncio
    async def test_summarize_generates_key_points_with_citations(
        self,
        summarizer: MockSummarizer,
        meeting_id: MeetingId,
    ) -> None:
        """Key points should have valid segment_id citations."""
        segments = [
            create_test_segment(0, "First point", 0.0, 5.0),
            create_test_segment(1, "Second point", 5.0, 10.0),
        ]
        request = SummarizationRequest(meeting_id=meeting_id, segments=segments)

        result = await summarizer.summarize(request)

        assert len(result.summary.key_points) == 2, "should generate 2 key points for 2 segments"
        assert result.summary.key_points[0].segment_ids == [0], "first key point should cite segment 0"
        assert result.summary.key_points[1].segment_ids == [1], "second key point should cite segment 1"

    @pytest.mark.asyncio
    async def test_summarize_respects_max_key_points(
        self,
        summarizer: MockSummarizer,
        meeting_id: MeetingId,
    ) -> None:
        """Key points should be limited to max_key_points."""
        segments = [create_test_segment(i, f"Segment {i}", i * 5.0, (i + 1) * 5.0) for i in range(10)]
        request = SummarizationRequest(
            meeting_id=meeting_id,
            segments=segments,
            max_key_points=3,
        )

        result = await summarizer.summarize(request)

        assert len(result.summary.key_points) == 3, "should limit key points to max_key_points"

    @pytest.mark.asyncio
    async def test_summarize_extracts_action_items(
        self,
        summarizer: MockSummarizer,
        meeting_id: MeetingId,
    ) -> None:
        """Action items should be extracted from segments with action keywords."""
        segments = [
            create_test_segment(0, "General discussion", 0.0, 5.0),
            create_test_segment(1, "We need to fix the bug", 5.0, 10.0),
            create_test_segment(2, "TODO: Review the code", 10.0, 15.0),
            create_test_segment(3, "The meeting went well", 15.0, 20.0),
        ]
        request = SummarizationRequest(meeting_id=meeting_id, segments=segments)

        result = await summarizer.summarize(request)

        assert len(result.summary.action_items) == 2, "should extract 2 action items"
        assert result.summary.action_items[0].segment_ids == [1], "first action should cite segment 1"
        assert result.summary.action_items[1].segment_ids == [2], "second action should cite segment 2"

    @pytest.mark.asyncio
    async def test_summarize_respects_max_action_items(
        self,
        summarizer: MockSummarizer,
        meeting_id: MeetingId,
    ) -> None:
        """Action items should be limited to max_action_items."""
        segments = [create_test_segment(i, f"TODO: task {i}", i * 5.0, (i + 1) * 5.0) for i in range(10)]
        request = SummarizationRequest(
            meeting_id=meeting_id,
            segments=segments,
            max_action_items=2,
        )

        result = await summarizer.summarize(request)

        assert len(result.summary.action_items) == 2, "should limit action items to max_action_items"

    @pytest.mark.asyncio
    async def test_summarize_sets_generated_at(
        self,
        summarizer: MockSummarizer,
        meeting_id: MeetingId,
    ) -> None:
        """Summary should have generated_at timestamp."""
        segments = [create_test_segment(0, "Test", 0.0, 5.0)]
        request = SummarizationRequest(meeting_id=meeting_id, segments=segments)

        result = await summarizer.summarize(request)

        assert result.summary.generated_at is not None, "summary should have generated_at timestamp"

    @pytest.mark.asyncio
    async def test_mock_summarize_empty_segments(
        self,
        summarizer: MockSummarizer,
        meeting_id: MeetingId,
    ) -> None:
        """Summarize should handle empty segments list."""
        request = SummarizationRequest(meeting_id=meeting_id, segments=[])

        result = await summarizer.summarize(request)

        assert result.summary.key_points == [], "empty segments should yield no key points"
        assert result.summary.action_items == [], "empty segments should yield no action items"
        assert "0 segments" in result.summary.executive_summary, "should indicate 0 segments"
