"""CollectorConfig — layered resolution with global defaults (TS-01 mirror of _config)."""

from __future__ import annotations

from mgf.logship._config import CollectorConfig


def test_defaults_are_the_globals() -> None:
    c = CollectorConfig.from_env({})
    assert c.backend == "sqlite"
    assert c.retention_days == 30  # the locked fleet-wide default
    assert c.flush_interval_s == 30.0
    assert c.openobserve_org == "default"
    assert c.log_stream == "mgf_logs"


def test_env_overrides_defaults() -> None:
    c = CollectorConfig.from_env(
        {
            "MGF_LOGSHIP_BACKEND": "openobserve",
            "MGF_LOGSHIP_RETENTION_DAYS": "7",
            "MGF_LOGSHIP_OPENOBSERVE_URL": "http://oo.internal",
            "MGF_LOGSHIP_TOKEN": "ship-secret",
            "MGF_LOGSHIP_FLUSH_INTERVAL_S": "5",
        }
    )
    assert c.backend == "openobserve"
    assert c.retention_days == 7
    assert c.openobserve_url == "http://oo.internal"
    assert c.token == "ship-secret"
    assert c.flush_interval_s == 5.0


def test_frozen() -> None:
    import dataclasses

    import pytest

    c = CollectorConfig()
    with pytest.raises(dataclasses.FrozenInstanceError):
        c.backend = "x"  # type: ignore[misc]
