"""Build graph from kconfig.json files generated by Zephyr Documentation System."""

import logging
from pathlib import Path

import httpx

from ._models import KConfigDoc

_LOGGER = logging.getLogger(__name__)


def from_json(json_path: Path) -> KConfigDoc:
    """Builds the graph from a JSON file containing KConfig items."""
    return KConfigDoc.model_validate_json(json_path.read_text())


def from_release(release: str) -> KConfigDoc:
    """Build the graph from specific Zephyr release by loading the corresponding kconfig.json file."""
    _LOGGER.info(f"Fetching kconfig.json for release {release} ...")

    url = f"https://docs.zephyrproject.org/{release}/kconfig.json"
    response = httpx.get(url)
    response.raise_for_status()  # Ensure we got a successful response
    return KConfigDoc.model_validate_json(response.text)
