import yaml
from pathlib import Path


def _read_bluesky_kafka_config_file(config_file_path):
    """Read a YAML file of Kafka producer configuration details.

    The file must have three top-level entries as shown:
    ---
        abort_run_on_kafka_exception: true
        bootstrap_servers:
            - kafka1:9092
            - kafka2:9092
        runengine_producer_config:
            acks: 0
            message.timeout.ms: 3000
            compression.codec: snappy

    Parameters
    ----------
    config_file_path: str
        path to the YAML file of Kafka producer configuration details

    Returns
    -------
    dict of configuration details
    """

    # read the Kafka Producer configuration details
    if Path(config_file_path).exists():
        with open(config_file_path) as f:
            bluesky_kafka_config = yaml.safe_load(f)
    else:
        raise FileNotFoundError(config_file_path)

    required_sections = ("abort_run_on_kafka_exception", "bootstrap_servers", "runengine_producer_config")
    missing_required_sections = [
        required_section
        for required_section
        in required_sections
        if required_section not in bluesky_kafka_config
    ]

    if missing_required_sections:
        raise Exception(
            f"Bluesky Kafka configuration file '{config_file_path}' is missing required section(s) `{missing_required_sections}`"
        )

    return bluesky_kafka_config