import pytest
import tempfile
import shutil
from pathlib import Path
from multi_swarm.plugins.base import PluginBase, PluginMetadata
from multi_swarm.plugins.registry import PluginRegistry
from multi_swarm.plugins.tools.development.git import GitTool

@pytest.fixture
def temp_workspace():
    """Create a temporary workspace for testing."""
    temp_dir = tempfile.mkdtemp()
    workspace = Path(temp_dir)
    
    # Create necessary subdirectories
    (workspace / "plugins").mkdir()
    (workspace / "config").mkdir()
    (workspace / "repos").mkdir()
    
    yield workspace
    shutil.rmtree(temp_dir)

@pytest.fixture
def plugin_registry():
    """Create a plugin registry with Git tool."""
    registry = PluginRegistry()
    registry.register_plugin(GitTool)
    yield registry
    registry.cleanup_all()

class TestPluginIntegration:
    def test_git_tool_functionality(self, temp_workspace, plugin_registry):
        """Test Git tool functionality."""
        # Get Git tool instance
        git_tool = plugin_registry.get_plugin("git_tool")
        assert git_tool is not None
        
        # Test repository creation
        repo_path = temp_workspace / "repos/test_repo"
        repo_path.mkdir()
        
        # Initialize repository
        result = git_tool.initialize(str(repo_path))
        assert result["status"] == "success"
        
        # Create and commit a test file
        test_file = repo_path / "test.txt"
        test_file.write_text("Test content")
        
        commit_result = git_tool.commit_changes(
            str(repo_path),
            "Initial commit",
            ["test.txt"]
        )
        assert commit_result["status"] == "success"
        
        # Check repository status
        status = git_tool.get_status(str(repo_path))
        assert status["status"] == "success"
        assert not status["is_dirty"]
    
    def test_plugin_health_checks(self, plugin_registry):
        """Test health checks for plugins."""
        # Get Git tool instance
        git_tool = plugin_registry.get_plugin("git_tool")
        
        # Check Git tool health
        health = git_tool.health_check()
        assert health["status"] == "healthy"
        assert "version" in health
        
        # Check all plugins health
        all_health = plugin_registry.health_check_all()
        assert "git_tool" in all_health
        assert all_health["git_tool"]["status"] == "healthy"
    
    def test_plugin_cleanup(self, plugin_registry):
        """Test plugin cleanup."""
        # Get Git tool instance
        git_tool = plugin_registry.get_plugin("git_tool")
        assert git_tool is not None
        
        # Cleanup all plugins
        plugin_registry.cleanup_all()
        assert len(plugin_registry._instances) == 0
    
    def test_error_handling(self, temp_workspace, plugin_registry):
        """Test error handling in plugins."""
        git_tool = plugin_registry.get_plugin("git_tool")
        
        # Test invalid repository path
        invalid_path = temp_workspace / "non_existent"
        result = git_tool.get_status(str(invalid_path))
        assert result["status"] == "error"
        assert "error" in result 