"""Test AWS profile handling in isolation."""

import os
import pytest
from awslabs.cloudwatch_applicationsignals_mcp_server import __version__
from unittest.mock import MagicMock, patch


def test_aws_profile_branch_coverage():
    """Test the AWS_PROFILE environment variable branch coverage."""
    # Test the condition when AWS_PROFILE is set
    with patch.dict(os.environ, {'AWS_PROFILE': 'test-profile'}):
        assert os.environ.get('AWS_PROFILE') == 'test-profile'

        # Test walrus operator assignment
        if aws_profile := os.environ.get('AWS_PROFILE'):
            assert aws_profile == 'test-profile'

    # Test the condition when AWS_PROFILE is not set
    with patch.dict(os.environ, {}, clear=True):
        assert os.environ.get('AWS_PROFILE') is None

        # Test walrus operator assignment with None
        if aws_profile := os.environ.get('AWS_PROFILE'):
            pytest.fail('Should not enter this branch when AWS_PROFILE is not set')
        else:
            assert aws_profile is None


def test_aws_client_initialization_flow():
    """Test the client initialization flow with and without AWS_PROFILE."""
    import boto3
    from botocore.config import Config

    # Mock config
    config = MagicMock(spec=Config)

    # Test with AWS_PROFILE
    with patch('boto3.Session') as mock_session:
        mock_session_instance = MagicMock()
        mock_session.return_value = mock_session_instance

        # Simulate the server initialization logic
        aws_profile = 'test-profile'
        AWS_REGION = 'us-west-2'

        if aws_profile:
            session = boto3.Session(profile_name=aws_profile, region_name=AWS_REGION)
            session.client('logs', config=config)
            session.client('application-signals', config=config)
            session.client('cloudwatch', config=config)
            session.client('xray', config=config)

            # Verify calls
            mock_session.assert_called_once_with(
                profile_name='test-profile', region_name='us-west-2'
            )
            assert mock_session_instance.client.call_count == 4

    # Test without AWS_PROFILE
    with patch('boto3.client') as mock_client:
        AWS_REGION = 'us-east-1'
        aws_profile = None

        if not aws_profile:
            boto3.client('logs', region_name=AWS_REGION, config=config)
            boto3.client('application-signals', region_name=AWS_REGION, config=config)
            boto3.client('cloudwatch', region_name=AWS_REGION, config=config)
            boto3.client('xray', region_name=AWS_REGION, config=config)

            # Verify calls
            assert mock_client.call_count == 4
            for call in mock_client.call_args_list:
                assert call.kwargs['region_name'] == 'us-east-1'
                assert call.kwargs['config'] == config


def test_server_initialization_with_aws_profile_coverage():
    """Test to ensure AWS_PROFILE code path gets coverage."""
    # This test simulates the exact logic from server.py to ensure coverage
    import boto3
    from botocore.config import Config

    # Mock the actual initialization logic
    mock_session = MagicMock()
    mock_session_instance = MagicMock()
    mock_session.return_value = mock_session_instance

    with patch.dict(os.environ, {'AWS_PROFILE': 'test-profile'}):
        with patch('boto3.Session', mock_session):
            # This is the exact code from server.py that needs coverage
            config = MagicMock(spec=Config)
            AWS_REGION = 'us-east-1'

            # Check for AWS_PROFILE environment variable (exact code from server.py)
            if aws_profile := os.environ.get('AWS_PROFILE'):
                # This block needs coverage
                session = boto3.Session(profile_name=aws_profile, region_name=AWS_REGION)
                session.client('logs', config=config)
                session.client('application-signals', config=config)
                session.client('cloudwatch', config=config)
                session.client('xray', config=config)

                # Verify the AWS profile was used
                mock_session.assert_called_once_with(
                    profile_name='test-profile', region_name='us-east-1'
                )
                assert mock_session_instance.client.call_count == 4


def test_initialize_aws_clients_with_profile():
    """Test _initialize_aws_clients function with AWS_PROFILE set."""
    from awslabs.cloudwatch_applicationsignals_mcp_server.aws_clients import (
        _initialize_aws_clients,
    )

    # Mock the necessary components
    mock_session = MagicMock()
    mock_session_instance = MagicMock()
    mock_session.return_value = mock_session_instance
    mock_client = MagicMock()
    mock_session_instance.client.return_value = mock_client

    with patch.dict(os.environ, {'AWS_PROFILE': 'test-profile', 'AWS_REGION': 'us-east-1'}):
        with patch(
            'awslabs.cloudwatch_applicationsignals_mcp_server.aws_clients.boto3.Session',
            mock_session,
        ):
            with patch('awslabs.cloudwatch_applicationsignals_mcp_server.aws_clients.Config'):
                # _initialize_aws_clients reads the module-level AWS_REGION (resolved
                # once at import), so pin it here rather than relying on the env var.
                with patch(
                    'awslabs.cloudwatch_applicationsignals_mcp_server.aws_clients.AWS_REGION',
                    'us-east-1',
                ):
                    # Call the initialization function
                    (
                        logs,
                        applicationsignals,
                        cloudwatch,
                        xray,
                        synthetics,
                        s3,
                        iam,
                        lambda_client,
                        sts,
                        rum,
                    ) = _initialize_aws_clients()

                    # Verify Session was called with the profile
                    mock_session.assert_called_once()
                    call_kwargs = mock_session.call_args[1]
                    assert call_kwargs['profile_name'] == 'test-profile'
                    assert call_kwargs['region_name'] == 'us-east-1'

                    # Verify all clients were created
                    assert mock_session_instance.client.call_count == 10
                    client_calls = [
                        call[0][0] for call in mock_session_instance.client.call_args_list
                    ]
                    assert 'logs' in client_calls
                    assert 'application-signals' in client_calls
                    assert 'cloudwatch' in client_calls
                    assert 'xray' in client_calls
                    assert 'rum' in client_calls

                    # Verify the returned clients
                    assert logs == mock_client
                    assert applicationsignals == mock_client
                    assert cloudwatch == mock_client
                    assert xray == mock_client
                    assert rum == mock_client


def test_initialize_aws_clients_with_mcp_source():
    """Test _initialize_aws_clients function with MCP_RUN_FROM set."""
    from awslabs.cloudwatch_applicationsignals_mcp_server.aws_clients import (
        _initialize_aws_clients,
    )

    with patch.dict(os.environ, {'MCP_RUN_FROM': 'test-caller', 'AWS_REGION': 'us-east-1'}):
        with patch(
            'awslabs.cloudwatch_applicationsignals_mcp_server.aws_clients.Config'
        ) as mock_config:
            with patch('boto3.client'):
                _initialize_aws_clients()

                # Verify Config was called with MCP_RUN_FROM in user agent
                mock_config.assert_called_once()
                call_args = mock_config.call_args
                user_agent = call_args.kwargs['user_agent_extra']
                assert (
                    user_agent
                    == f'awslabs.cloudwatch-applicationsignals-mcp-server/{__version__}/test-caller'
                )


def test_initialize_aws_clients_logs_rum_endpoint_override():
    """MCP_RUM_ENDPOINT must trigger the RUM endpoint-override log path."""
    from awslabs.cloudwatch_applicationsignals_mcp_server.aws_clients import (
        _initialize_aws_clients,
    )

    with patch.dict(
        os.environ,
        {'MCP_RUM_ENDPOINT': 'https://rum.test.local', 'AWS_REGION': 'us-east-1'},
    ):
        with patch('awslabs.cloudwatch_applicationsignals_mcp_server.aws_clients.Config'):
            with patch(
                'awslabs.cloudwatch_applicationsignals_mcp_server.aws_clients.logger'
            ) as mock_logger:
                with patch('boto3.client'):
                    _initialize_aws_clients()
                    msgs = [args[0] for args, _ in mock_logger.debug.call_args_list if args]
                    assert any('RUM endpoint override' in m for m in msgs)
