"""Model precedence resolution logic.

Pure precedence helper shared by shell modules.
No I/O imports.
"""

from deal import post, pre


@pre(
    lambda cli_model, env_model, config_model, persona_model: all(
        value is None or len(value) > 0
        for value in (cli_model, env_model, config_model, persona_model)
    )
)
@post(lambda result: result is None or len(result) > 0)
def resolve_model_precedence(
    cli_model: str | None,
    env_model: str | None,
    config_model: str | None,
    persona_model: str | None,
) -> str | None:
    """Resolve model by precedence: CLI > env > config > persona.

    Spec source: vectl step
    ``post-review-fixes.move-resolve-model-precedence-to-core-with-contracts``
    requires moving this pure precedence helper to ``core/`` with contracts.

    Args:
        cli_model: CLI override model.
        env_model: Model from ``ANIMA_DEFAULT_MODEL``.
        config_model: Model from global config defaults.
        persona_model: Model from ``PersonaSpec``.

    Returns:
        The highest-priority non-empty model string, or None.

    Doctests:
        >>> resolve_model_precedence(
        ...     cli_model="cli",
        ...     env_model="env",
        ...     config_model="config",
        ...     persona_model="persona",
        ... )
        'cli'
        >>> resolve_model_precedence(
        ...     cli_model=None,
        ...     env_model="env",
        ...     config_model="config",
        ...     persona_model="persona",
        ... )
        'env'
        >>> resolve_model_precedence(
        ...     cli_model=None,
        ...     env_model=None,
        ...     config_model="config",
        ...     persona_model="persona",
        ... )
        'config'
        >>> resolve_model_precedence(
        ...     cli_model=None,
        ...     env_model=None,
        ...     config_model=None,
        ...     persona_model="persona",
        ... )
        'persona'
        >>> resolve_model_precedence(
        ...     cli_model=None,
        ...     env_model=None,
        ...     config_model=None,
        ...     persona_model=None,
        ... ) is None
        True
        >>> resolve_model_precedence(
        ...     cli_model="",
        ...     env_model="env",
        ...     config_model=None,
        ...     persona_model=None,
        ... )  # doctest: +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
        ...
        PreContractError: ...
    """
    return cli_model or env_model or config_model or persona_model


__all__ = ["resolve_model_precedence"]
