"""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) requires sufficient VRAM. "
            f"This is recommended for the langgraph-agent strategy. "
            f"If you experience slowdowns, reduce context size in settings "
            f"and switch to the source-based strategy instead. "
            f"Tip: check the metrics page in each research history entry "
            f"to monitor actual token usage and VRAM consumption."
        ),
        "dismissKey": "app.warnings.dismiss_high_context",
        "actionUrl": "/metrics/context-overflow",
        "actionLabel": "View context metrics",
    }


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",
        # Shares the high_context destination on purpose: this warning fires on
        # large-model + high-context together, and the actionable lever is
        # reducing context size. The "Token Usage & Context Analytics" page is
        # where the user sees actual token/context usage to make that call —
        # there is no separate GPU/VRAM analytics page.
        "actionUrl": "/metrics/context-overflow",
        "actionLabel": "View context metrics",
    }


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",
    }
