"""Tests for environment variable priority over database values.

This module tests that environment variables are always checked first,
even when the database value is None (e.g., on first launch).

Regression test for issue #870: LDR_WEB_PORT not respected on first launch.
"""

import os

import pytest

from local_deep_research.settings.manager import get_typed_setting_value


class TestEnvVarPriorityOverNoneDb:
    """Test that env vars are checked even when db value is None."""

    @pytest.fixture(autouse=True)
    def clean_env(self):
        """Clean environment before each test."""
        original_env = {
            k: v for k, v in os.environ.items() if k.startswith("LDR_")
        }
        for key in list(os.environ.keys()):
            if key.startswith("LDR_"):
                os.environ.pop(key, None)
        yield
        for key in list(os.environ.keys()):
            if key.startswith("LDR_"):
                os.environ.pop(key, None)
        for key, value in original_env.items():
            os.environ[key] = value

    def test_env_var_used_when_db_value_is_none_number(self):
        """Test that number env var is used when database value is None.

        This is the specific bug from #870 - LDR_WEB_PORT was not
        respected on first launch because the code returned the default
        immediately when the database value was None.
        """
        os.environ["LDR_WEB_PORT"] = "8080"

        result = get_typed_setting_value(
            key="web.port",
            value=None,  # Simulates first launch, no value in DB
            ui_element="number",
            default=5000,
            check_env=True,
        )

        assert result == 8080

    def test_env_var_used_when_db_value_is_none_text(self):
        """Test that text env var is used when database value is None."""
        os.environ["LDR_WEB_HOST"] = "127.0.0.1"

        result = get_typed_setting_value(
            key="web.host",
            value=None,
            ui_element="text",
            default="0.0.0.0",
            check_env=True,
        )

        assert result == "127.0.0.1"

    def test_env_var_used_when_db_value_is_none_checkbox(self):
        """Test that boolean env var is used when database value is None."""
        os.environ["LDR_APP_DEBUG"] = "true"

        result = get_typed_setting_value(
            key="app.debug",
            value=None,
            ui_element="checkbox",
            default=False,
            check_env=True,
        )

        assert result is True

    def test_default_used_when_no_env_and_no_db(self):
        """Test that default is used when neither env nor db has value."""
        result = get_typed_setting_value(
            key="web.port",
            value=None,
            ui_element="number",
            default=5000,
            check_env=True,
        )

        assert result == 5000

    def test_env_var_overrides_db_value(self):
        """Test that env var takes priority over database value."""
        os.environ["LDR_WEB_PORT"] = "9000"

        result = get_typed_setting_value(
            key="web.port",
            value=8080,  # Value from database
            ui_element="number",
            default=5000,
            check_env=True,
        )

        assert result == 9000

    def test_db_value_used_when_check_env_false(self):
        """Test that db value is used when check_env is False."""
        os.environ["LDR_WEB_PORT"] = "9000"

        result = get_typed_setting_value(
            key="web.port",
            value=8080,
            ui_element="number",
            default=5000,
            check_env=False,
        )

        assert result == 8080

    def test_default_used_when_check_env_false_and_db_none(self):
        """Test that default is used when check_env=False and db value is None."""
        os.environ["LDR_WEB_PORT"] = "9000"

        result = get_typed_setting_value(
            key="web.port",
            value=None,
            ui_element="number",
            default=5000,
            check_env=False,
        )

        assert result == 5000
