from __future__ import annotations

from typing import Any, Dict

from api.v1.modules.a76.general_catalogs.doda.external_service import DodaExternalService


class _FakeResponse:
    def __init__(self, payload: Dict[str, Any]):
        self._payload = payload

    def raise_for_status(self) -> None:
        return None

    def json(self) -> Dict[str, Any]:
        return self._payload


class _FakeClient:
    calls = []

    def __init__(self, *args, **kwargs):
        self.kwargs = kwargs

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc, tb):
        return False

    def post(self, url: str, json: Dict[str, Any]):
        _FakeClient.calls.append(("POST", url, json))
        return _FakeResponse({"task_id": "t-1", "status": "queued", "message": "ok"})

    def get(self, url: str):
        _FakeClient.calls.append(("GET", url, None))
        return _FakeResponse({"task_id": "t-1", "status": "done", "message": "ok"})


def test_external_service_supports_consulta_and_eliminar(monkeypatch):
    from api.v1.modules.a76.general_catalogs.doda import external_service as module

    _FakeClient.calls = []
    monkeypatch.setattr(module, "httpx", module.httpx)
    monkeypatch.setattr(module.httpx, "Client", _FakeClient)

    service = DodaExternalService()
    service.base_url = "http://example.test"

    payload = {"foo": "bar"}
    consulta = service.post_consulta(payload)
    consulta_status = service.get_consulta_status("abc123")
    eliminar = service.post_eliminar(payload)
    eliminar_status = service.get_eliminar_status("abc123")

    assert consulta["task_id"] == "t-1"
    assert consulta_status["status"] == "done"
    assert eliminar["status"] == "queued"
    assert eliminar_status["task_id"] == "t-1"
    assert _FakeClient.calls == [
        ("POST", "http://example.test/api/v1/doda/consulta", payload),
        ("GET", "http://example.test/api/v1/doda/consulta-status/abc123", None),
        ("POST", "http://example.test/api/v1/doda/eliminar", payload),
        ("GET", "http://example.test/api/v1/doda/eliminar-status/abc123", None),
    ]
