"""Mock summarization provider for testing."""

import time
from datetime import UTC, datetime

from noteflow.domain.entities import ActionItem, KeyPoint, Summary
from noteflow.domain.summarization import (
    SummarizationRequest,
    SummarizationResult,
)


class MockSummarizer:
    """Deterministic mock summarizer for testing.

    Generates predictable summaries based on input segments without
    requiring an actual LLM. Useful for unit tests and development.
    """

    def __init__(self, latency_ms: float = 10.0) -> None:
        """Initialize mock summarizer.

        Args:
            latency_ms: Simulated latency in milliseconds.
        """
        self._latency_ms = latency_ms

    @property
    def provider_name(self) -> str:
        """Provider identifier."""
        return "mock"

    @property
    def is_available(self) -> bool:
        """Mock provider is always available."""
        return True

    @property
    def requires_cloud_consent(self) -> bool:
        """Mock provider does not send data externally."""
        return False

    async def summarize(self, request: SummarizationRequest) -> SummarizationResult:
        """Generate deterministic mock summary.

        Creates key points and action items based on segment content,
        with proper evidence linking to segment_ids.

        Args:
            request: Summarization request with segments.

        Returns:
            SummarizationResult with mock summary.
        """
        start = time.monotonic()

        # Generate executive summary
        segment_count = request.segment_count
        total_duration = request.total_duration
        executive_summary = (
            f"Meeting with {segment_count} segments spanning {total_duration:.1f} seconds."
        )

        # Generate key points from segments (up to max_key_points)
        key_points: list[KeyPoint] = []
        for i, segment in enumerate(request.segments[: request.max_key_points]):
            # Truncate text for key point
            text = f"{segment.text[:100]}..." if len(segment.text) > 100 else segment.text
            key_points.append(
                KeyPoint(
                    text=f"Point {i + 1}: {text}",
                    segment_ids=[segment.segment_id],
                    start_time=segment.start_time,
                    end_time=segment.end_time,
                )
            )

        # Generate action items from segments containing action words
        action_items: list[ActionItem] = []
        action_keywords = {"todo", "action", "will", "should", "must", "need to"}
        for segment in request.segments:
            text_lower = segment.text.lower()
            if any(kw in text_lower for kw in action_keywords):
                if len(action_items) >= request.max_action_items:
                    break
                action_items.append(
                    ActionItem(
                        text=f"Action: {segment.text[:80]}",
                        assignee="",  # Mock doesn't extract assignees
                        segment_ids=[segment.segment_id],
                    )
                )

        summary = Summary(
            meeting_id=request.meeting_id,
            executive_summary=executive_summary,
            key_points=key_points,
            action_items=action_items,
            generated_at=datetime.now(UTC),
            provider_name=self.provider_name,
            model_name="mock-1.0",
        )

        elapsed = (time.monotonic() - start) * 1000 + self._latency_ms

        return SummarizationResult(
            summary=summary,
            model_name="mock-1.0",
            provider_name=self.provider_name,
            tokens_used=None,
            latency_ms=elapsed,
        )
