"""Load and validate adk.config.json from CWD."""

import json
from pathlib import Path

from foremet.config.schema import ADKConfig


CONFIG_FILENAME = "adk.config.json"


def load_config(cwd: Path | None = None) -> ADKConfig:
    """Read adk.config.json from cwd (default current directory), validate, return ADKConfig."""
    base = cwd or Path.cwd()
    path = base / CONFIG_FILENAME
    if not path.exists():
        raise FileNotFoundError(
            f"{CONFIG_FILENAME} not found in {base}. Run 'foremet init' first."
        )
    raw = path.read_text()
    try:
        data = json.loads(raw)
    except json.JSONDecodeError as e:
        raise ValueError(f"Invalid JSON in {CONFIG_FILENAME}: {e}") from e
    return ADKConfig.model_validate(data)
