from __future__ import annotations

from backend.data.contracts import ProviderResult


class DataQualityError(RuntimeError):
    pass


MINIMUM_COVERAGE = {
    "calendar": 1.0,
    "entities": 0.98,
    "daily_chart": 1.0,
    "minute_chart": 1.0,
}


def require_quality(dataset: str, result: ProviderResult) -> ProviderResult:
    minimum = MINIMUM_COVERAGE.get(dataset, 1.0)
    if not result.rows:
        raise DataQualityError(f"{dataset} returned no real observations")
    if result.metadata.coverage < minimum:
        raise DataQualityError(
            f"{dataset} coverage {result.metadata.coverage:.3f} is below {minimum:.3f}"
        )
    return result
