"""Helpers for writing runtime configs and preprocessing bundles."""

from __future__ import annotations

import json
from pathlib import Path

from python_app.models.run_config_model import RunConfigModel, parse_combos_from_text
from python_app.storage.npz_store import NpzStore


class ConfigWriter:
    """Write runtime artifacts consumed by C++ processes."""

    def __init__(self, runtime_dir: Path) -> None:
        """Create writer rooted at runtime directory."""
        self._runtime_dir = runtime_dir
        self._runtime_dir.mkdir(parents=True, exist_ok=True)

    def prepare_bundles(
        self,
        store: NpzStore,
        radar_key: str,
        calibration_set: str,
        reference_set: str,
    ) -> tuple[Path, Path]:
        """Export calibration/reference sets into binary bundles for preprocessor."""
        calibration_bundle = self._runtime_dir / "calibration_bundle.bin"
        reference_bundle = self._runtime_dir / "reference_bundle.bin"

        store.export_set_bundle("calibration", radar_key, calibration_set, calibration_bundle)
        store.export_set_bundle("reference", radar_key, reference_set, reference_bundle)
        return calibration_bundle, reference_bundle

    def write(self, config: RunConfigModel, output_path: Path) -> Path:
        """Write run configuration JSON file."""
        output_path.parent.mkdir(parents=True, exist_ok=True)
        output_path.write_text(json.dumps(config.to_dict(), indent=2), encoding="utf-8")
        return output_path


__all__ = ["ConfigWriter", "parse_combos_from_text"]
