from __future__ import annotations

import base64

from app.domain.contas.client import TimApiClient


def _real(monkeypatch) -> TimApiClient:
    monkeypatch.setenv("TIM_USE_MOCK_GATEWAY", "false")
    monkeypatch.setenv("TIM_GATEWAY_MODE", "real")
    return TimApiClient()


def test_query_vas_legacy_base_url_appends_country_code_and_msisdn(monkeypatch):
    c = _real(monkeypatch)
    monkeypatch.setenv("TIM_URL_CONSULTA_VAS", "http://tim/access/v1")
    calls = []
    monkeypatch.setattr(c, "request", lambda *a, **kw: calls.append((a, kw)) or {})
    c.consultar_vas("11999999999")
    assert calls[0][0] == ("GET", "http://tim/access/v1/5511999999999")


def test_query_vas_accepts_original_aliases_and_timeout(monkeypatch):
    c = _real(monkeypatch)
    monkeypatch.delenv("TIM_URL_CONSULTA_VAS", raising=False)
    monkeypatch.setenv("TIM_CONSULTA_URL", "http://tim/query")
    monkeypatch.setenv("TIM_QUERY_AUTH", "Basic old")
    monkeypatch.setenv("TIM_QUERY_TIMEOUT", "44")
    calls = []
    monkeypatch.setattr(c, "request", lambda *a, **kw: calls.append((a, kw)) or {})
    c.consultar_vas("5511999999999")
    _, kw = calls[0]
    assert kw["headers"]["Authorization"] == "Basic old"
    assert kw["timeout"] == 44


def test_cancelamento_accepts_original_cancellation_aliases(monkeypatch):
    c = _real(monkeypatch)
    monkeypatch.delenv("TIM_CANCELAMENTO_URL", raising=False)
    monkeypatch.setenv("TIM_CANCELLATION_URL", "http://tim/cancel")
    monkeypatch.setenv("TIM_CANCELLATION_AUTH", "Basic legacy")
    monkeypatch.setenv("TIM_CANCELLATION_AUTH_OAM", "OAM")
    monkeypatch.setenv("TIM_CANCELLATION_CN_FIELD", "CN")
    monkeypatch.setenv("TIM_CANCELLATION_TYPE_FIELD", "TYPE")
    calls = []
    monkeypatch.setattr(c, "request", lambda *a, **kw: calls.append((a, kw)) or {})
    c.cancelar_vas("119", {"appId": "1", "cspId": "740"})
    args, kw = calls[0]
    assert args[:2] == ("DELETE", "http://tim/cancel")
    assert kw["headers"]["Authorization"] == "Basic legacy"
    assert kw["headers"]["AuthorizationOAM"] == "OAM"
    assert kw["headers"]["Cn_field"] == "CN"
    assert kw["headers"]["Type_field"] == "TYPE"


def test_profile_full_appends_msisdn_when_original_url_has_no_placeholder(monkeypatch):
    c = _real(monkeypatch)
    monkeypatch.setenv("TIM_PROFILE_FULL_URL", "http://tim/profile")
    calls = []
    monkeypatch.setattr(c, "request", lambda *a, **kw: calls.append((a, kw)) or {})
    c.profile_full("11999999999")
    assert calls[0][0] == ("GET", "http://tim/profile/11999999999")


def test_divergencia_preserves_original_user_password_basic_auth(monkeypatch):
    c = _real(monkeypatch)
    monkeypatch.setenv("TIM_DIVERGENCIA_URL", "http://tim/div")
    monkeypatch.setenv("TIM_DIVERGENCIA_USER", "user")
    monkeypatch.setenv("TIM_DIVERGENCIA_PASSWORD", "pass")
    calls = []
    monkeypatch.setattr(c, "request", lambda *a, **kw: calls.append((a, kw)) or {})
    c.billing_analysis("119")
    auth = calls[0][1]["headers"]["Authorization"]
    assert auth == "Basic " + base64.b64encode(b"user:pass").decode("ascii")


def test_vas_history_and_contrato_default_client_id_match_original(monkeypatch):
    c = _real(monkeypatch)
    monkeypatch.setenv("TIM_VAS_HISTORY_URL", "http://tim/history")
    monkeypatch.setenv("TIM_CONTRATO_URL", "http://tim/contract")
    monkeypatch.delenv("TIM_VAS_HISTORY_CLIENT_ID", raising=False)
    monkeypatch.delenv("TIM_CONTRATO_CLIENT_ID", raising=False)
    calls = []
    monkeypatch.setattr(c, "request", lambda *a, **kw: calls.append((a, kw)) or {})
    c.historico_vas("119")
    c.contrato("119")
    assert calls[0][1]["headers"]["clientId"] == "AIAGENTCR"
    assert calls[1][1]["headers"]["clientId"] == "AIAGENTCR"


def test_profile_bill_uses_complete_invoices_contract_like_original_factory(monkeypatch):
    c = _real(monkeypatch)
    monkeypatch.setenv("TIM_COMPLETE_INVOICES_URL", "http://tim/complete")
    monkeypatch.setenv("TIM_URL_PERFIL_FATURA", "http://tim/legacy-profile")
    monkeypatch.setenv("TIM_COMPLETE_INVOICES_CLIENT_ID", "CID")
    calls = []
    monkeypatch.setattr(c, "request", lambda *a, **kw: calls.append((a, kw)) or {})
    c.profile_bill("119")
    args, kw = calls[0]
    assert args[:2] == ("POST", "http://tim/complete")
    assert kw["headers"]["ClientID"] == "CID"
