"""Tests for skill builder integration awareness."""

from __future__ import annotations

from unittest.mock import patch

import pytest


class TestBuildIntegrationsContext:
    """Test that the skill builder injects integration context into prompts."""

    @patch("alive.integrations.get_available_integrations")
    def test_includes_web_search_when_available(self, mock_get):
        from alive.core.skill_builder import _build_integrations_context

        mock_get.return_value = {
            "web_search": {"configured": True, "backend": "duckduckgo"},
            "telegram": {"configured": False},
            "browser": {"configured": False},
            "webhook": {"configured": False},
        }

        ctx = _build_integrations_context()
        assert "search_web" in ctx
        assert "search_and_read" in ctx
        assert "search_news" in ctx
        assert "duckduckgo" in ctx

    @patch("alive.integrations.get_available_integrations")
    def test_includes_telegram_when_configured(self, mock_get):
        from alive.core.skill_builder import _build_integrations_context

        mock_get.return_value = {
            "web_search": {"configured": True, "backend": "duckduckgo"},
            "telegram": {"configured": True},
            "browser": {"configured": False},
            "webhook": {"configured": False},
        }

        ctx = _build_integrations_context()
        assert "send_telegram" in ctx
        assert "alert me" in ctx.lower() or "notify me" in ctx.lower()

    @patch("alive.integrations.get_available_integrations")
    def test_includes_browser_when_configured(self, mock_get):
        from alive.core.skill_builder import _build_integrations_context

        mock_get.return_value = {
            "web_search": {"configured": True, "backend": "duckduckgo"},
            "telegram": {"configured": False},
            "browser": {"configured": True},
            "webhook": {"configured": False},
        }

        ctx = _build_integrations_context()
        assert "BrowserTool" in ctx
        assert "playwright" in ctx.lower() or "browser" in ctx.lower()

    @patch("alive.integrations.get_available_integrations")
    def test_includes_webhook_when_configured(self, mock_get):
        from alive.core.skill_builder import _build_integrations_context

        mock_get.return_value = {
            "web_search": {"configured": True, "backend": "duckduckgo"},
            "telegram": {"configured": False},
            "browser": {"configured": False},
            "webhook": {"configured": True, "names": ["slack-alerts"]},
        }

        ctx = _build_integrations_context()
        assert "send_webhook" in ctx
        assert "slack-alerts" in ctx

    @patch("alive.integrations.get_available_integrations")
    def test_excludes_unconfigured_integrations(self, mock_get):
        from alive.core.skill_builder import _build_integrations_context

        mock_get.return_value = {
            "web_search": {"configured": True, "backend": "duckduckgo"},
            "telegram": {"configured": False},
            "browser": {"configured": False},
            "webhook": {"configured": False},
        }

        ctx = _build_integrations_context()
        assert "send_telegram" not in ctx
        assert "BrowserTool" not in ctx
        assert "send_webhook" not in ctx

    @patch("alive.integrations.get_available_integrations")
    def test_all_configured(self, mock_get):
        from alive.core.skill_builder import _build_integrations_context

        mock_get.return_value = {
            "web_search": {"configured": True, "backend": "tavily"},
            "telegram": {"configured": True},
            "browser": {"configured": True},
            "webhook": {"configured": True, "names": ["slack", "discord"]},
        }

        ctx = _build_integrations_context()
        assert "search_web" in ctx
        assert "send_telegram" in ctx
        assert "BrowserTool" in ctx
        assert "send_webhook" in ctx
        assert "tavily" in ctx

    @patch("alive.integrations.get_available_integrations", side_effect=Exception("import error"))
    def test_returns_empty_on_error(self, mock_get):
        from alive.core.skill_builder import _build_integrations_context

        ctx = _build_integrations_context()
        assert ctx == ""

    @patch("alive.integrations.get_available_integrations")
    def test_includes_important_rules(self, mock_get):
        from alive.core.skill_builder import _build_integrations_context

        mock_get.return_value = {
            "web_search": {"configured": True, "backend": "duckduckgo"},
            "telegram": {"configured": False},
            "browser": {"configured": False},
            "webhook": {"configured": False},
        }

        ctx = _build_integrations_context()
        assert "IMPORTANT RULES" in ctx
        assert "monitor" in ctx.lower() or "track" in ctx.lower()


class TestGenerateSystemPrompt:
    """Test that the generate system prompt includes integration context."""

    def test_prompt_has_integrations_placeholder(self):
        from alive.core.skill_builder import _GENERATE_SYSTEM

        assert "{integrations_context}" in _GENERATE_SYSTEM

    @patch("alive.core.skill_builder._build_integrations_context", return_value="TEST_CONTEXT_HERE")
    def test_get_generate_system_injects_context(self, mock_ctx):
        from alive.core.skill_builder import SkillBuilder

        builder = SkillBuilder("test skill")
        prompt = builder._get_generate_system()
        assert "TEST_CONTEXT_HERE" in prompt
        assert "{integrations_context}" not in prompt
