import pytest

from aider_gitea import (
    AIDER_LINT,
    AIDER_TEST,
    CLAUDE_CODE_MESSAGE_FORMAT,
    AiderCodeSolver,
    ClaudeCodeSolver,
    create_code_solver,
    is_anthropic_model,
)


class TestClaudeCodeIntegration:
    """Test Claude Code integration and model routing logic."""

    def test_is_anthropic_model_detection(self):
        """Test that Anthropic models are correctly detected."""
        # Anthropic models should return True
        assert is_anthropic_model('claude-3-sonnet')
        assert is_anthropic_model('claude-3-haiku')
        assert is_anthropic_model('claude-3-opus')
        assert is_anthropic_model('anthropic/claude-3-sonnet')
        assert is_anthropic_model('Claude-3-Sonnet')  # Case insensitive
        assert is_anthropic_model('ANTHROPIC/CLAUDE')
        assert is_anthropic_model('some-sonnet-model')
        assert is_anthropic_model('haiku-variant')

        # Non-Anthropic models should return False
        assert not is_anthropic_model('gpt-4')
        assert not is_anthropic_model('gpt-3.5-turbo')
        assert not is_anthropic_model('ollama/llama')
        assert not is_anthropic_model('gemini-pro')
        assert not is_anthropic_model('mistral-7b')
        assert not is_anthropic_model('')
        assert not is_anthropic_model(None)

    def test_create_code_solver_routing(self, monkeypatch):
        """Test that the correct solver is created based on model."""
        import aider_gitea

        # Test Anthropic model routing
        monkeypatch.setattr(aider_gitea, 'CODE_MODEL', 'claude-3-sonnet')
        solver = create_code_solver()
        assert isinstance(solver, ClaudeCodeSolver)

        # Test non-Anthropic model routing
        monkeypatch.setattr(aider_gitea, 'CODE_MODEL', 'gpt-4')
        solver = create_code_solver()
        assert isinstance(solver, AiderCodeSolver)

        # Test None model routing (should default to Aider)
        monkeypatch.setattr(aider_gitea, 'CODE_MODEL', None)
        solver = create_code_solver()
        assert isinstance(solver, AiderCodeSolver)

    def test_claude_code_solver_command_creation(self):
        """Test that Claude Code commands are created correctly."""
        import aider_gitea

        solver = ClaudeCodeSolver()
        issue = 'Fix the bug in the code'

        # Test without model
        with pytest.MonkeyPatch().context() as m:
            m.setattr(aider_gitea, 'CODE_MODEL', None)
            cmd = solver._create_claude_command(issue)
            expected = [
                'claude',
                '-p',
                '--output-format',
                'json',
                '--max-turns',
                '10',
                issue,
            ]
            assert cmd == expected

        # Test with model
        with pytest.MonkeyPatch().context() as m:
            m.setattr(aider_gitea, 'CODE_MODEL', 'claude-3-sonnet')
            cmd = solver._create_claude_command(issue)
            expected = [
                'claude',
                '-p',
                '--output-format',
                'json',
                '--max-turns',
                '10',
                '--model',
                'claude-3-sonnet',
                issue,
            ]
            assert cmd == expected

    def test_claude_code_message_format(self):
        """Test that Claude Code message format works correctly."""
        issue_content = 'Fix the authentication bug'

        formatted_message = CLAUDE_CODE_MESSAGE_FORMAT.format(
            issue=issue_content,
            test_command=AIDER_TEST,
            lint_command=AIDER_LINT,
        )

        # Verify the issue content is included
        assert issue_content in formatted_message

        # Verify the test and lint commands are included
        assert AIDER_TEST in formatted_message
        assert AIDER_LINT in formatted_message

        # Verify the guidelines are present
        assert 'Run tests after making changes' in formatted_message
        assert 'Follow existing code style' in formatted_message
        assert 'Make minimal, focused changes' in formatted_message
        assert 'Commit your changes' in formatted_message

        # Verify the structure contains placeholders that got replaced
        assert '{issue}' not in formatted_message
        assert '{test_command}' not in formatted_message
        assert '{lint_command}' not in formatted_message