"""Read OMRChecker output without altering OMRChecker itself."""

from __future__ import annotations

import csv
from pathlib import Path


def read_omr_results(output: Path) -> dict[str, dict[str, str]]:
    rows: dict[str, dict[str, str]] = {}
    groups = (
        ("Results/*.csv", "accepted"),
        ("Manual/MultiMarkedFiles.csv", "manual_review"),
        ("Manual/ErrorFiles.csv", "error"),
    )
    for pattern, route in groups:
        for csv_path in output.glob(pattern):
            with csv_path.open(newline="") as handle:
                for row in csv.DictReader(handle):
                    row["route"] = route
                    rows[row["file_id"]] = row
    return rows


def has_valid_controls(row: dict[str, str] | None) -> bool:
    """The fixed B/C marks validate orientation independently of handwriting."""
    return bool(row and row.get("control_left") == "B" and row.get("control_right") == "C")
