"""Tests for the runtime flag loader."""

from __future__ import annotations

import pytest

from cerbero_bite.config.runtime_flags import (
    DATA_ANALYSIS_ENV,
    STRATEGY_ENV,
    RuntimeFlags,
    load_runtime_flags,
)


def test_default_profile_is_analysis_only() -> None:
    flags = load_runtime_flags(env={})
    assert flags == RuntimeFlags(
        data_analysis_enabled=True, strategy_enabled=False
    )


def test_strategy_can_be_explicitly_enabled() -> None:
    flags = load_runtime_flags(env={STRATEGY_ENV: "true"})
    assert flags.strategy_enabled is True
    assert flags.data_analysis_enabled is True


def test_data_analysis_can_be_disabled() -> None:
    flags = load_runtime_flags(env={DATA_ANALYSIS_ENV: "false"})
    assert flags.data_analysis_enabled is False
    assert flags.strategy_enabled is False


@pytest.mark.parametrize(
    "raw,expected",
    [
        ("1", True),
        ("0", False),
        ("yes", True),
        ("no", False),
        ("on", True),
        ("OFF", False),
        ("ENABLED", True),
        ("Disabled", False),
        ("True", True),
        ("False", False),
        ("  true  ", True),
    ],
)
def test_parses_common_truthy_falsy_tokens(raw: str, expected: bool) -> None:
    flags = load_runtime_flags(env={STRATEGY_ENV: raw})
    assert flags.strategy_enabled is expected


def test_blank_value_falls_back_to_default() -> None:
    flags = load_runtime_flags(env={DATA_ANALYSIS_ENV: "   ", STRATEGY_ENV: ""})
    assert flags.data_analysis_enabled is True
    assert flags.strategy_enabled is False


def test_unknown_token_raises() -> None:
    with pytest.raises(ValueError, match=DATA_ANALYSIS_ENV):
        load_runtime_flags(env={DATA_ANALYSIS_ENV: "maybe"})
