"""Pure hardware/settings-based warning checks.

These functions have zero dependencies on Flask or SQLAlchemy —
they take primitive values and return warning dicts (or None).
"""

from typing import Optional

LOCAL_PROVIDERS = frozenset({"ollama", "llamacpp", "lmstudio"})


def check_high_context(
    provider: str, local_context: int, dismissed: bool
) -> Optional[dict]:
    """Return a high_context warning dict if context exceeds 8192 for a local provider."""
    if provider not in LOCAL_PROVIDERS:
        return None
    if local_context <= 8192:
        return None
    if dismissed:
        return None

    return {
        "type": "high_context",
        "icon": "⚠️",
        "title": "High Context Warning",
        "message": (
            f"Context size ({local_context:,} tokens) may cause memory issues "
            f"with {provider}. Increase VRAM or reduce context size if you "
            f"experience slowdowns."
        ),
        "dismissKey": "app.warnings.dismiss_high_context",
    }


def check_model_mismatch(
    provider: str, model: str, local_context: int, dismissed: bool
) -> Optional[dict]:
    """Return a model_mismatch warning dict for large models with high context."""
    if not model:
        return None
    if provider not in LOCAL_PROVIDERS:
        return None
    if "70b" not in model.lower():
        return None
    if local_context <= 8192:
        return None
    if dismissed:
        return None

    return {
        "type": "model_mismatch",
        "icon": "🧠",
        "title": "Model & Context Warning",
        "message": (
            f"Large model ({model}) with high context ({local_context:,}) "
            f"may exceed VRAM. Consider reducing context size or upgrading "
            f"GPU memory."
        ),
        "dismissKey": "app.warnings.dismiss_model_mismatch",
    }


def check_legacy_server_config(dismissed: bool) -> Optional[dict]:
    """Return a warning only if server_config.json has non-default values."""
    from ..server_config import has_legacy_customizations

    if dismissed:
        return None
    if not has_legacy_customizations():
        return None
    return {
        "type": "legacy_server_config",
        "icon": "ℹ️",
        "title": "server_config.json Detected",
        "message": (
            "A server_config.json file was found with non-default settings. "
            "Environment variables are the preferred configuration method. "
            "See the documentation for migration details."
        ),
        "dismissKey": "app.warnings.dismiss_legacy_config",
    }
