"""Motion-related simple types and JSON helpers.

Decision: MotionId is an alias for str for simplicity.
"""

from dataclasses import dataclass, asdict
from typing import List
import json

MotionId = str
Embedding = List[float]


@dataclass
class SimilarityNeighbor:
    motion_id: MotionId
    score: float


def to_json(neighbors: List[SimilarityNeighbor]) -> str:
    """Serialize a list of SimilarityNeighbor to a JSON string.

    The format is a JSON list of objects with keys 'motion_id' and 'score'.
    """
    list_of_dicts = [asdict(n) for n in neighbors]
    return json.dumps(list_of_dicts)


def from_json(json_str: str) -> List[SimilarityNeighbor]:
    """Deserialize a JSON string (list of dicts) into SimilarityNeighbor list."""
    parsed = json.loads(json_str)
    return [
        SimilarityNeighbor(motion_id=item["motion_id"], score=float(item["score"]))
        for item in parsed
    ]
