"""
Copyright (C) 2025 Luciano Guerche

This file is part of rabbitmq-mcp-server.

rabbitmq-mcp-server is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

rabbitmq-mcp-server is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with rabbitmq-mcp-server. If not, see <https://www.gnu.org/licenses/>.
"""

from __future__ import annotations

from pathlib import Path
from typing import Any

try:
    import tomli as tomllib
except ImportError:
    raise ImportError(
        "tomli package required for Python < 3.11. Install with: pip install tomli"
    )


def load_toml_config(path: Path) -> dict[str, Any]:
    """
    Load TOML configuration file.

    Args:
        path: Path to TOML configuration file

    Returns:
        Dictionary with configuration values, or empty dict if file doesn't exist

    Raises:
        ValueError: If TOML file is malformed
    """
    if not path.exists():
        return {}

    try:
        with open(path, "rb") as f:
            config: dict[str, Any] = tomllib.load(f)
        return config
    except tomllib.TOMLDecodeError as e:
        raise ValueError(f"Invalid TOML file '{path}': {e}") from e
