"""Factory for creating configured SummarizationService instances."""

from noteflow.application.services.summarization_service import (
    SummarizationMode,
    SummarizationService,
    SummarizationServiceSettings,
)
from noteflow.infrastructure.logging import get_logger
from noteflow.infrastructure.summarization.citation_verifier import SegmentCitationVerifier
from noteflow.infrastructure.summarization.mock_provider import MockSummarizer
from noteflow.infrastructure.summarization.ollama_provider import OllamaSummarizer

logger = get_logger(__name__)


def create_summarization_service(
    default_mode: SummarizationMode = SummarizationMode.LOCAL,
    include_local: bool = True,
    include_mock: bool = True,
    verify_citations: bool = True,
    filter_invalid_citations: bool = True,
) -> SummarizationService:
    """Create a fully-configured SummarizationService.

    Auto-detects provider availability. Falls back to MOCK if LOCAL unavailable.

    Args:
        default_mode: Preferred summarization mode.
        include_local: Register OllamaSummarizer (checked at runtime).
        include_mock: Register MockSummarizer (always available).
        verify_citations: Enable citation verification.
        filter_invalid_citations: Remove invalid citations from output.

    Returns:
        Configured SummarizationService ready for use.
    """
    service = SummarizationService(
        settings=SummarizationServiceSettings(
            default_mode=default_mode,
            fallback_to_local=True,  # Enables LOCAL → MOCK fallback
            verify_citations=verify_citations,
            filter_invalid_citations=filter_invalid_citations,
        ),
    )

    # Always register MOCK as fallback
    if include_mock:
        service.register_provider(SummarizationMode.MOCK, MockSummarizer())
        logger.debug("Registered MOCK summarization provider")

    # Register LOCAL (Ollama) - availability checked at runtime
    if include_local:
        ollama = OllamaSummarizer()
        service.register_provider(SummarizationMode.LOCAL, ollama)
        if ollama.is_available:
            logger.info("Registered LOCAL (Ollama) summarization provider - available")
        else:
            logger.info(
                "Registered LOCAL (Ollama) summarization provider - unavailable, will fallback"
            )

    # Set citation verifier
    if verify_citations:
        service.set_verifier(SegmentCitationVerifier())
        logger.debug("Citation verification enabled")

    return service
