from __future__ import annotations

from types import SimpleNamespace

from src.services.ollama_endpoint_resolver import (
    resolve_ollama_order,
    resolve_ollama_selection,
)


def _settings(
    *,
    primary: str = "http://192.168.0.110:11435",
    secondary: str = "http://192.168.0.110:11436",
    fallback: str = "http://192.168.0.110:11437",
) -> SimpleNamespace:
    return SimpleNamespace(
        OLLAMA_URL=primary,
        OLLAMA_SECONDARY_URL=secondary,
        OLLAMA_FALLBACK_URL=fallback,
    )


def test_all_workloads_prefer_gcp_a_lane() -> None:
    cfg = _settings()

    for workload in (
        "interactive",
        "healthcheck",
        "alert_fast",
        "batch",
        "embedding",
        "rag",
        "code_review",
        "shadow",
        "canary",
        "deep_rca",
        "image_analysis",
        "hermes",
        "local_required",
        "privacy_sensitive",
        "dr",
    ):
        selection = resolve_ollama_selection(workload, config=cfg)
        assert selection.url == "http://192.168.0.110:11435"
        assert selection.provider_name == "ollama_gcp_a"
        assert selection.reason == "global_primary_gcp_a"


def test_all_workloads_share_global_ollama_order() -> None:
    cfg = _settings()

    for workload in ("interactive", "deep_rca", "local_required", "privacy_sensitive", "dr"):
        order = resolve_ollama_order(workload, config=cfg)
        assert [selection.url for selection in order] == [
            "http://192.168.0.110:11435",
            "http://192.168.0.110:11436",
            "http://192.168.0.110:11437",
        ]
        assert [selection.provider_name for selection in order] == [
            "ollama_gcp_a",
            "ollama_gcp_b",
            "ollama_local",
        ]


def test_non_sensitive_workloads_fall_back_to_gcp_b_when_primary_missing() -> None:
    cfg = _settings(primary="")

    selection = resolve_ollama_selection("embedding", config=cfg)
    assert selection.url == "http://192.168.0.110:11436"
    assert selection.provider_name == "ollama_gcp_b"
    assert selection.reason == "global_secondary_gcp_b"

    order = resolve_ollama_order("embedding", config=cfg)
    assert [selection.provider_name for selection in order] == [
        "ollama_gcp_b",
        "ollama_local",
    ]
