from __future__ import annotations

from pathlib import Path
from typing import Any, Dict, List

import yaml


def load_member_registry(path: str | Path) -> List[Dict[str, Any]]:
    path = Path(path)
    with path.open('r', encoding='utf-8') as f:
        data = yaml.safe_load(f) or {}
    members = data.get('members', [])
    if not isinstance(members, list) or not members:
        raise ValueError(f'No members found in {path}')
    required = {'name', 'method', 'predictor_set', 'spatial_mode'}
    for m in members:
        missing = required - set(m)
        if missing:
            raise ValueError(f'Member {m!r} missing keys: {sorted(missing)}')
    return members


def operational_candidates(members: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
    return [m for m in members if bool(m.get('use_for_operational_candidate', True))]
