from svg_scrapling.config import DiscoveryProvider, FindAssetsConfig
from svg_scrapling.runtime import (
    RuntimeFactories,
    build_default_pipeline_dependencies,
    default_runtime_factories,
    ordered_discovery_providers_for,
)
from svg_scrapling.scraping import FetchOrchestrator, StaticHtmlFetcher
from svg_scrapling.search import CandidatePage, FakeSearchProvider


class FakeFetchTransport:
    def fetch(self, url: str, timeout_seconds: float, headers: dict[str, str]):
        _ = timeout_seconds
        _ = headers
        return 200, "<html><body>ok</body></html>", {}, url


def test_default_runtime_factories_include_live_provider_and_fetch_defaults() -> None:
    config = FindAssetsConfig(query="tiger coloring page")

    dependencies = build_default_pipeline_dependencies(
        config,
        factories=default_runtime_factories(),
    )

    assert dependencies.search_provider.name == "duckduckgo_html->bing_html"
    assert dependencies.fetch_orchestrator is not None


def test_ordered_discovery_providers_respect_preferred_provider_and_disables() -> None:
    config = FindAssetsConfig(query="tiger coloring page")
    brave_first = FindAssetsConfig(
        query="tiger coloring page",
        provider=DiscoveryProvider.BING_HTML,
        disabled_providers=frozenset(),
    )

    assert ordered_discovery_providers_for(config) == (
        config.provider,
        DiscoveryProvider.BING_HTML,
    )
    assert ordered_discovery_providers_for(brave_first) == (
        brave_first.provider,
        DiscoveryProvider.DUCKDUCKGO_HTML,
    )


def test_default_runtime_builder_accepts_test_factories() -> None:
    config = FindAssetsConfig(query="tiger coloring page")

    dependencies = build_default_pipeline_dependencies(
        config,
        factories=RuntimeFactories(
            provider_factory=lambda _config: FakeSearchProvider(
                name="fake-provider",
                pages=(
                    CandidatePage(
                        url="https://example.com/page",
                        query="tiger coloring page",
                        provider_name="fake-provider",
                        rank=1,
                    ),
                ),
            ),
            fetch_orchestrator_factory=lambda _config: FetchOrchestrator(
                static_fetcher=StaticHtmlFetcher(transport=FakeFetchTransport(), retries=0)
            ),
        ),
    )

    assert dependencies.search_provider.name == "fake-provider"
