import textwrap
from pathlib import Path

from veritell_langchain.config import resolve_config


def test_config_file_picks_env_specific_key_and_base_url(tmp_path: Path) -> None:
    cfg_path = tmp_path / "veritell.toml"
    cfg_path.write_text(
        textwrap.dedent(
            """
            [veritell]
            env = "staging"
            timeout_seconds = 12
            fail_open = true

            [veritell.environments.staging]
            base_url = "https://api.dev.veritell.ai"
            api_key = "stg_key"

            [veritell.environments.production]
            base_url = "https://api.veritell.ai"
            api_key = "prod_key"
            """
        ).lstrip(),
        encoding="utf-8",
    )

    cfg = resolve_config(config_path=str(cfg_path))
    assert cfg.env == "staging"
    assert cfg.base_url == "https://api.dev.veritell.ai"
    assert cfg.api_key == "stg_key"
    assert cfg.timeout_seconds == 12.0
    assert cfg.fail_open is True


def test_constructor_overrides_file(tmp_path: Path) -> None:
    cfg_path = tmp_path / "veritell.toml"
    cfg_path.write_text(
        textwrap.dedent(
            """
            [veritell]
            env = "production"

            [veritell.environments.production]
            base_url = "https://api.veritell.ai"
            api_key = "prod_key"
            """
        ).lstrip(),
        encoding="utf-8",
    )

    cfg = resolve_config(
        config_path=str(cfg_path),
        env="staging",
        api_key="override_key",
        base_url="https://override",
    )
    assert cfg.env == "staging"
    assert cfg.api_key == "override_key"
    assert cfg.base_url == "https://override"
