"""Tests for web_client_deployment_mode module."""

from unittest.mock import patch

import pytest

from wren.app_server.web_client.web_client_deployment_mode import (
    get_deployment_mode,
)


class TestGetDeploymentMode:
    """Tests for get_deployment_mode() function."""

    @pytest.fixture(autouse=True)
    def _hermetic_env(self, monkeypatch):
        """Each test fully controls deployment-mode env; ignore ambient values."""
        for var in ('OH_DEPLOYMENT_MODE', 'OH_WEB_HOST', 'WEB_HOST'):
            monkeypatch.delenv(var, raising=False)

    @pytest.mark.parametrize(
        'web_host,expected',
        [
            # All-Hands managed domains should return 'cloud'
            ('app.wren.dev', 'cloud'),
            ('staging.wren.dev', 'cloud'),
            ('feature-123.staging.wren.dev', 'cloud'),
            ('app.wren.ai', 'cloud'),
            ('subdomain.wren.ai', 'cloud'),
            # Customer domains should return 'self_hosted'
            ('wren.acme.com', 'self_hosted'),
            ('internal.company.io', 'self_hosted'),
            ('dev.mycompany.net', 'self_hosted'),
            # Edge cases - not subdomains
            ('wren.dev', 'self_hosted'),
            ('wren.ai', 'self_hosted'),
            # Malicious domains
            ('fake-wren.dev', 'self_hosted'),
            ('app.wren.dev.evil.com', 'self_hosted'),
        ],
    )
    def test_deployment_mode_detection(self, web_host: str, expected: str):
        """Test that deployment mode is correctly determined based on WEB_HOST."""
        with patch.dict('os.environ', {'WEB_HOST': web_host}, clear=False):
            result = get_deployment_mode()
            assert result == expected

    def test_returns_none_when_web_host_empty(self):
        """Test that empty WEB_HOST returns None."""
        with patch.dict('os.environ', {'WEB_HOST': ''}, clear=False):
            result = get_deployment_mode()
            assert result is None

    def test_returns_none_when_web_host_not_set(self):
        """Test that missing WEB_HOST returns None."""
        with patch.dict('os.environ', {}, clear=True):
            result = get_deployment_mode()
            assert result is None

    def test_returns_none_when_web_host_whitespace_only(self):
        """Test that whitespace-only WEB_HOST returns None."""
        with patch.dict('os.environ', {'WEB_HOST': '   '}, clear=False):
            result = get_deployment_mode()
            assert result is None

    def test_oh_web_host_takes_precedence_over_web_host(self):
        """Test that OH_WEB_HOST takes precedence over WEB_HOST."""
        with patch.dict(
            'os.environ',
            {'OH_WEB_HOST': 'app.wren.dev', 'WEB_HOST': 'customer.example.com'},
            clear=False,
        ):
            result = get_deployment_mode()
            assert result == 'cloud'

    def test_falls_back_to_web_host_when_oh_web_host_not_set(self):
        """Test that WEB_HOST is used when OH_WEB_HOST is not set."""
        with patch.dict(
            'os.environ', {'WEB_HOST': 'customer.example.com'}, clear=False
        ):
            # Ensure OH_WEB_HOST is not set
            import os

            if 'OH_WEB_HOST' in os.environ:
                del os.environ['OH_WEB_HOST']
            result = get_deployment_mode()
            assert result == 'self_hosted'

    @pytest.mark.parametrize(
        'flag,web_host,expected',
        [
            # Explicit flag wins over the host heuristic
            ('self_hosted', 'app.wren.dev', 'self_hosted'),
            ('cloud', 'customer.example.com', 'cloud'),
            # Case/whitespace tolerant
            (' Cloud ', 'customer.example.com', 'cloud'),
            # Invalid value falls back to the host chain
            ('bogus', 'app.wren.dev', 'cloud'),
        ],
    )
    def test_explicit_deployment_mode_overrides_host(
        self, flag: str, web_host: str, expected: str
    ):
        """OH_DEPLOYMENT_MODE takes precedence; invalid values fall back to WEB_HOST."""
        with patch.dict(
            'os.environ',
            {'OH_DEPLOYMENT_MODE': flag, 'WEB_HOST': web_host},
            clear=False,
        ):
            assert get_deployment_mode() == expected
