"""View mode types for TUI dashboard."""

from __future__ import annotations

from dataclasses import dataclass
from enum import Enum, auto


class ViewMode(Enum):
    """Available view modes for the dashboard."""

    PLANS = auto()
    LEARN = auto()
    OBJECTIVES = auto()


@dataclass(frozen=True)
class ViewConfig:
    """Configuration for a specific view mode.

    Attributes:
        mode: The view mode this config describes
        display_name: Human-readable name for the view
        labels: GitHub labels to use when fetching data
        key_hint: Key binding hint (e.g., "1", "2", "3")
        exclude_labels: Labels to exclude from results (client-side filtering
            applied before expensive enrichment). Empty tuple means no exclusion.
    """

    mode: ViewMode
    display_name: str
    labels: tuple[str, ...]
    key_hint: str
    exclude_labels: tuple[str, ...]


PLANS_VIEW = ViewConfig(
    mode=ViewMode.PLANS,
    display_name="Plans",
    labels=("erk-plan",),
    key_hint="1",
    exclude_labels=("erk-learn",),
)

LEARN_VIEW = ViewConfig(
    mode=ViewMode.LEARN,
    display_name="Learn",
    labels=("erk-learn",),
    key_hint="2",
    exclude_labels=(),
)

OBJECTIVES_VIEW = ViewConfig(
    mode=ViewMode.OBJECTIVES,
    display_name="Objectives",
    labels=("erk-objective",),
    key_hint="3",
    exclude_labels=(),
)

VIEW_CONFIGS: tuple[ViewConfig, ...] = (PLANS_VIEW, LEARN_VIEW, OBJECTIVES_VIEW)

_VIEW_CONFIG_BY_MODE: dict[ViewMode, ViewConfig] = {config.mode: config for config in VIEW_CONFIGS}


def get_view_config(mode: ViewMode) -> ViewConfig:
    """Look up the ViewConfig for a given mode.

    Args:
        mode: The view mode to look up

    Returns:
        The corresponding ViewConfig
    """
    return _VIEW_CONFIG_BY_MODE[mode]


def get_next_view_mode(current: ViewMode) -> ViewMode:
    """Get the next view mode by cycling forward through VIEW_CONFIGS.

    Args:
        current: The currently active view mode

    Returns:
        The next view mode, wrapping around to the first
    """
    modes = [c.mode for c in VIEW_CONFIGS]
    if current not in modes:
        return current
    idx = modes.index(current)
    return modes[(idx + 1) % len(modes)]


def get_previous_view_mode(current: ViewMode) -> ViewMode:
    """Get the previous view mode by cycling backward through VIEW_CONFIGS.

    Args:
        current: The currently active view mode

    Returns:
        The previous view mode, wrapping around to the last
    """
    modes = [c.mode for c in VIEW_CONFIGS]
    if current not in modes:
        return current
    idx = modes.index(current)
    return modes[(idx - 1) % len(modes)]
