"""
Test TDD workflow integration for Issue #11: Setup TDD workspace infrastructure

This test validates the complete TDD workflow from workspace creation through
test generation to completion and cleanup.

Issue Reference: #11 - Setup TDD workspace infrastructure
"""
import pytest
import os
import subprocess
import tempfile
import shutil
from pathlib import Path
from unittest.mock import patch, MagicMock
from tddai.config import TddaiConfig
from tddai.workspace import WorkspaceStatus


class TestTDDWorkflowIntegration:
    """Test complete TDD workflow integration."""

    def setup_method(self):
        """Set up test environment."""
        self.original_cwd = os.getcwd()
        self.test_dir = tempfile.mkdtemp()
        os.chdir(self.test_dir)

    def teardown_method(self):
        """Clean up test environment."""
        os.chdir(self.original_cwd)
        if os.path.exists(self.test_dir):
            shutil.rmtree(self.test_dir)

    @patch('tddai.IssueFetcher.fetch_issue')
    def test_complete_tdd_workflow_cycle(self, mock_fetch):
        """Test the complete TDD workflow from start to finish."""
        # Arrange
        mock_fetch.return_value = {
            'number': 11,
            'title': 'Setup TDD workspace infrastructure',
            'body': 'Complete workflow test',
            'state': 'open'
        }

        # Simulate the make commands workflow
        from tddai import WorkspaceManager
        config = TddaiConfig(workspace_dir=Path('.markitect_workspace'))
        workspace_manager = WorkspaceManager(config)

        # Act & Assert - Workspace Creation
        issue_data = mock_fetch.return_value
        workspace = workspace_manager.create_workspace(issue_data)
        assert workspace.issue_dir.exists()

        # Act & Assert - Status Check
        status = workspace_manager.get_workspace_status()
        assert status == WorkspaceStatus.ACTIVE

        # Act & Assert - Test Generation (simulate multiple tests)
        test_files = [
            'test_issue_11_basic.py',
            'test_issue_11_advanced.py'
        ]

        for test_file in test_files:
            workspace_manager.add_test_to_workspace(test_file, f'# Test file: {test_file}\ndef test_example(): pass')

        # Verify tests are created
        status = workspace_manager.get_workspace_status()
        assert status == WorkspaceStatus.ACTIVE

        # Act & Assert - Workspace Completion
        main_tests_dir = Path('tests')
        main_tests_dir.mkdir(exist_ok=True)

        workspace_manager.finish_workspace()

        # Verify tests moved to main
        for test_file in test_files:
            main_test_path = main_tests_dir / test_file
            assert main_test_path.exists()

        # Verify workspace cleaned up
        final_status = workspace_manager.get_workspace_status()
        assert final_status == WorkspaceStatus.CLEAN

    def test_workspace_git_exclusion(self):
        """Test that workspace files are properly excluded from git."""
        # Arrange
        gitignore_path = Path('.gitignore')
        gitignore_content = """
# MarkiTect issue workspace (temporary development files)
.markitect_workspace/
"""
        gitignore_path.write_text(gitignore_content)

        # Create workspace directory
        workspace_dir = Path('.markitect_workspace')
        workspace_dir.mkdir()
        test_file = workspace_dir / 'test_file.py'
        test_file.write_text('# Test content')

        # Act - Check git status would ignore workspace files
        # This simulates what git status would show
        gitignore_patterns = ['.markitect_workspace/']

        # Assert
        assert any(str(test_file).startswith(pattern.rstrip('/')) for pattern in gitignore_patterns)

    @patch('subprocess.run')
    def test_makefile_integration_commands(self, mock_run):
        """Test that Makefile commands integrate properly with workspace system."""
        # Arrange
        mock_run.return_value = MagicMock(returncode=0, stdout='Success', stderr='')

        # Act & Assert - Test make tdd-start command integration
        from tddai_cli import main as cli_main

        # Simulate CLI call for tdd-start
        with patch('sys.argv', ['tddai_cli.py', 'start-issue', '11']):
            with patch('tddai.IssueFetcher.fetch_issue') as mock_fetch:
                mock_fetch.return_value = {
                    'number': 11,
                    'title': 'Setup TDD workspace infrastructure',
                    'body': 'Makefile integration test',
                    'state': 'open'
                }
                # This would normally create workspace
                # Just verify the CLI interface works
                assert callable(cli_main)

    def test_error_handling_invalid_workflow_states(self):
        """Test error handling for invalid workflow states."""
        from tddai import WorkspaceManager, WorkspaceError

        config = TddaiConfig(workspace_dir=Path('.markitect_workspace'))
        workspace_manager = WorkspaceManager(config)

        # Test adding test without workspace
        with pytest.raises(WorkspaceError, match="No active workspace"):
            workspace_manager.add_test_to_workspace("test_file.py", "test content")

        # Test finishing workspace without workspace (should return None, not raise)
        result = workspace_manager.finish_workspace()
        assert result is None

        # Test getting status without workspace
        status = workspace_manager.get_workspace_status()
        assert status == WorkspaceStatus.CLEAN

    def test_workspace_status_monitoring_accuracy(self):
        """Test that workspace status monitoring provides accurate information."""
        # Arrange
        from tddai import WorkspaceManager
        config = TddaiConfig(workspace_dir=Path('.markitect_workspace'))
        workspace_manager = WorkspaceManager(config)

        issue_data = {
            'number': 11,
            'title': 'Setup TDD workspace infrastructure',
            'body': 'Status monitoring test',
            'state': 'open'
        }

        # Act
        workspace = workspace_manager.create_workspace(issue_data)

        # Add some test files using the WorkspaceManager method
        test_files = ['test_a.py', 'test_b.py', 'test_c.py']
        for test_file in test_files:
            workspace_manager.add_test_to_workspace(test_file, f'# {test_file}')

        status = workspace_manager.get_workspace_status()

        # Assert
        assert status == WorkspaceStatus.ACTIVE

        # Check that test files were actually created
        assert workspace.tests_dir.exists()
        created_files = list(workspace.tests_dir.glob("*.py"))
        assert len(created_files) == 3


