import importlib.util
from pathlib import Path
from types import SimpleNamespace


VOICE_MODULE_PATH = Path(__file__).resolve().parents[1] / "services" / "ai_orchestrator_service" / "voice.py"
VOICE_SPEC = importlib.util.spec_from_file_location("voice_start_policy_under_test", VOICE_MODULE_PATH)
assert VOICE_SPEC is not None and VOICE_SPEC.loader is not None
voice_policy = importlib.util.module_from_spec(VOICE_SPEC)
VOICE_SPEC.loader.exec_module(voice_policy)


def test_voice_start_name_outcome_obtains_clear_name():
    status, value, source = voice_policy._voice_start_name_outcome("Меня зовут Айдос", "ru")

    assert status == "name_obtained"
    assert value == "Айдос"
    assert source == "voice_start"


def test_voice_start_name_outcome_marks_low_signal_as_not_obtained():
    status, value, source = voice_policy._voice_start_name_outcome("Алло", "ru")

    assert status == "name_not_obtained"
    assert value is None
    assert source == "none"


def test_voice_start_name_outcome_accepts_explicit_name_inside_mixed_request():
    status, value, source = voice_policy._voice_start_name_outcome(
        "Меня зовут Айдос, хотел узнать тариф",
        "ru",
    )

    assert status == "name_obtained"
    assert value == "Айдос"
    assert source == "voice_start"


def test_voice_start_name_outcome_rejects_garbage_name_inside_mixed_request():
    status, value, source = voice_policy._voice_start_name_outcome(
        "Меня зовут Ания Хачу узнать график работы",
        "ru",
    )

    assert status == "name_not_obtained"
    assert value is None
    assert source == "none"


def test_normalize_name_candidate_drops_trailing_asr_initial():
    assert voice_policy._normalize_name_candidate("\u0415\u0440\u043d\u0443\u0440 \u0418") == "\u0415\u0440\u043d\u0443\u0440"


def test_display_name_looks_trusted_rejects_phone_and_accepts_real_name():
    customer = SimpleNamespace(display_name="+77010000001")
    assert voice_policy._display_name_looks_trusted(customer, "+77010000001", None) is False

    named_customer = SimpleNamespace(display_name="Алия")
    assert voice_policy._display_name_looks_trusted(named_customer, "+77010000001", None) is True
