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

"""CLI entry point for MCP server."""

import argparse
import asyncio
import sys
from typing import Any

from mcp_server.server import main


def parse_args() -> dict[str, Any]:
    """Parse CLI arguments for configuration overrides (AC #1 Story 2.1)."""
    parser = argparse.ArgumentParser(description="RabbitMQ MCP Server")

    # Configuration file
    parser.add_argument(
        "--config-file", type=str, help="Path to TOML configuration file"
    )

    # AMQP connection parameters
    parser.add_argument("--amqp-host", type=str, help="RabbitMQ AMQP host")
    parser.add_argument("--amqp-port", type=int, help="RabbitMQ AMQP port")
    parser.add_argument("--amqp-user", type=str, help="RabbitMQ AMQP username")
    parser.add_argument("--amqp-password", type=str, help="RabbitMQ AMQP password")
    parser.add_argument("--amqp-vhost", type=str, help="RabbitMQ AMQP virtual host")
    parser.add_argument("--amqp-use-tls", type=bool, help="Enable TLS for AMQP")

    # HTTP Management API parameters
    parser.add_argument(
        "--http-host", type=str, help="RabbitMQ HTTP Management API host"
    )
    parser.add_argument(
        "--http-port", type=int, help="RabbitMQ HTTP Management API port"
    )
    parser.add_argument("--http-user", type=str, help="RabbitMQ HTTP username")
    parser.add_argument("--http-password", type=str, help="RabbitMQ HTTP password")
    parser.add_argument("--http-use-tls", type=bool, help="Enable TLS for HTTP")

    # General parameters
    parser.add_argument(
        "--log-level",
        type=str,
        choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
        help="Logging level",
    )

    args = parser.parse_args()

    # Convert argparse Namespace to dict, filtering out None values
    return {k: v for k, v in vars(args).items() if v is not None}


if __name__ == "__main__":
    try:
        cli_args = parse_args()
        asyncio.run(main(cli_args))
    except KeyboardInterrupt:
        sys.exit(0)
    except Exception:
        sys.exit(1)
