"""Tests for centralized default model identifiers."""

from __future__ import annotations

from pathlib import Path

import yaml

from mindroom import model_defaults


def test_default_model_strings_are_not_redeclared_in_source() -> None:
    """Runtime code should import default model IDs from mindroom.model_defaults."""
    repo_root = Path(__file__).resolve().parents[1]
    model_defaults_path = repo_root / "src" / "mindroom" / "model_defaults.py"
    ignored_paths = {
        model_defaults_path,
        repo_root / "src" / "mindroom" / "tools_metadata.json",
    }
    model_strings = {
        value
        for name, value in vars(model_defaults).items()
        if name.isupper()
        if isinstance(value, str) and any(marker in value for marker in ("-", ".", "/", ":"))
    }

    offenders: list[str] = []
    for root in (repo_root / "src" / "mindroom", repo_root / "scripts"):
        for path in root.rglob("*"):
            if path in ignored_paths or path.suffix not in {".py", ".yaml", ".yml", ".json"}:
                continue
            text = path.read_text(encoding="utf-8")
            offenders.extend(
                f"{path.relative_to(repo_root)} contains {model_string!r}"
                for model_string in model_strings
                if model_string in text
            )

    assert offenders == []


def test_saas_default_config_models_match_central_defaults() -> None:
    """The generated Helm default config should stay in sync with the central SaaS model defaults."""
    repo_root = Path(__file__).resolve().parents[1]
    config_path = repo_root / "cluster" / "k8s" / "instance" / "default-config.yaml"
    config = yaml.safe_load(config_path.read_text(encoding="utf-8"))

    assert config["models"] == {
        name: preset.to_config_dict() for name, preset in model_defaults.SAAS_MODEL_PRESETS.items()
    }
    assert config["memory"]["llm"]["config"]["model"] == model_defaults.OPENAI_GPT_LUNA
    assert config["memory"]["embedder"]["config"]["model"] == model_defaults.OPENAI_EMBEDDING_SMALL
    assert config["voice"]["stt"]["model"] == model_defaults.OPENAI_TRANSCRIPTION


def test_saas_default_uses_current_gemini_flash() -> None:
    """The SaaS default and named Flash preset should use the current Gemini Flash model."""
    expected_model = "google/gemini-3.6-flash"
    retired_models = {
        "google/gemini-3-flash-preview",
        "google/gemini-3.5-flash",
    }

    assert model_defaults.SAAS_MODEL_PRESETS["default"].id == expected_model
    assert model_defaults.SAAS_MODEL_PRESETS["gemini_flash"].id == expected_model
    assert retired_models.isdisjoint(preset.id for preset in model_defaults.SAAS_MODEL_PRESETS.values())


def test_anthropic_frontier_presets_use_current_models() -> None:
    """Every Anthropic surface should expose current Fable and Opus IDs."""
    anthropic_alternatives = dict(model_defaults.CONFIG_INIT_MODEL_ALTERNATIVES["anthropic"])
    bedrock_alternatives = dict(model_defaults.CONFIG_INIT_MODEL_ALTERNATIVES["bedrock_claude"])
    openrouter_alternatives = dict(model_defaults.CONFIG_INIT_MODEL_ALTERNATIVES["openrouter"])
    vertex_alternatives = dict(model_defaults.CONFIG_INIT_MODEL_ALTERNATIVES["vertexai_claude"])

    assert anthropic_alternatives["fable"] == model_defaults.ModelPreset("anthropic", "claude-fable-5", 1_000_000)
    assert anthropic_alternatives["opus"] == model_defaults.ModelPreset("anthropic", "claude-opus-5", 1_000_000)
    assert model_defaults.CONFIG_INIT_MODEL_PRESETS["bedrock_claude"] == model_defaults.ModelPreset(
        "bedrock_claude",
        "anthropic.claude-opus-5",
        1_000_000,
    )
    assert bedrock_alternatives["fable"] == model_defaults.ModelPreset(
        "bedrock_claude",
        "anthropic.claude-fable-5",
        1_000_000,
    )
    assert openrouter_alternatives["fable"] == model_defaults.ModelPreset(
        "openrouter",
        "anthropic/claude-fable-5",
        1_000_000,
    )
    assert openrouter_alternatives["opus"] == model_defaults.ModelPreset(
        "openrouter",
        "anthropic/claude-opus-5",
        1_000_000,
    )
    assert vertex_alternatives["fable"] == model_defaults.ModelPreset(
        "vertexai_claude",
        "claude-fable-5",
        1_000_000,
    )
    assert vertex_alternatives["opus"] == model_defaults.ModelPreset(
        "vertexai_claude",
        "claude-opus-5",
        1_000_000,
    )
    assert model_defaults.SAAS_MODEL_PRESETS["fable"] == model_defaults.ModelPreset(
        "openrouter",
        "anthropic/claude-fable-5",
        1_000_000,
    )
    assert model_defaults.SAAS_MODEL_PRESETS["opus"] == model_defaults.ModelPreset(
        "openrouter",
        "anthropic/claude-opus-5",
        1_000_000,
    )


def test_current_google_and_openrouter_specialist_models() -> None:
    """Google media and OpenRouter specialist presets should use current IDs."""
    openrouter_alternatives = dict(model_defaults.CONFIG_INIT_MODEL_ALTERNATIVES["openrouter"])

    assert model_defaults.GOOGLE_AVATAR_PROMPT == "gemini-3.5-flash-lite"
    assert model_defaults.GOOGLE_AVATAR_IMAGE == "gemini-3.1-flash-image"
    assert model_defaults.GOOGLE_IMAGE == "gemini-3.1-flash-image"
    assert model_defaults.GOOGLE_VEO == "veo-3.1-generate-preview"
    assert model_defaults.GOOGLE_PROVIDER_DEFAULT_SAMPLING_MODEL_SUFFIXES == (
        "gemini-3.6-flash",
        "gemini-3.5-flash-lite",
    )
    assert model_defaults.CLAUDE_PROVIDER_DEFAULT_SAMPLING_MODEL_SUFFIXES == (
        "claude-fable-5",
        "claude-opus-5",
        "claude-sonnet-5",
    )
    assert openrouter_alternatives["gemini_flash"] == model_defaults.ModelPreset(
        "openrouter",
        "google/gemini-3.6-flash",
        1_048_576,
    )
    assert openrouter_alternatives["gemini_lite"] == model_defaults.ModelPreset(
        "openrouter",
        "google/gemini-3.5-flash-lite",
        1_048_576,
    )
    assert openrouter_alternatives["kimi"] == model_defaults.ModelPreset(
        "openrouter",
        "moonshotai/kimi-k3",
        1_048_576,
    )
    assert model_defaults.DEEPSEEK_V4_PRO == "deepseek-v4-pro"


def test_sonnet_presets_use_current_generation() -> None:
    """Sonnet presets should track the current provider-specific Sonnet generation."""
    bedrock_alternatives = dict(model_defaults.CONFIG_INIT_MODEL_ALTERNATIVES["bedrock_claude"])

    assert model_defaults.CONFIG_INIT_MODEL_PRESETS["anthropic"].id == "claude-sonnet-5"
    assert model_defaults.CONFIG_INIT_MODEL_PRESETS["vertexai_claude"].id == "claude-sonnet-5"
    assert model_defaults.CONFIG_INIT_MODEL_PRESETS["openrouter"].id == "anthropic/claude-sonnet-5"
    assert model_defaults.SAAS_MODEL_PRESETS["sonnet"].id == "anthropic/claude-sonnet-5"
    assert bedrock_alternatives["sonnet"].id == "anthropic.claude-sonnet-5"
    assert bedrock_alternatives["haiku"].id == "anthropic.claude-haiku-4-5"
    assert "claude-sonnet-4-6" not in {
        model_defaults.CONFIG_INIT_MODEL_PRESETS["anthropic"].id,
        model_defaults.CONFIG_INIT_MODEL_PRESETS["vertexai_claude"].id,
        model_defaults.CONFIG_INIT_MODEL_PRESETS["openrouter"].id,
        model_defaults.SAAS_MODEL_PRESETS["sonnet"].id,
        bedrock_alternatives["sonnet"].id,
        bedrock_alternatives["haiku"].id,
    }


def test_openai_presets_use_gpt_5_6_family() -> None:
    """OpenAI and Codex presets should use the current provider-specific GPT-5.6 models."""
    openai_alternatives = dict(model_defaults.CONFIG_INIT_MODEL_ALTERNATIVES["openai"])

    assert model_defaults.CONFIG_INIT_MODEL_PRESETS["openai"] == model_defaults.ModelPreset(
        "openai",
        "gpt-5.6",
        1_050_000,
    )
    assert model_defaults.CONFIG_INIT_MODEL_PRESETS["codex"] == model_defaults.ModelPreset(
        "codex",
        "gpt-5.6",
        258_000,
    )
    assert openai_alternatives == {
        "openai_terra": model_defaults.ModelPreset("openai", "gpt-5.6-terra", 1_050_000),
        "openai_luna": model_defaults.ModelPreset("openai", "gpt-5.6-luna", 1_050_000),
    }
    assert model_defaults.SAAS_MODEL_PRESETS["gpt5terra"] == model_defaults.ModelPreset(
        "openrouter",
        "openai/gpt-5.6-terra",
        1_050_000,
    )
    assert model_defaults.SAAS_MODEL_PRESETS["gpt5luna"] == model_defaults.ModelPreset(
        "openai",
        "gpt-5.6-luna",
        1_050_000,
    )


def test_glm_presets_use_current_generation() -> None:
    """GLM presets should track the current GLM generation on OpenRouter."""
    openrouter_alternatives = dict(model_defaults.CONFIG_INIT_MODEL_ALTERNATIVES["openrouter"])

    assert model_defaults.SAAS_MODEL_PRESETS["glm"].id == "z-ai/glm-5.2"
    assert openrouter_alternatives["glm"].id == "z-ai/glm-5.2"


def test_config_init_openrouter_alternatives_cover_saas_openrouter_models() -> None:
    """Config init should comment every non-default OpenRouter model alias we expose elsewhere."""
    openrouter_saas_aliases = {
        name
        for name, preset in model_defaults.SAAS_MODEL_PRESETS.items()
        if preset.provider == "openrouter" and name not in {"default", "sonnet"}
    }
    openrouter_alternative_aliases = {
        name for name, _preset in model_defaults.CONFIG_INIT_MODEL_ALTERNATIVES["openrouter"]
    }

    assert openrouter_alternative_aliases == openrouter_saas_aliases
