"""AGBAgent (v2, routed). Embedding/LLM offline-gestubbt → kein Netzwerk."""
import asyncio

import pytest

import compliance.services.specialist_agents.agb._pipeline as pipeline
from compliance.services.checkers.base import CheckResult
from compliance.services.specialist_agents import REGISTRY, AgentInput


class _Stub:
    def __init__(self, present):
        self._p = present

    async def check(self, ctrl, doc):
        return CheckResult(present=self._p)


@pytest.fixture(autouse=True)
def _offline(monkeypatch):
    monkeypatch.setattr(pipeline, "_EMB", _Stub(None))
    monkeypatch.setattr(pipeline, "_LLM", _Stub(None))


def _run(text: str):
    return asyncio.run(
        REGISTRY.get("agb").evaluate(AgentInput(doc_type="agb", text=text)))


def test_agb_agent_registered():
    assert REGISTRY.get("agb") is not None


def test_agb_detects_core_clauses():
    text = (
        "Allgemeine Geschaeftsbedingungen. Geltungsbereich: Diese AGB gelten "
        "fuer alle Vertraege. Vertragsschluss durch Bestellung. Preise inkl. "
        "MwSt. Lieferung. Zahlung. Widerrufsrecht. Gewaehrleistung. Haftung. "
        "Gerichtsstand Muenchen. ") * 4
    out = _run(text)
    assert out.agent == "agb"
    assert out.mc_total >= 1
    ok = [c.label for c in out.mc_coverage if c.status == "ok"]
    assert any("Geltungsbereich" in lbl for lbl in ok)
    # Titel/Maßnahme kurz (ChecklistAgent-Vertrag)
    assert all(len(f.action) < 110 for f in out.findings)


def test_agb_short_text_skips():
    out = _run("zu kurz")
    assert out.confidence == 0.0
    assert all(c.status == "skipped" for c in out.mc_coverage)
