"""
Tests for modules with zero test coverage.
Focuses on import validation and basic function smoke tests.
"""

import pytest
import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))


# ── MCP Plugin Loader (Miss.Bossy fixed this) ───────────────────────────────

class TestMCPPluginLoader:
    def test_import(self):
        from brynq.mcp_plugin_loader import MCPServerPluginLoader
        assert MCPServerPluginLoader is not None

    def test_load_plugins(self):
        from pathlib import Path
        from brynq.mcp_plugin_loader import MCPServerPluginLoader
        loader = MCPServerPluginLoader(Path("src/brynq/mcp_plugins"))
        tools, handlers = loader.load_plugins()
        assert len(tools) > 100
        assert len(handlers) > 100

    def test_no_orphan_tools(self):
        from pathlib import Path
        from brynq.mcp_plugin_loader import MCPServerPluginLoader
        loader = MCPServerPluginLoader(Path("src/brynq/mcp_plugins"))
        tools, handlers = loader.load_plugins()
        tool_names = set()
        for t in tools:
            name = t.get("name", "") if isinstance(t, dict) else t.name
            tool_names.add(name)
        orphans = tool_names - set(handlers.keys())
        assert len(orphans) == 0, f"Orphan tools without handlers: {orphans}"

    def test_nonexistent_dir(self):
        from pathlib import Path
        from brynq.mcp_plugin_loader import MCPServerPluginLoader
        loader = MCPServerPluginLoader(Path("/nonexistent/path"))
        tools, handlers = loader.load_plugins()
        assert tools == []
        assert handlers == {}


# ── Daemon (Miss.Bossy built this) ──────────────────────────────────────────

class TestDaemon:
    def test_import(self):
        from brynq.daemon import launch_agent, kill_agent, list_agents, daemon_status, check_health
        assert callable(launch_agent)
        assert callable(kill_agent)
        assert callable(list_agents)
        assert callable(daemon_status)
        assert callable(check_health)

    def test_list_agents(self):
        from brynq.daemon import list_agents
        result = list_agents()
        assert isinstance(result, list)

    def test_daemon_status(self):
        from brynq.daemon import daemon_status
        result = daemon_status()
        assert isinstance(result, dict)
        assert "daemon_running" in result
        assert "total_agents" in result

    def test_check_health(self):
        from brynq.daemon import check_health
        result = check_health()
        assert isinstance(result, list)

    def test_is_process_alive_invalid_pid(self):
        from brynq.daemon import _is_process_alive
        assert _is_process_alive(0) is False
        assert _is_process_alive(-1) is False

    def test_get_agent_logs_nonexistent(self):
        from brynq.daemon import get_agent_logs
        result = get_agent_logs("nonexistent-agent-xyz")
        assert "No logs found" in result


# ── API Security ─────────────────────────────────────────────────────────────

class TestAPISecurity:
    def test_import(self):
        from brynq.api_security import apply_security_hardening
        assert callable(apply_security_hardening)


# ── Orchestrator ─────────────────────────────────────────────────────────────

class TestOrchestrator:
    def test_import(self):
        from brynq.orchestrator import OrchestratorAgent
        assert OrchestratorAgent is not None

    def test_create_instance(self):
        from brynq.orchestrator import OrchestratorAgent
        agent = OrchestratorAgent(name="test-orch", backend="mock", model="test")
        assert agent.name == "test-orch"


# ── Supervisor ───────────────────────────────────────────────────────────────

class TestSupervisor:
    def test_import(self):
        from brynq.supervisor import AgentSupervisor
        assert AgentSupervisor is not None


# ── API Versioning ───────────────────────────────────────────────────────────

class TestAPIVersioning:
    def test_import(self):
        import brynq.api_versioning
        assert brynq.api_versioning is not None


# ── Company Roster ───────────────────────────────────────────────────────────

class TestCompanyRoster:
    def test_import(self):
        import brynq.company_roster
        assert brynq.company_roster is not None


# ── Threading Support ────────────────────────────────────────────────────────

class TestThreadingSupport:
    def test_import(self):
        import brynq.threading_support
        assert brynq.threading_support is not None
