from beaver.foundation.events import ChannelIdentity, OutboundMessage
from beaver.interfaces.channels.platforms.base import (
    chunk_text,
    compact_media_summary,
    config_bool,
    config_list,
    outbound_target,
)


def test_config_helpers_normalize_common_values() -> None:
    assert config_bool({"enabled": "true"}, "enabled", default=False) is True
    assert config_bool({"enabled": "0"}, "enabled", default=True) is False
    assert config_list({"allowFrom": "u1,u2"}, "allowFrom") == ["u1", "u2"]
    assert config_list({"allowFrom": ["u1", 2]}, "allowFrom") == ["u1", "2"]


def test_chunk_text_preserves_order_and_limit() -> None:
    chunks = chunk_text("abcdef", max_chars=2)

    assert chunks == ["ab", "cd", "ef"]


def test_outbound_target_prefers_channel_identity() -> None:
    identity = ChannelIdentity(
        channel_id="telegram-main",
        kind="telegram",
        account_id="bot-main",
        peer_id="chat-1",
        thread_id="topic-1",
        peer_type="group",
        user_id="user-1",
    )
    message = OutboundMessage(
        channel="telegram-main",
        content="ok",
        session_id="ignored",
        finish_reason="stop",
        channel_identity=identity,
    )

    target = outbound_target(message)

    assert target.peer_id == "chat-1"
    assert target.thread_id == "topic-1"
    assert target.peer_type == "group"
    assert target.user_id == "user-1"


def test_outbound_target_falls_back_to_session_id() -> None:
    message = OutboundMessage(
        channel="telegram-main",
        content="ok",
        session_id="telegram-main:bot-main:chat-1:topic-1",
        finish_reason="stop",
    )

    target = outbound_target(message)

    assert target.peer_id == "chat-1"
    assert target.thread_id == "topic-1"


def test_compact_media_summary_mentions_attachment_type() -> None:
    assert compact_media_summary("photo", file_name="cat.png") == "[photo: cat.png]"
    assert compact_media_summary("document") == "[document]"
