"""
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 pathlib import Path

import pytest

from config.loader import load_toml_config


class TestTOMLLoader:
    """Test TOML configuration file loader (AC #1, #3)."""

    def test_load_valid_toml(self, tmp_path):
        """Test loading a valid TOML file."""
        toml_file = tmp_path / "config.toml"
        toml_file.write_text(
            """
amqp_host = "localhost"
amqp_port = 5672
amqp_user = "guest"
amqp_password = "guest"
amqp_vhost = "/"
"""
        )

        config = load_toml_config(toml_file)
        assert config["amqp_host"] == "localhost"
        assert config["amqp_port"] == 5672
        assert config["amqp_user"] == "guest"
        assert config["amqp_password"] == "guest"
        assert config["amqp_vhost"] == "/"

    def test_load_nested_toml(self, tmp_path):
        """Test loading TOML with nested structure."""
        toml_file = tmp_path / "config.toml"
        toml_file.write_text(
            """
[amqp]
host = "localhost"
port = 5672

[http]
host = "localhost"
port = 15672
"""
        )

        config = load_toml_config(toml_file)
        assert config["amqp"]["host"] == "localhost"
        assert config["amqp"]["port"] == 5672
        assert config["http"]["host"] == "localhost"
        assert config["http"]["port"] == 15672

    def test_load_missing_file(self, tmp_path):
        """Test loading non-existent file returns empty dict."""
        non_existent = tmp_path / "missing.toml"
        config = load_toml_config(non_existent)
        assert config == {}

    def test_load_invalid_toml(self, tmp_path):
        """Test loading malformed TOML raises ValueError."""
        toml_file = tmp_path / "invalid.toml"
        toml_file.write_text(
            """
amqp_host = "localhost
amqp_port = invalid
"""
        )

        with pytest.raises(ValueError) as exc_info:
            load_toml_config(toml_file)
        assert "Invalid TOML file" in str(exc_info.value)

    def test_load_empty_toml(self, tmp_path):
        """Test loading empty TOML file."""
        toml_file = tmp_path / "empty.toml"
        toml_file.write_text("")

        config = load_toml_config(toml_file)
        assert config == {}

    def test_load_toml_with_comments(self, tmp_path):
        """Test loading TOML with comments."""
        toml_file = tmp_path / "config.toml"
        toml_file.write_text(
            """
# AMQP Configuration
amqp_host = "localhost"  # RabbitMQ host
amqp_port = 5672        # Default AMQP port

# HTTP Configuration
http_host = "localhost"
"""
        )

        config = load_toml_config(toml_file)
        assert config["amqp_host"] == "localhost"
        assert config["amqp_port"] == 5672
        assert config["http_host"] == "localhost"

    def test_load_toml_with_boolean_values(self, tmp_path):
        """Test loading TOML with boolean values."""
        toml_file = tmp_path / "config.toml"
        toml_file.write_text(
            """
amqp_use_tls = true
amqp_verify_cert = false
"""
        )

        config = load_toml_config(toml_file)
        assert config["amqp_use_tls"] is True
        assert config["amqp_verify_cert"] is False

    def test_load_toml_with_arrays(self, tmp_path):
        """Test loading TOML with array values."""
        toml_file = tmp_path / "config.toml"
        toml_file.write_text(
            """
allowed_hosts = ["localhost", "rabbitmq.local", "*.example.com"]
ports = [5672, 5671]
"""
        )

        config = load_toml_config(toml_file)
        assert config["allowed_hosts"] == [
            "localhost",
            "rabbitmq.local",
            "*.example.com",
        ]
        assert config["ports"] == [5672, 5671]
