from pathlib import Path

from armorer.security.conversation_guard import ConversationGuard
from armorer.tools.orchestrator import (
    disable_guard_alert_type,
    enable_guard_alert_type,
    get_guard_alert_settings,
    get_proactive_security_settings,
    reset_guard_alert_preferences,
    set_proactive_security_admin_chat_ids,
    set_proactive_security_admin_user_ids,
    set_proactive_security_alert_chat,
    set_proactive_security_allow_unconfigured_admins,
    set_proactive_security_enabled,
    set_guard_alert_verbosity,
)


def test_guard_alert_tools_use_user_scoped_file(tmp_path: Path, monkeypatch):
    monkeypatch.setattr(Path, "home", lambda: tmp_path)

    settings = get_guard_alert_settings()
    assert settings["ok"] is True
    assert str(tmp_path / ".armorer" / "guard_settings.json") == settings["settings_file"]


def test_guard_alert_tools_modify_and_reset_settings(tmp_path: Path, monkeypatch):
    monkeypatch.setattr(Path, "home", lambda: tmp_path)

    less = set_guard_alert_verbosity("prompt_injection", step=0.2)
    assert "confidence >=" in less["message"]
    disabled = disable_guard_alert_type("data_exfiltration")
    assert "data exfiltration" in disabled["message"].lower()

    settings = ConversationGuard(config_dir=tmp_path / ".armorer").get_guard_settings()
    assert float(settings["alert_flag_thresholds"]["prompt_injection"]) >= 0.2
    assert "data_exfiltration" in settings["disabled_alert_flags"]

    enabled = enable_guard_alert_type("data_exfiltration")
    assert "Enabled proactive-security alerts" in enabled["message"]
    settings = ConversationGuard(config_dir=tmp_path / ".armorer").get_guard_settings()
    assert "data_exfiltration" not in settings["disabled_alert_flags"]

    reset = reset_guard_alert_preferences()
    assert reset["message"] == "Reset proactive-security alert preferences to defaults."
    settings = ConversationGuard(config_dir=tmp_path / ".armorer").get_guard_settings()
    assert settings["alert_flag_thresholds"] == {}
    assert settings["disabled_alert_flags"] == []


def test_proactive_security_tools_modify_user_scoped_settings(tmp_path: Path, monkeypatch):
    monkeypatch.setattr(Path, "home", lambda: tmp_path)

    enabled = set_proactive_security_enabled(True)
    assert enabled["proactive_security_enabled"] is True
    assert enabled["proactive_security_prompted"] is True

    alert_chat = set_proactive_security_alert_chat("777")
    assert alert_chat["telegram_alert_chat_id"] == "777"

    admin_chats = set_proactive_security_admin_chat_ids("777, 888")
    assert admin_chats["telegram_admin_chat_ids"] == ["777", "888"]

    admin_users = set_proactive_security_admin_user_ids("111, 222")
    assert admin_users["telegram_admin_user_ids"] == ["111", "222"]

    allow = set_proactive_security_allow_unconfigured_admins(True)
    assert allow["telegram_allow_unconfigured_admins"] is True

    settings = get_proactive_security_settings()
    assert settings["telegram_alert_chat_id"] == "777"
    assert settings["telegram_admin_chat_ids"] == ["777", "888"]
    assert settings["telegram_admin_user_ids"] == ["111", "222"]
    assert settings["telegram_allow_unconfigured_admins"] is True
