from __future__ import annotations

import pytest


def test_v2_config_from_root_dict_new_format_defaults():
    from argus.service.config import V2Config

    cfg = V2Config.from_root_dict(
        {
            "ray": {"shared_root": "/private"},
            "service": {
                "api": {"host": "127.0.0.1", "port": 9999},
                "auth": {"token_env": "X"},
                "sqlite": {"db_path": "/tmp/x.sqlite3"},
                "scheduler": {"tick_s": 1, "retry_interval_s": 2, "max_running_tasks": 3},
            },
        }
    )
    assert cfg.api.host == "127.0.0.1"
    assert cfg.api.port == 9999
    assert cfg.auth.token_env == "X"
    assert cfg.sqlite.db_path.endswith(".sqlite3")
    assert cfg.scheduler.max_running_tasks == 3
    assert cfg.tracking.wandb.enabled is False
    assert cfg.serving.enabled is False


def test_v2_config_backward_compat_v2_section_and_default_db_path():
    from argus.service.config import V2Config

    cfg = V2Config.from_root_dict({"shared_root": "/private", "v2": {"sqlite": {}}})
    assert cfg.sqlite.db_path == "/private/common/db/mvp.sqlite3"
    assert cfg.tracking.wandb.enabled is False


def test_v2_config_requires_mappings():
    from argus.service.config import V2Config

    with pytest.raises(ValueError, match="config\\.service must be a mapping"):
        V2Config.from_root_dict({"service": ["nope"]})
    with pytest.raises(ValueError, match="config\\.service\\.\\{api,auth,sqlite,scheduler\\} must be mappings"):
        V2Config.from_root_dict({"service": {"api": [1], "auth": {}, "sqlite": {}, "scheduler": {}}})


def test_v2_config_requires_data_mappings():
    from argus.service.config import V2Config

    base = {
        "ray": {"shared_root": "/private"},
        "service": {"api": {}, "auth": {}, "sqlite": {}, "scheduler": {}},
    }

    with pytest.raises(ValueError, match="config\\.data must be a mapping"):
        V2Config.from_root_dict({**base, "data": ["nope"]})

    with pytest.raises(ValueError, match="config\\.data\\.\\{sftpgo,retention\\} must be mappings"):
        V2Config.from_root_dict({**base, "data": {"sftpgo": ["x"], "retention": {}}})


def test_v2_config_requires_tracking_and_serving_mappings():
    from argus.service.config import V2Config

    base = {
        "ray": {"shared_root": "/private"},
        "service": {"api": {}, "auth": {}, "sqlite": {}, "scheduler": {}},
        "data": {"sftpgo": {}, "retention": {}},
    }

    with pytest.raises(ValueError, match="config\\.tracking must be a mapping"):
        V2Config.from_root_dict({**base, "tracking": ["nope"]})

    with pytest.raises(ValueError, match="config\\.tracking\\.wandb must be a mapping"):
        V2Config.from_root_dict({**base, "tracking": {"wandb": ["nope"]}})

    with pytest.raises(ValueError, match="config\\.serving must be a mapping"):
        V2Config.from_root_dict({**base, "serving": ["nope"]})

    with pytest.raises(ValueError, match="config\\.serving\\.\\{serve,llm\\} must be mappings"):
        V2Config.from_root_dict({**base, "serving": {"serve": ["x"], "llm": {}}})

def test_tracking_wandb_defaults_disabled():
    from argus.service.config import V2Config

    cfg = V2Config.from_root_dict(
        {
            "ray": {"shared_root": "/private"},
            "service": {"api": {}, "auth": {}, "sqlite": {}, "scheduler": {}},
            "data": {"sftpgo": {}, "retention": {}},
        }
    )
    assert cfg.tracking.wandb.enabled is False
    assert cfg.tracking.wandb.base_url == ""
    assert cfg.tracking.wandb.api_key_env == "WANDB_API_KEY"
    assert cfg.tracking.wandb.project_suffix == "_project"


def test_tracking_wandb_enabled_parses():
    from argus.service.config import V2Config

    cfg = V2Config.from_root_dict(
        {
            "ray": {"shared_root": "/private"},
            "service": {"api": {}, "auth": {}, "sqlite": {}, "scheduler": {}},
            "tracking": {"wandb": {"enabled": True, "base_url": "http://127.0.0.1:8090", "api_key_env": "X"}},
            "data": {"sftpgo": {}, "retention": {}},
        }
    )
    assert cfg.tracking.wandb.enabled is True
    assert cfg.tracking.wandb.base_url == "http://127.0.0.1:8090"
    assert cfg.tracking.wandb.api_key_env == "X"


def test_tracking_wandb_enabled_requires_base_url():
    from argus.service.config import V2Config

    with pytest.raises(ValueError, match="tracking\\.wandb\\.base_url is required"):
        V2Config.from_root_dict(
            {
                "ray": {"shared_root": "/private"},
                "service": {"api": {}, "auth": {}, "sqlite": {}, "scheduler": {}},
                "tracking": {"wandb": {"enabled": True, "base_url": ""}},
                "data": {"sftpgo": {}, "retention": {}},
            }
        )
