"""Tests for send_message_to_user session handling."""

from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock

import pytest

from astrbot.core.astr_agent_runtime.tools.message_tools import SendMessageToUserTool
from astrbot.core.message.message_event_result import MessageEventResult
from astrbot.core.pipeline.respond.stage import RespondStage
from astrbot.core.platform.astr_message_event import AstrMessageEvent


def _make_context(
    current_session="feishu:GroupMessage:oc_xxx",
    role="admin",
    require_admin=True,
    runtime="local",
    local_permissions=None,
):
    """Build a minimal ContextWrapper for SendMessageToUserTool."""
    cfg = {
        "provider_settings": {
            "computer_use_require_admin": require_admin,
            "computer_use_runtime": runtime,
        }
    }
    if local_permissions is not None:
        cfg["provider_settings"]["computer_use_local_permissions"] = local_permissions
    extras = {}
    event = SimpleNamespace(
        unified_msg_origin=current_session,
        role=role,
        _has_send_oper=False,
        get_sender_id=lambda: "user-1",
        trace=MagicMock(),
    )
    event.set_extra = lambda key, value: extras.__setitem__(key, value)
    event.get_extra = lambda key, default=None: extras.get(key, default)
    event._outline_chain = lambda chain: AstrMessageEvent._outline_chain(event, chain)
    return SimpleNamespace(
        context=SimpleNamespace(
            event=event,
            send_to_user=True,
            _output_turn=None,
            context=SimpleNamespace(
                get_config=lambda umo: cfg,
                send_message=AsyncMock(),
            ),
        )
    )


class _DummyRespondEvent:
    def __init__(self, result_text: str, sent_plain_texts: list[str]) -> None:
        self._extras = {
            "_send_message_to_user_current_session_plain_texts": sent_plain_texts,
        }
        self._result = MessageEventResult().message(result_text)
        self.send = AsyncMock()
        self.trace = MagicMock()
        self.plugins_name = []

    def get_result(self):
        """Return the current message result."""
        return self._result

    def set_extra(self, key, value) -> None:
        """Set pipeline extra data."""
        self._extras[key] = value

    def get_extra(self, key, default=None):
        """Get pipeline extra data."""
        return self._extras.get(key, default)

    def get_sender_name(self) -> str:
        """Return a sender name for respond-stage logging."""
        return "tester"

    def get_sender_id(self) -> str:
        """Return a sender ID for respond-stage logging."""
        return "user-1"

    def get_platform_id(self) -> str:
        """Return a platform ID for respond-stage logging."""
        return "test"

    def get_platform_name(self) -> str:
        """Return a platform name for segmented-reply checks."""
        return "test"

    def _outline_chain(self, chain) -> str:
        """Return a readable outline for respond-stage logging."""
        return " ".join(comp.text for comp in chain if hasattr(comp, "text"))

    def is_stopped(self) -> bool:
        """Return whether this dummy event has stopped."""
        return False

    def clear_result(self) -> None:
        """Clear the current message result."""
        self._result = None


def _make_respond_stage() -> RespondStage:
    """Build a minimally initialized RespondStage for unit tests."""
    stage = RespondStage()
    stage.config = {"provider_settings": {}}
    stage.platform_settings = {"path_mapping": []}
    stage.enable_seg = False
    return stage


@pytest.mark.asyncio
async def test_send_message_with_full_three_part_session():
    """LLM passes a complete three-part session string."""
    tool = SendMessageToUserTool()
    ctx = _make_context(current_session="feishu:GroupMessage:oc_aaa")
    result = await tool.call(
        ctx,
        messages=[{"type": "plain", "text": "hello"}],
        session="feishu:GroupMessage:oc_aaa",
    )
    assert "Message sent to session" in result


@pytest.mark.asyncio
async def test_send_message_with_partial_session_id_fallback():
    """LLM passes only session_id (no colons) — fallback to current_session's prefix."""
    tool = SendMessageToUserTool()
    ctx = _make_context(current_session="feishu:GroupMessage:oc_abc")
    result = await tool.call(
        ctx,
        messages=[{"type": "plain", "text": "hello"}],
        session="oc_abc",
    )
    assert "Message sent to session" in result
    # Verify the target session was reconstructed with current_session's platform/msg_type
    call_args = ctx.context.context.send_message.call_args
    target_session = call_args[0][0]
    assert target_session.platform_id == "feishu"
    assert target_session.message_type.value == "GroupMessage"
    assert target_session.session_id == "oc_abc"


@pytest.mark.asyncio
@pytest.mark.parametrize("send_to_user", [False, True])
async def test_send_message_defaults_to_current_session(send_to_user):
    """LLM does not pass session — uses current_session directly."""
    tool = SendMessageToUserTool()
    ctx = _make_context(current_session="feishu:GroupMessage:oc_xxx")
    ctx.context.send_to_user = send_to_user
    ctx.context._output_turn = SimpleNamespace(done=False, emit=MagicMock())
    result = await tool.call(
        ctx,
        messages=[{"type": "plain", "text": "hello"}],
    )
    output = ctx.context._output_turn.emit.call_args.args[0]
    assert output.target_umo == "feishu:GroupMessage:oc_xxx"
    assert output.content[0].text == "hello"
    if not send_to_user:
        assert "It has not been sent" in result
        ctx.context.context.send_message.assert_not_awaited()
        assert ctx.context.event._has_send_oper is False
        return
    assert "Message sent to session" in result
    call_args = ctx.context.context.send_message.call_args
    target_session = call_args[0][0]
    assert str(target_session) == "feishu:GroupMessage:oc_xxx"
    assert ctx.context.event._has_send_oper is True
    ctx.context.event.trace.record.assert_not_called()
    assert ctx.context.event.get_extra(
        "_send_message_to_user_current_session_plain_texts",
    ) == ["hello"]


@pytest.mark.asyncio
async def test_send_message_returns_platform_error_to_tool_result():
    """Platform send failures are returned to the agent as tool errors."""
    tool = SendMessageToUserTool()
    ctx = _make_context(current_session="qq_official:GroupMessage:group-1")
    ctx.context.context.send_message.side_effect = RuntimeError(
        "413 Request Entity Too Large"
    )

    result = await tool.call(
        ctx,
        messages=[{"type": "plain", "text": "hello"}],
    )

    assert result == (
        "error: failed to send message to session "
        "qq_official:GroupMessage:group-1: 413 Request Entity Too Large"
    )
    assert ctx.context.event._has_send_oper is False
    ctx.context.event.trace.record.assert_not_called()


@pytest.mark.asyncio
async def test_send_message_missing_platform_does_not_record_delivery():
    """Keep missing-platform failures out of delivered-response traces."""
    ctx = _make_context()
    ctx.context.context.send_message.return_value = False
    result = await SendMessageToUserTool().call(
        ctx, messages=[{"type": "plain", "text": "hello"}]
    )
    assert result.startswith("error: failed to find platform")
    ctx.context.event.trace.record.assert_not_called()
    assert ctx.context.event._has_send_oper is False


@pytest.mark.asyncio
async def test_send_message_other_session_does_not_record_current_text():
    """Messages sent to another session do not affect current-session dedupe."""
    tool = SendMessageToUserTool()
    ctx = _make_context(current_session="feishu:GroupMessage:oc_xxx")
    result = await tool.call(
        ctx,
        messages=[{"type": "plain", "text": "hello"}],
        session="feishu:GroupMessage:oc_other",
    )
    assert "Message sent to session" in result
    assert ctx.context.event._has_send_oper is False
    ctx.context.event.trace.record.assert_not_called()
    assert (
        ctx.context.event.get_extra(
            "_send_message_to_user_current_session_plain_texts",
        )
        is None
    )


@pytest.mark.asyncio
async def test_respond_stage_skips_same_text_after_send_message_to_user():
    """RespondStage skips only when the tool already sent the same text."""
    stage = _make_respond_stage()
    event = _DummyRespondEvent(
        result_text="duplicate reply",
        sent_plain_texts=["duplicate reply"],
    )

    result = await stage.process(event)

    assert result is None
    event.send.assert_not_awaited()


@pytest.mark.asyncio
async def test_respond_stage_sends_different_text_after_send_message_to_user():
    """RespondStage still sends a distinct completion after the tool call."""
    stage = _make_respond_stage()
    event = _DummyRespondEvent(
        result_text="I have sent the message with the tool.",
        sent_plain_texts=["duplicate reply"],
    )

    result = await stage.process(event)

    assert result is None
    event.send.assert_awaited_once()
    event.trace.record.assert_not_called()
    assert event.get_result() is None


@pytest.mark.asyncio
async def test_send_message_partial_session_falls_back_to_current():
    """LLM passes session_id matching current_session's id — same session, just incomplete format."""
    tool = SendMessageToUserTool()
    ctx = _make_context(current_session="qq_official:GroupMessage:g123")
    result = await tool.call(
        ctx,
        messages=[{"type": "plain", "text": "world"}],
        session="g123",
    )
    assert "Message sent to session" in result
    call_args = ctx.context.context.send_message.call_args
    target_session = call_args[0][0]
    assert target_session.platform_id == "qq_official"
    assert target_session.message_type.value == "GroupMessage"
    assert target_session.session_id == "g123"


@pytest.mark.asyncio
async def test_cron_context_current_session_is_target_session():
    """Resolve delivery against the cron job's target session.

    AgentWakeupEvent.session comes from the cron payload's target session,
    independently of the absence of an incoming user message.
    """
    tool = SendMessageToUserTool()
    # cron 任务的目标 session(用户配置的完整三段式)
    cron_target_session = "feishu:GroupMessage:oc_cron_target"
    ctx = _make_context(current_session=cron_target_session)

    # LLM 在 cron 上下文中只传了 session_id 部分
    result = await tool.call(
        ctx,
        messages=[{"type": "plain", "text": "cron message"}],
        session="oc_cron_target",
    )
    assert "Message sent to session" in result
    call_args = ctx.context.context.send_message.call_args
    target_session = call_args[0][0]
    # 补全后的 session 应与 cron 目标 session 完全一致
    assert str(target_session) == cron_target_session
    assert target_session.platform_id == "feishu"
    assert target_session.message_type.value == "GroupMessage"
    assert target_session.session_id == "oc_cron_target"


@pytest.mark.asyncio
async def test_send_message_empty_messages_returns_error():
    """Empty or missing messages returns error before session resolution."""
    tool = SendMessageToUserTool()
    ctx = _make_context()
    result = await tool.call(ctx, messages=[], session="oc_xxx")
    assert "error:" in result
    assert "messages" in result.lower()


@pytest.mark.asyncio
async def test_send_message_missing_image_path_stops_before_send(tmp_path, monkeypatch):
    """Missing image paths fail before sending any message components."""
    tool = SendMessageToUserTool()
    # Sandbox runtime so the booter is still consulted for missing paths;
    # local runtime now rejects them before any booter call.
    ctx = _make_context(runtime="sandbox")
    missing_image_path = tmp_path / "missing.png"

    async def mock_get_booter(*args, **kwargs):
        del args, kwargs
        raise RuntimeError("sandbox unavailable")

    monkeypatch.setattr(
        "astrbot.core.astr_agent_runtime.tools.message_tools.get_booter",
        mock_get_booter,
    )

    result = await tool.call(
        ctx,
        messages=[
            {"type": "plain", "text": "before image"},
            {"type": "image", "path": str(missing_image_path)},
        ],
    )

    assert "error: failed to build messages[1] component: sandbox unavailable" in result
    ctx.context.context.send_message.assert_not_called()


@pytest.mark.asyncio
@pytest.mark.parametrize("component_type", ["file", "image", "record", "video"])
@pytest.mark.parametrize(
    ("role", "scope"),
    [
        ("member", None),
        ("member", "none"),
        ("admin", "none"),
        ("member", "workspace"),
        ("admin", "workspace"),
    ],
)
async def test_restricted_role_cannot_send_arbitrary_local_absolute_file(
    tmp_path, component_type, role, scope
):
    """Only explicit host access permits sending files outside trusted roots."""
    tool = SendMessageToUserTool()
    ctx = _make_context(
        role=role,
        local_permissions={role: {"filesystem_scope": scope, "allow_execution": False}}
        if scope
        else None,
    )
    secret_path = tmp_path / "secret.txt"
    secret_path.write_text("secret", encoding="utf-8")

    result = await tool.call(
        ctx,
        messages=[{"type": component_type, "path": str(secret_path)}],
    )

    assert "error: Local file send is restricted for this user" in result
    assert str(secret_path) in result
    ctx.context.context.send_message.assert_not_called()


@pytest.mark.asyncio
async def test_member_with_host_scope_can_send_local_absolute_file(tmp_path):
    """Host filesystem scope should cover the local file-send bridge."""
    permissions = {
        "member": {
            "allow_execution": False,
            "allow_network": False,
            "filesystem_scope": "host",
        }
    }
    tool = SendMessageToUserTool()
    ctx = _make_context(role="member", local_permissions=permissions)
    file_path = tmp_path / "report.html"
    file_path.write_text("result", encoding="utf-8")

    result = await tool.call(
        ctx,
        messages=[
            {"type": "file", "path": str(file_path), "text": "Open in a browser."}
        ],
    )

    assert "Message sent to session" in result
    caption, sent_file = ctx.context.context.send_message.await_args.args[1].chain
    assert caption.text == "Open in a browser."
    assert (await sent_file.to_dict())["data"]["name"] == file_path.name


@pytest.mark.asyncio
async def test_send_file_url_with_separate_caption():
    ctx = _make_context()
    url = "https://example.com/files/test%20report.html?download=1#preview"

    await SendMessageToUserTool().call(
        ctx,
        messages=[
            {"type": "plain", "text": "Open in a browser."},
            {"type": "file", "url": url},
        ],
    )

    caption, sent_file = ctx.context.context.send_message.await_args.args[1].chain
    assert caption.text == "Open in a browser."
    assert (await sent_file.to_dict())["data"] == {
        "file": url,
        "name": "test report.html",
    }


@pytest.mark.asyncio
@pytest.mark.parametrize("scope", ["none", "workspace"])
async def test_non_admin_can_send_workspace_file(tmp_path, monkeypatch, scope):
    """Non-admin users can send files inside their per-session workspace."""
    tool = SendMessageToUserTool()
    ctx = _make_context(
        current_session="feishu:GroupMessage:oc_workspace",
        role="member",
        require_admin=True,
        local_permissions={"member": {"filesystem_scope": scope}},
    )
    workspace_root = tmp_path / "workspaces"
    workspace_file = workspace_root / "feishu_GroupMessage_oc_workspace" / "result.txt"
    workspace_file.parent.mkdir(parents=True)
    workspace_file.write_text("result", encoding="utf-8")
    monkeypatch.setattr(
        "astrbot.core.astr_agent_runtime.tools.computer_tools.util.get_astrbot_workspaces_path",
        lambda: str(workspace_root),
    )

    result = await tool.call(
        ctx,
        messages=[{"type": "file", "path": "result.txt"}],
    )

    assert "Message sent to session" in result
    ctx.context.context.send_message.assert_called_once()


@pytest.mark.asyncio
@pytest.mark.parametrize("scope", ["none", "workspace"])
async def test_non_admin_can_send_temp_file(tmp_path, monkeypatch, scope):
    """Non-admin users can send generated files under AstrBot temp."""
    tool = SendMessageToUserTool()
    ctx = _make_context(
        role="member", local_permissions={"member": {"filesystem_scope": scope}}
    )
    temp_root = tmp_path / "temp"
    temp_root.mkdir()
    output_path = temp_root / "output.txt"
    output_path.write_text("output", encoding="utf-8")
    monkeypatch.setattr(
        "astrbot.core.astr_agent_runtime.tools.message_tools.get_astrbot_temp_path",
        lambda: str(temp_root),
    )

    result = await tool.call(
        ctx,
        messages=[{"type": "file", "path": str(output_path)}],
    )

    assert "Message sent to session" in result
    ctx.context.context.send_message.assert_called_once()


@pytest.mark.asyncio
async def test_send_message_downloads_windows_sandbox_file_with_original_name(
    tmp_path, monkeypatch
):
    """Windows sandbox paths keep their basename when sent as files."""
    tool = SendMessageToUserTool()
    ctx = _make_context(runtime="sandbox")
    temp_root = tmp_path / "temp"
    temp_root.mkdir()
    monkeypatch.setattr(
        "astrbot.core.astr_agent_runtime.tools.message_tools.get_astrbot_temp_path",
        lambda: str(temp_root),
    )

    async def _exec(_command):
        return {"content": "_&exists_"}

    async def _download_file(_remote_path, local_path):
        # The local temp path keeps the original remote basename; separators
        # are platform-native, so only the basename is asserted here.
        assert local_path.endswith("report.txt")
        with open(local_path, "w", encoding="utf-8") as file:
            file.write("report")

    booter = SimpleNamespace(
        shell=SimpleNamespace(exec=AsyncMock(side_effect=_exec)),
        download_file=AsyncMock(side_effect=_download_file),
    )

    async def mock_get_booter(*args, **kwargs):
        del args, kwargs
        return booter

    monkeypatch.setattr(
        "astrbot.core.astr_agent_runtime.tools.message_tools.get_booter",
        mock_get_booter,
    )

    result = await tool.call(
        ctx,
        messages=[{"type": "file", "path": r"C:\Users\AstrBot\report.txt"}],
    )

    assert "Message sent to session" in result
    sent_chain = ctx.context.context.send_message.await_args.args[1]
    sent_file = sent_chain.chain[0]
    assert sent_file.name == "report.txt"


@pytest.mark.asyncio
async def test_send_message_downloads_trailing_slash_sandbox_file_with_basename(
    tmp_path, monkeypatch
):
    tool = SendMessageToUserTool()
    ctx = _make_context(runtime="sandbox")
    temp_root = tmp_path / "temp"
    temp_root.mkdir()
    monkeypatch.setattr(
        "astrbot.core.astr_agent_runtime.tools.message_tools.get_astrbot_temp_path",
        lambda: str(temp_root),
    )

    async def _exec(_command):
        return {"content": "_&exists_"}

    async def _download_file(_remote_path, local_path):
        assert local_path.endswith("export")
        with open(local_path, "w", encoding="utf-8") as file:
            file.write("export")

    booter = SimpleNamespace(
        shell=SimpleNamespace(exec=AsyncMock(side_effect=_exec)),
        download_file=AsyncMock(side_effect=_download_file),
    )

    async def mock_get_booter(*args, **kwargs):
        del args, kwargs
        return booter

    monkeypatch.setattr(
        "astrbot.core.astr_agent_runtime.tools.message_tools.get_booter",
        mock_get_booter,
    )

    result = await tool.call(
        ctx,
        messages=[{"type": "file", "path": "reports/export/"}],
    )

    assert "Message sent to session" in result
    sent_chain = ctx.context.context.send_message.await_args.args[1]
    sent_file = sent_chain.chain[0]
    assert sent_file.name == "export"


@pytest.mark.asyncio
async def test_send_message_local_runtime_skips_sandbox_file_probe(
    tmp_path, monkeypatch
):
    """Local runtime must resolve send-file paths only via permission-checked branches.

    Falling through to the booter branch would probe the host shell without
    the caller's filesystem permissions.
    """
    workspace = tmp_path / "workspace"
    workspace.mkdir()
    secret = tmp_path / "host-only.txt"
    secret.write_text("host-only marker", encoding="utf-8")
    allowed = workspace / "allowed.txt"
    allowed.write_text("workspace file", encoding="utf-8")

    async def mock_workspace_root_for_context(_context):
        return workspace

    async def mock_get_booter(*args, **kwargs):
        del args, kwargs
        raise AssertionError("local runtime must not query a sandbox booter")

    monkeypatch.setattr(
        "astrbot.core.astr_agent_runtime.tools.message_tools.workspace_root_for_context",
        mock_workspace_root_for_context,
    )
    monkeypatch.setattr(
        "astrbot.core.astr_agent_runtime.tools.message_tools.get_booter",
        mock_get_booter,
    )

    tool = SendMessageToUserTool()
    ctx = _make_context(
        role="member",
        local_permissions={
            "member": {
                "allow_execution": False,
                "allow_network": False,
                "filesystem_scope": "workspace",
            }
        },
    )

    resolved, downloaded = await tool._resolve_path_from_sandbox(ctx, "allowed.txt")
    assert resolved == str(allowed.resolve())
    assert downloaded is False

    with pytest.raises(FileNotFoundError):
        await tool._resolve_path_from_sandbox(ctx, "host-only.txt")