import pytest

from ouro.config import Config
from ouro.core.llm.model_manager import ModelManager
from ouro.core.llm.oauth_model_sync import remove_oauth_models, sync_oauth_models


@pytest.fixture(autouse=True)
def _disable_dynamic_model_refresh(monkeypatch):
    monkeypatch.setattr(Config, "OAUTH_MODEL_DYNAMIC_REFRESH", False)


def _write_models_yaml(path, content: str) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(content, encoding="utf-8")


def test_sync_oauth_models_adds_chatgpt_models(tmp_path):
    config_path = tmp_path / "models.yaml"
    _write_models_yaml(
        config_path,
        "\n".join(
            [
                "models:",
                "  openai/gpt-4o:",
                "    api_key: test",
                "default: openai/gpt-4o",
                "",
            ]
        ),
    )

    manager = ModelManager(config_path=str(config_path))
    added = sync_oauth_models(manager, "chatgpt")

    assert added
    assert "openai-codex/gpt-5.5" in manager.models
    assert "openai-codex/gpt-5.4" in manager.models
    assert "openai-codex/gpt-5.2-codex" in manager.models
    assert manager.models["openai-codex/gpt-5.5"].extra.get("oauth_managed") is True


def test_sync_oauth_models_removes_stale_managed_entries(tmp_path):
    config_path = tmp_path / "models.yaml"
    _write_models_yaml(
        config_path,
        "\n".join(
            [
                "models:",
                "  openai-codex/legacy-codex:",
                "    timeout: 600",
                "    oauth_managed: true",
                "    oauth_provider: chatgpt",
                "  openai-codex/gpt-5.2-codex:",
                "    timeout: 600",
                "    oauth_managed: true",
                "    oauth_provider: chatgpt",
                "default: openai-codex/legacy-codex",
                "",
            ]
        ),
    )

    manager = ModelManager(config_path=str(config_path))
    sync_oauth_models(manager, "chatgpt")

    assert "openai-codex/legacy-codex" not in manager.models
    assert "openai-codex/gpt-5.5" in manager.models
    assert "openai-codex/gpt-5.2-codex" in manager.models
    assert manager.default_model_id in manager.models


def test_remove_oauth_models_removes_only_managed_entries(tmp_path):
    config_path = tmp_path / "models.yaml"
    _write_models_yaml(
        config_path,
        "\n".join(
            [
                "models:",
                "  openai-codex/gpt-5.2-codex:",
                "    timeout: 600",
                "    oauth_managed: true",
                "    oauth_provider: chatgpt",
                "  chatgpt/gpt-5.2:",
                "    timeout: 600",
                "  openai/gpt-4o:",
                "    api_key: test",
                "default: openai-codex/gpt-5.2-codex",
                "",
            ]
        ),
    )

    manager = ModelManager(config_path=str(config_path))
    removed = remove_oauth_models(manager, "chatgpt")

    assert "openai-codex/gpt-5.2-codex" in removed
    assert "openai-codex/gpt-5.2-codex" not in manager.models
    # Unmanaged entry should remain
    assert "chatgpt/gpt-5.2" in manager.models
    # Default should be repaired
    assert manager.default_model_id in manager.models


def test_remove_oauth_models_removes_stale_managed_entries(tmp_path):
    config_path = tmp_path / "models.yaml"
    _write_models_yaml(
        config_path,
        "\n".join(
            [
                "models:",
                "  openai-codex/legacy-codex:",
                "    timeout: 600",
                "    oauth_managed: true",
                "    oauth_provider: chatgpt",
                "  openai/gpt-4o:",
                "    api_key: test",
                "default: openai-codex/legacy-codex",
                "",
            ]
        ),
    )

    manager = ModelManager(config_path=str(config_path))
    removed = remove_oauth_models(manager, "chatgpt")

    assert removed == ["openai-codex/legacy-codex"]
    assert "openai-codex/legacy-codex" not in manager.models
    assert manager.default_model_id in manager.models
