"""
Practice: the parts of Lab 10's evaluation platform that keep a sharded run honest
(vox_eval/datasets.py, store.py, runner.py and compare.py, cut down to plain Python).

Fill in the five TODOs, then run the tests:
    pytest test_platform_practice.py        (locally)
or press "Run tests" on the chapter page.

A row is one clip's result, as a dict: {"clip_id": ..., "attempt": ..., "errors": ..., "words": ...}.
"""
import hashlib


def shard_of(clip_id: str, n_shards: int) -> int:
    """Which shard (0 to n_shards - 1) a clip goes in, from its id alone:
    the first 8 hex characters of the SHA-256 of the id, read as a number, modulo n_shards.

    Because nothing else goes in, a retried shard holds exactly the same clips.

    TODO 1
    """
    raise NotImplementedError


def corpus_wer(rows: list[dict]) -> float:
    """Word error rate in percent, as a ratio of sums: all errors divided by all words,
    times 100. Never an average of per-clip or per-shard WERs. 0.0 if there are no words.

    TODO 2
    """
    raise NotImplementedError


def merge_keyed(rows: list[dict]) -> dict[str, dict]:
    """The results store: one slot per clip. Return {clip_id: row}.

    Rows arrive in the order the workers sent them. When a clip arrives again (a crashed
    attempt sent it, then the retry sent it again), the later row replaces the earlier one,
    like INSERT OR REPLACE on PRIMARY KEY (run_id, clip_id).

    TODO 3
    """
    raise NotImplementedError


def completeness(stored_ids, manifest_ids: list[str]) -> tuple[str, list[str]]:
    """Check the stored clips against the benchmark's list of clips, before trusting any score.

    Return (status, missing): missing is the manifest ids with no stored result, in manifest
    order; status is "complete" if nothing is missing, otherwise "incomplete".

    TODO 4
    """
    raise NotImplementedError


def comparable(a: dict, b: dict) -> list[str]:
    """The reasons two runs can't be compared; an empty list means they can.

    Each run is a dict with "run_id", "status", "dataset", "fingerprint" and "normalizer".
    Check, in this order, with exactly these messages (the tool's own):
      - for each run, a then b, whose status isn't "complete":
            "run <run_id> is <status>"
      - different fingerprints:
            "different dataset versions (<a dataset> <a fingerprint> vs <b dataset> <b fingerprint>)"
      - different normalizers:
            "different normalizers (<a normalizer> vs <b normalizer>)"

    TODO 5
    """
    raise NotImplementedError
