from __future__ import annotations

from types import SimpleNamespace

from adapter.telegram.converter import format_outgoing, from_message
from core.protocol import OutgoingMessage, OutgoingUI


def make_message(*, text="hello", thread_id=42, user_id=1):
    m = SimpleNamespace()
    m.text = text
    m.caption = None
    m.photo = None
    m.document = None
    m.voice = None
    m.message_thread_id = thread_id
    m.from_user = SimpleNamespace(id=user_id, full_name="Alice")
    return m


def test_from_message_in_topic():
    msg = make_message(thread_id=42, user_id=7)
    result = from_message(msg)
    assert result is not None
    assert result.user_id == "7"
    assert result.chat_id == "42"
    assert result.text == "hello"
    assert result.platform == "telegram"


def test_from_message_in_general_returns_none():
    msg = make_message(thread_id=None)
    assert from_message(msg) is None


def test_from_message_uses_caption_if_no_text():
    msg = make_message(text=None, thread_id=10)
    msg.caption = "caption text"
    result = from_message(msg)
    assert result.text == "caption text"


def test_format_outgoing_message():
    event = OutgoingMessage(chat_id="42", text="response")
    assert format_outgoing(event) == "response"


def test_format_outgoing_ui():
    event = OutgoingUI(chat_id="42", text="choose")
    assert format_outgoing(event) == "choose"
