"""Single source of truth for EB1 model-to-plan mapping.

All callers that need to resolve a model name to an :class:`Eb1Plan` should
use :func:`select_plan` rather than maintaining their own mapping tables.
"""

from __future__ import annotations

from .eb1 import (
    DEFAULT_CODEX_HIGH_PLAN,
    DEFAULT_CODEX_PLAN,
    DEFAULT_EB2_CODEX_PLAN,
    DEFAULT_FAST_PLAN,
    DEFAULT_PLAN,
    DEFAULT_PRO_HIGH_PLAN,
    DEFAULT_PRO_PLAN,
    Eb1Plan,
)


def select_plan(model: str) -> Eb1Plan:
    """Map a model identifier to its EB1 execution plan.

    Handles optional ``takumi/`` or ``eb1/`` prefixes and normalises case
    before matching.  Falls back to :data:`DEFAULT_PLAN` for unrecognised
    models.

    Args:
        model: Model identifier (e.g. ``"eb1-preview"``, ``"eb1/eb1-codex"``).

    Returns:
        The :class:`Eb1Plan` appropriate for the given model.
    """
    suffix = model.lower()
    if "/" in suffix:
        suffix = suffix.split("/", 1)[1]

    # Codex variants (check high before base to avoid prefix collision).
    if suffix.startswith("eb1-codex-high"):
        return DEFAULT_CODEX_HIGH_PLAN
    if suffix.startswith("eb1-codex"):
        return DEFAULT_CODEX_PLAN
    if suffix.startswith("eb2-codex"):
        return DEFAULT_EB2_CODEX_PLAN

    # Pro variants (check pro-high before pro to avoid prefix collision).
    if suffix.startswith("eb1-pro-high"):
        return DEFAULT_PRO_HIGH_PLAN
    if suffix.startswith("eb1-a"):
        return DEFAULT_PRO_PLAN
    if suffix.startswith("eb1-pro"):
        return DEFAULT_PRO_PLAN

    # Fast variants.
    if suffix.startswith("eb1-fast") or suffix.startswith("eb1-swe-fast"):
        return DEFAULT_FAST_PLAN

    # Frontier = max quality.
    if suffix.startswith("eb1-frontier"):
        return DEFAULT_PRO_HIGH_PLAN

    # Delta / conductor = codex plan.
    if suffix.startswith("eb1-delta"):
        return DEFAULT_CODEX_PLAN

    # SWE pro variant.
    if suffix.startswith("eb1-swe-pro"):
        return DEFAULT_PRO_PLAN

    # SWE base and eb1-preview (default plan).
    if suffix.startswith("eb1-swe"):
        return DEFAULT_PLAN

    return DEFAULT_PLAN
