from unittest.mock import patch

from armorer.main import _run_armorer_managed_openclaw_setup


@patch("armorer.main._configure_openclaw_instance")
@patch("armorer.main._run_openclaw_telegram_pairing")
@patch("armorer.tools.app_setup.apply_openclaw_config")
@patch("armorer.tools.app_setup.validate_openclaw_selection")
@patch("armorer.tools.app_setup.get_openclaw_setup_context")
@patch("armorer.main._ui_checkbox")
@patch("armorer.main._ui_confirm")
@patch("armorer.main._ui_select")
def test_managed_openclaw_setup_skips_legacy_local_config_wizard(
    mock_ui_select,
    mock_ui_confirm,
    mock_ui_checkbox,
    mock_get_context,
    mock_validate,
    mock_apply,
    mock_pairing,
    mock_configure_openclaw_instance,
):
    mock_get_context.return_value = {
        "ok": True,
        "options": {
            "providers": ["gemini"],
            "provider_default_models": {"gemini": "google/gemini-3.1-flash-lite-preview"},
            "channels": [],
            "skills": [],
        },
        "current_config": {
            "primary_provider": "gemini",
            "default_model": "google/gemini-3.1-flash-lite-preview",
            "enabled_channels": [],
            "enabled_skills": [],
        },
        "credentials": {
            "GEMINI_API_KEY": {"present": True},
        },
        "state_summary": {
            "channel_state": {}
        },
    }
    mock_ui_select.side_effect = ["gemini", "google/gemini-3.1-flash-lite-preview"]
    mock_ui_confirm.side_effect = [True]
    mock_ui_checkbox.return_value = []
    mock_validate.return_value = {"ok": True, "warnings": [], "errors": [], "missing_credentials": []}
    mock_apply.return_value = {
        "ok": True,
        "message": "Applied OpenClaw configuration.",
        "warnings": [],
        "channel_verification": {"statuses": {}, "warnings": []},
    }

    exit_code = _run_armorer_managed_openclaw_setup("openclaw")

    assert exit_code == 0
    mock_configure_openclaw_instance.assert_not_called()
    mock_pairing.assert_not_called()


@patch("armorer.main._run_openclaw_telegram_pairing")
@patch("armorer.tools.app_setup.apply_openclaw_config")
@patch("armorer.tools.app_setup.validate_openclaw_selection")
@patch("armorer.tools.app_setup.get_openclaw_setup_context")
@patch("armorer.main._ui_checkbox")
@patch("armorer.main._ui_confirm")
@patch("armorer.main._ui_select")
def test_managed_openclaw_setup_prompts_pairing_when_telegram_probe_ok_but_not_linked(
    mock_ui_select,
    mock_ui_confirm,
    mock_ui_checkbox,
    mock_get_context,
    mock_validate,
    mock_apply,
    mock_pairing,
):
    mock_get_context.return_value = {
        "ok": True,
        "options": {
            "providers": ["gemini"],
            "provider_default_models": {"gemini": "google/gemini-3.1-flash-lite-preview"},
            "channels": ["telegram", "discord", "slack"],
            "skills": [],
        },
        "current_config": {
            "primary_provider": "gemini",
            "default_model": "google/gemini-3.1-flash-lite-preview",
            "enabled_channels": [],
            "enabled_skills": [],
        },
        "credentials": {
            "GEMINI_API_KEY": {"present": True},
            "TELEGRAM_BOT_TOKEN": {"present": True},
        },
        "state_summary": {
            "channel_state": {
                "telegram": {
                    "enabled": False,
                    "required_keys": ["TELEGRAM_BOT_TOKEN"],
                    "present_keys": ["TELEGRAM_BOT_TOKEN"],
                },
                "discord": {"enabled": False, "required_keys": [], "present_keys": []},
                "slack": {"enabled": False, "required_keys": [], "present_keys": []},
            }
        },
    }
    mock_ui_select.side_effect = ["gemini", "google/gemini-3.1-flash-lite-preview"]
    mock_ui_confirm.side_effect = [True, True, True, False, False]
    mock_ui_checkbox.return_value = []
    mock_validate.return_value = {"ok": True, "warnings": [], "errors": [], "missing_credentials": []}
    mock_apply.return_value = {
        "ok": True,
        "message": "Applied OpenClaw configuration.",
        "warnings": [],
        "channel_verification": {
            "statuses": {
                "telegram": {
                    "configured": True,
                    "verified": True,
                    "linked": False,
                    "detail": "connected, not linked, running",
                }
            },
            "warnings": [],
        },
    }

    exit_code = _run_armorer_managed_openclaw_setup("openclaw")

    assert exit_code == 0
    mock_pairing.assert_called_once_with("openclaw")


@patch("armorer.main._run_openclaw_telegram_pairing")
@patch("armorer.tools.app_setup.apply_openclaw_config")
@patch("armorer.tools.app_setup.validate_openclaw_selection")
@patch("armorer.tools.app_setup.get_openclaw_setup_context")
@patch("armorer.main._ui_checkbox")
@patch("armorer.main._ui_confirm")
@patch("armorer.main._ui_select")
def test_managed_openclaw_setup_skips_pairing_when_telegram_is_verified_and_linked(
    mock_ui_select,
    mock_ui_confirm,
    mock_ui_checkbox,
    mock_get_context,
    mock_validate,
    mock_apply,
    mock_pairing,
):
    mock_get_context.return_value = {
        "ok": True,
        "options": {
            "providers": ["gemini"],
            "provider_default_models": {"gemini": "google/gemini-3.1-flash-lite-preview"},
            "channels": ["telegram", "discord", "slack"],
            "skills": [],
        },
        "current_config": {
            "primary_provider": "gemini",
            "default_model": "google/gemini-3.1-flash-lite-preview",
            "enabled_channels": [],
            "enabled_skills": [],
        },
        "credentials": {
            "GEMINI_API_KEY": {"present": True},
            "TELEGRAM_BOT_TOKEN": {"present": True},
        },
        "state_summary": {
            "channel_state": {
                "telegram": {
                    "enabled": False,
                    "required_keys": ["TELEGRAM_BOT_TOKEN"],
                    "present_keys": ["TELEGRAM_BOT_TOKEN"],
                },
                "discord": {"enabled": False, "required_keys": [], "present_keys": []},
                "slack": {"enabled": False, "required_keys": [], "present_keys": []},
            }
        },
    }
    mock_ui_select.side_effect = ["gemini", "google/gemini-3.1-flash-lite-preview"]
    mock_ui_confirm.side_effect = [True, True, True, False, False]
    mock_ui_checkbox.return_value = []
    mock_validate.return_value = {"ok": True, "warnings": [], "errors": [], "missing_credentials": []}
    mock_apply.return_value = {
        "ok": True,
        "message": "Applied OpenClaw configuration.",
        "warnings": [],
        "channel_verification": {
            "statuses": {
                "telegram": {
                    "configured": True,
                    "verified": True,
                    "linked": True,
                    "detail": "connected, linked, running",
                }
            },
            "warnings": [],
        },
    }

    exit_code = _run_armorer_managed_openclaw_setup("openclaw")

    assert exit_code == 0
    mock_pairing.assert_not_called()
