from __future__ import annotations

"""
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/>.
"""

"""Table formatter utilities for CLI output."""

from collections.abc import Iterable, Mapping, Sequence
from typing import Any

from tabulate import tabulate


def _stringify(value: Any) -> str:
    if value is None:
        return ""
    if isinstance(value, bool):
        return "yes" if value else "no"
    return str(value)


def format_table(rows: Iterable[Mapping[str, Any]], headers: Sequence[str]) -> str:
    data = [[_stringify(row.get(header)) for header in headers] for row in rows]
    return tabulate(data, headers=headers, tablefmt="github")


__all__ = ["format_table"]
