"""Configuration with three layers: default < file < environment."""

import os
import tomllib  # noqa: F401  — the file layer is TOML
from collections.abc import Mapping
from dataclasses import dataclass, fields  # noqa: F401
from pathlib import Path


@dataclass(frozen=True)
class Config:
    """Provided. Frozen: once loaded, nothing in the process can change it under you."""

    model_name: str
    timeout_s: float
    log_level: str


# Provided. The lowest layer: what you get when neither the file nor the environment says.
DEFAULTS: Mapping[str, object] = {
    "model_name": "tiny-local",
    "timeout_s": 30.0,
    "log_level": "INFO",
}

# Provided. The environment layer reads ASKCLI_MODEL_NAME, ASKCLI_TIMEOUT_S, ASKCLI_LOG_LEVEL.
ENV_PREFIX = "ASKCLI_"


def load_config(
    defaults: Mapping[str, object],
    path: Path | None,
    env: Mapping[str, str] = os.environ,
) -> Config:
    """Merge the three layers — env > file > default — into one frozen Config.

    ``path`` is a TOML file with flat keys named like the fields (``model_name = "gpt-large"``);
    ``None`` or a path that does not exist means "no file layer". A key in the file that is not
    a field raises ``ValueError`` naming it (a typo must not be silently ignored). Environment
    values are strings: convert each to its field's type (``"5"`` -> ``5.0`` for ``timeout_s``).
    """
    ...
