from dataclasses import dataclass, field, fields
from typing import Any

from tickit_devices.eiger.eiger_schema import ro_str, ro_uint


def stream_status_keys() -> list[str]:
    # Eiger does not report error as a key
    return ["dropped", "state"]


@dataclass
class StreamStatus:
    """Eiger stream status taken from the API spec."""

    state: str = field(
        default="ready",
        metadata=ro_str(allowed_values=["disabled", "ready", "acquire", "error"]),
    )
    error: list[str] = field(default_factory=lambda: [], metadata=ro_str())
    dropped: int = field(default=0, metadata=ro_uint())

    keys: list[str] = field(default_factory=stream_status_keys)

    def __getitem__(self, key: str) -> Any:  # noqa: D105
        for field_ in fields(self):
            if field_.name == key:
                return {"value": vars(self)[field_.name], "metadata": field_.metadata}
        raise ValueError(f"No field with name {key}")
