"""SMS 分派器:按 settings.SMS_PROVIDER 路由到正确 provider。

契约:send_code / verify_code **每次调用**读 settings.SMS_PROVIDER 选 provider(支持运行时切换 /
灰度回退);默认 jiguang。此处 monkeypatch 两 provider 的实现为标记函数,断言路由命中 + 可秒切。
"""
from __future__ import annotations

from app.core.config import settings
from app.integrations import sms
from app.integrations.sms import aliyun, chuanglan, jiguang


def test_send_code_routes_by_provider_and_switches_per_call(monkeypatch) -> None:
    calls: list[str] = []
    monkeypatch.setattr(jiguang, "send_code", lambda phone: (calls.append("jiguang"), 60)[1])
    monkeypatch.setattr(aliyun, "send_code", lambda phone: (calls.append("aliyun"), 60)[1])
    monkeypatch.setattr(chuanglan, "send_code", lambda phone: (calls.append("chuanglan"), 60)[1])

    monkeypatch.setattr(settings, "SMS_PROVIDER", "jiguang")
    assert sms.send_code("13800138000") == 60
    monkeypatch.setattr(settings, "SMS_PROVIDER", "aliyun")
    assert sms.send_code("13800138000") == 60
    monkeypatch.setattr(settings, "SMS_PROVIDER", "chuanglan")
    assert sms.send_code("13800138000") == 60

    assert calls == ["jiguang", "aliyun", "chuanglan"]  # 每次按当前 provider 路由,运行时可切


def test_verify_code_routes_by_provider(monkeypatch) -> None:
    calls: list[str] = []
    monkeypatch.setattr(jiguang, "verify_code", lambda phone, code: (calls.append("jiguang"), True)[1])
    monkeypatch.setattr(aliyun, "verify_code", lambda phone, code: (calls.append("aliyun"), True)[1])
    monkeypatch.setattr(chuanglan, "verify_code", lambda phone, code: (calls.append("chuanglan"), True)[1])

    monkeypatch.setattr(settings, "SMS_PROVIDER", "jiguang")
    assert sms.verify_code("13800138000", "123456") is True
    monkeypatch.setattr(settings, "SMS_PROVIDER", "aliyun")
    assert sms.verify_code("13800138000", "123456") is True
    monkeypatch.setattr(settings, "SMS_PROVIDER", "chuanglan")
    assert sms.verify_code("13800138000", "123456") is True

    assert calls == ["jiguang", "aliyun", "chuanglan"]


def test_unknown_provider_falls_back_to_jiguang(monkeypatch) -> None:
    """SMS_PROVIDER 非 aliyun 一律走 jiguang(默认兜底,防误配把登录打挂)。"""
    calls: list[str] = []
    monkeypatch.setattr(jiguang, "send_code", lambda phone: (calls.append("jiguang"), 60)[1])
    monkeypatch.setattr(aliyun, "send_code", lambda phone: (calls.append("aliyun"), 60)[1])
    monkeypatch.setattr(settings, "SMS_PROVIDER", "jiguang")

    sms.send_code("13800138000")
    assert calls == ["jiguang"]
