"""Submission format self-check.

Run it on the folder you are about to zip; it stops at the first violation::

    python -m challenge.check_format --submission_dir <submission folder> \
        --nju_data <nju set>/dataCND \
        --dlaad_data <dlaad set>/dataCND

``--nju_data`` and ``--dlaad_data`` are the ``dataCND`` folders of the two test
sets. The checker reads their ``dataStimN.mat`` files at 128 Hz for the subject
ids and the per-trial sample counts.

Checks
    submission_metadata.yaml  present, three keys filled, value ranges
    nju/ and dlaad/           one test_sample_NNN.csv per subject of the set
    each CSV                  header ``time_index,label,trial``, three integer
                              columns, labels in {0, 1}, trials in ascending
                              order, time_index restarting at 0 per trial and
                              stepping by one. The sampling rate must be 128Hz,
                              regardless of the data you use.
"""

from __future__ import annotations

import argparse
import os
from typing import Dict, List, Tuple, TypedDict

import numpy as np
import scipy.io as sio

from .config import (
    LABEL_COLUMNS,
    LABEL_FS,
    LABEL_TYPES,
    MAX_LOOKAHEAD_MS,
    METADATA_KEYS,
    METADATA_NAME,
    PREDICTION_PREFIX,
    SET_NAMES,
    STIM_PREFIX,
)


class _MetadataInfo(TypedDict):
    """The values ``read_metadata`` returns."""

    label_type: str
    code_public: bool


def read_metadata(path: str) -> _MetadataInfo:
    """Parse the three required keys and reject missing or out-of-range values."""
    if not os.path.exists(path):
        raise ValueError("%s: the submission has no metadata file" % path)

    values: Dict[str, str] = {}
    with open(path, encoding="utf-8-sig") as handle:
        for number, raw in enumerate(handle, 1):
            line = raw.split("#", 1)[0].strip()
            if not line:
                continue
            key, separator, value = line.partition(":")
            if not separator:
                raise ValueError(
                    "%s line %d: expected 'key: value', got %r"
                    % (path, number, raw.strip())
                )
            values[key.strip()] = value.strip()

    nulls = {"", "null", "~", "none", "None"}
    missing = [key for key in METADATA_KEYS if values.get(key, "") in nulls]
    if missing:
        raise ValueError("%s: %s must be filled in" % (path, ", ".join(missing)))

    label_type = values["label_type"].strip("'\"")
    if label_type not in LABEL_TYPES:
        raise ValueError(
            "%s: label_type is %r; expected one of %s"
            % (path, label_type, list(LABEL_TYPES))
        )

    code_public = values["code_public"].strip("'\"").lower()
    if code_public not in ("true", "false"):
        raise ValueError(
            "%s: code_public is %r; expected true or false"
            % (path, values["code_public"])
        )

    raw_lookahead = values["decoder_lookahead_ms"].strip("'\"")
    try:
        lookahead = float(raw_lookahead)
    except ValueError:
        raise ValueError(
            "%s: decoder_lookahead_ms is %r; expected a number of milliseconds"
            % (path, raw_lookahead)
        )
    if not np.isfinite(lookahead) or lookahead < 0 or lookahead > MAX_LOOKAHEAD_MS:
        raise ValueError(
            "%s: decoder_lookahead_ms is %g; it must be finite and in [0, %g]"
            % (path, lookahead, MAX_LOOKAHEAD_MS)
        )

    return {"label_type": label_type, "code_public": code_public == "true"}


def discover_subjects(data_dir: str) -> List[int]:
    """Subject ids from ``dataStim<digits>.mat`` in the set's dataCND folder."""
    if not os.path.isdir(data_dir):
        raise ValueError("%s is not a directory" % data_dir)
    ids = []
    for name in os.listdir(data_dir):
        if name.startswith(STIM_PREFIX) and name.endswith(".mat"):
            stem = name[len(STIM_PREFIX) : -len(".mat")]
            if stem.isdigit():
                ids.append(int(stem))
    if not ids:
        raise ValueError("%s holds no %s<digits>.mat" % (data_dir, STIM_PREFIX))
    return sorted(ids)


def native_trial_lengths(data_dir: str, subject_id: int, set_name: str) -> List[int]:
    """Per-trial sample count at 128 Hz, read from ``dataStim<id>.mat``."""
    path = os.path.join(data_dir, "%s%d.mat" % (STIM_PREFIX, subject_id))
    if not os.path.exists(path):
        raise ValueError(
            "%s is missing; the row-count check needs the native file" % path
        )
    payload = sio.loadmat(path)
    if "stim" not in payload:
        raise ValueError("%s holds no stim struct" % path)
    block = payload["stim"][0, 0]
    if "fs" not in block.dtype.names:
        raise ValueError("%s: stim has no fs field" % path)
    if float(np.asarray(block["fs"]).reshape(-1)[0]) != LABEL_FS:
        raise ValueError(
            "%s: stim.fs is not %g; point --%s_data at the dataCND folder that "
            "holds the native 128 Hz files" % (path, LABEL_FS, set_name)
        )
    data = block["data"]
    return [int(np.asarray(data[0, t]).size) for t in range(data.shape[1])]


def read_label_csv(path: str) -> Tuple[np.ndarray, np.ndarray]:
    """Return ``(label, trial)`` after checking the file format."""
    with open(path, encoding="utf-8-sig") as handle:
        header = handle.readline().strip()
    if header.replace(" ", "").lower() != ",".join(LABEL_COLUMNS):
        raise ValueError("%s: header must be time_index,label,trial" % path)

    raw = np.loadtxt(path, delimiter=",", skiprows=1, dtype=np.float64, ndmin=2)
    if raw.size == 0:
        raise ValueError("%s: the file has a header but no rows" % path)
    if raw.shape[1] != len(LABEL_COLUMNS):
        raise ValueError("%s: every row needs three columns" % path)
    if not np.all(np.isfinite(raw)):
        raise ValueError("%s: the table contains NaN or infinite values" % path)
    if np.any(raw != np.round(raw)):
        raise ValueError("%s: every column must be a whole number" % path)
    table = raw.astype(np.int64)

    label = table[:, 1]
    trial = table[:, 2]
    if np.any(label < 0) or np.any(label > 1):
        raise ValueError("%s: label must be 0 or 1" % path)
    if np.any(np.diff(trial) < 0):
        raise ValueError("%s: trial indices must be in ascending order" % path)

    starts = np.empty(trial.size, dtype=bool)
    starts[0] = True
    starts[1:] = np.diff(trial) != 0
    if np.any(trial[starts] != np.arange(int(starts.sum()))):
        raise ValueError("%s: trials must run 0, 1, 2, ... in ascending order" % path)
    if np.any(table[:, 0][starts] != 0):
        raise ValueError("%s: time_index must restart at 0 in each trial" % path)
    within_trial = ~starts[1:]
    if np.any(np.diff(table[:, 0])[within_trial] != 1):
        raise ValueError("%s: time_index must step by one inside a trial" % path)
    return label, trial


def check_set(set_name: str, submission_set_dir: str, data_dir: str) -> None:
    """Check one set's prediction folder against its dataCND folder."""
    if not os.path.isdir(submission_set_dir):
        raise ValueError("%s/ is missing from the submission" % set_name)

    expected = discover_subjects(data_dir)
    predicted: Dict[int, str] = {}
    for name in os.listdir(submission_set_dir):
        if not (name.startswith(PREDICTION_PREFIX) and name.endswith(".csv")):
            continue
        suffix = name[len(PREDICTION_PREFIX) : -len(".csv")]
        if not suffix.isdigit():
            raise ValueError(
                "%s: file name must be %sNNN.csv with a numeric id"
                % (os.path.join(submission_set_dir, name), PREDICTION_PREFIX)
            )
        subject = int(suffix)
        if subject in predicted:
            raise ValueError(
                "%s: two files claim subject %d" % (submission_set_dir, subject)
            )
        predicted[subject] = os.path.join(submission_set_dir, name)

    missing = sorted(set(expected) - set(predicted))
    if missing:
        raise ValueError("%s/: no prediction for subject(s) %s" % (set_name, missing))
    extra = sorted(set(predicted) - set(expected))
    if extra:
        raise ValueError(
            "%s/: prediction for unknown subject(s) %s" % (set_name, extra)
        )

    for subject in expected:
        label, trial = read_label_csv(predicted[subject])
        lengths = native_trial_lengths(data_dir, subject, set_name)
        if len(trial) > 0 and int(trial[-1]) + 1 != len(lengths):
            raise ValueError(
                "%s subject %d: the CSV holds %d trials, the data holds %d"
                % (set_name, subject, int(trial[-1]) + 1, len(lengths))
            )
        rows_per_trial = np.bincount(trial)
        for trial_index, length in enumerate(lengths):
            got = int(rows_per_trial[trial_index]) if trial_index < len(rows_per_trial) else 0
            if got != length:
                raise ValueError(
                    "%s subject %d trial %d: %d rows, expected %d (the trial's "
                    "sample count at 128 Hz)"
                    % (set_name, subject, trial_index, got, length)
                )


def check_format(submission_dir: str, nju_data: str, dlaad_data: str) -> None:
    """Check the whole submission folder; raise on the first violation."""
    read_metadata(os.path.join(submission_dir, METADATA_NAME))
    data_dirs = {"nju": nju_data, "dlaad": dlaad_data}
    for set_name in SET_NAMES:
        check_set(set_name, os.path.join(submission_dir, set_name), data_dirs[set_name])


def main() -> None:
    parser = argparse.ArgumentParser(
        description="Check a submission folder against the required format."
    )
    parser.add_argument(
        "--submission_dir",
        required=True,
        help="folder holding submission_metadata.yaml, nju/ and dlaad/",
    )
    parser.add_argument("--nju_data", required=True, help="<nju set>/dataCND folder")
    parser.add_argument(
        "--dlaad_data", required=True, help="<dlaad set>/dataCND folder"
    )
    args = parser.parse_args()
    check_format(args.submission_dir, args.nju_data, args.dlaad_data)
    print("format check passed: %s" % args.submission_dir)


if __name__ == "__main__":
    main()
