"""Dispatcher filtering by config.remit.remit_allowlist (only register in-remit backends)."""

from __future__ import annotations

import pytest

from llmhosts.proxy.dispatcher import BackendDispatcher, BackendEntry


@pytest.mark.unit
async def test_remit_allowlist_empty_all_registered() -> None:
    """When remit_allowlist is empty, all backends are registered."""
    dispatcher = BackendDispatcher(remit_allowlist=[])
    await dispatcher.startup()

    # ollama-local may or may not be present depending on env; register a remote
    dispatcher.register_remote_backend(
        tunnel_url="https://a.example.com",
        name="remote-a",
        models=["m1"],
        device_id="dev-a",
    )
    remote = [b for b in dispatcher._backends if b.name == "remote-a"]
    assert len(remote) == 1

    await dispatcher.shutdown()


@pytest.mark.unit
async def test_remit_allowlist_filters_by_name() -> None:
    """When remit_allowlist is set, only matching name/base_url/device_id is registered."""
    dispatcher = BackendDispatcher(
        remit_allowlist=["remote-a", "https://b.example.com", "dev-c"],
    )
    await dispatcher.startup()

    dispatcher.register_remote_backend(
        tunnel_url="https://a.example.com",
        name="remote-a",
        models=["m1"],
        device_id="dev-a",
    )
    dispatcher.register_remote_backend(
        tunnel_url="https://b.example.com/",
        name="remote-b",
        models=["m2"],
        device_id="dev-b",
    )
    dispatcher.register_remote_backend(
        tunnel_url="https://c.example.com",
        name="remote-c",
        models=["m3"],
        device_id="dev-c",
    )
    dispatcher.register_remote_backend(
        tunnel_url="https://out.example.com",
        name="out",
        models=[],
        device_id="out-id",
    )

    remote = [b for b in dispatcher._backends if b.backend_type == "remote"]
    assert len(remote) == 3  # remote-a (name), remote-b (url), remote-c (device_id)
    names = {b.name for b in remote}
    assert names == {"remote-a", "remote-b", "remote-c"}
    assert not any(b.name == "out" for b in dispatcher._backends)

    await dispatcher.shutdown()


@pytest.mark.unit
async def test_remit_allowlist_register_backend_direct() -> None:
    """register_backend(BackendEntry) also respects remit_allowlist."""
    dispatcher = BackendDispatcher(remit_allowlist=["in-remit"])
    await dispatcher.startup()

    in_entry = BackendEntry(
        name="in-remit",
        base_url="http://localhost:11434",
        backend_type="ollama",
        models=["m1"],
    )
    out_entry = BackendEntry(
        name="out-of-remit",
        base_url="http://192.168.1.10:11434",
        backend_type="ollama",
        models=["m2"],
    )

    dispatcher.register_backend(in_entry)
    dispatcher.register_backend(out_entry)

    names = [b.name for b in dispatcher._backends]
    assert "in-remit" in names
    assert "out-of-remit" not in names

    await dispatcher.shutdown()


@pytest.mark.unit
async def test_remit_allowlist_none_same_as_empty() -> None:
    """BackendDispatcher(remit_allowlist=None) behaves like empty allowlist."""
    dispatcher = BackendDispatcher()
    await dispatcher.startup()
    dispatcher.register_remote_backend(
        tunnel_url="https://any.example.com",
        name="any",
        models=[],
    )
    remote = [b for b in dispatcher._backends if b.name == "any"]
    assert len(remote) == 1
    await dispatcher.shutdown()
