# SPDX-FileCopyrightText: 2025 OmniNode.ai Inc.
# SPDX-License-Identifier: MIT

"""
Integration tests for check-database-health skill.

Tests:
- Connection pool statistics
- Query performance metrics
- Recent errors retrieval
- Log lines parameter

Created: 2025-11-20
"""

from pathlib import Path
from unittest.mock import patch

import pytest

# Import load_skill_module from conftest
conftest_path = Path(__file__).parent / "conftest.py"
import importlib.util

spec = importlib.util.spec_from_file_location("conftest", conftest_path)
conftest = importlib.util.module_from_spec(spec)
spec.loader.exec_module(conftest)
load_skill_module = conftest.load_skill_module


# Load the check-database-health execute module
execute = load_skill_module("check-database-health")
main = execute.main
validate_table_name = execute.validate_table_name


class TestCheckDatabaseHealth:
    """Test check-database-health skill."""

    @pytest.mark.skip(
        reason="Mock data structure mismatch - requires aligning mock with execute.py expectations"
    )
    def test_connection_pool_stats(self):
        """Test connection pool statistics query."""
        with (
            patch.object(execute, "execute_query") as mock_query,
            patch("sys.argv", ["execute.py"]),
        ):
            mock_query.side_effect = [
                {
                    "success": True,
                    "rows": [
                        {
                            "total_connections": 25,
                            "active_connections": 12,
                            "idle_connections": 13,
                            "max_connections": 100,
                        }
                    ],
                },
                {"success": True, "rows": []},  # query performance
                {"success": True, "rows": []},  # recent errors
            ]

            exit_code = main()

            assert exit_code == 0
            assert mock_query.call_count >= 1

    @pytest.mark.skip(
        reason="Mock data structure mismatch - requires aligning mock with execute.py expectations"
    )
    def test_query_performance_metrics(self):
        """Test query performance metrics."""
        with (
            patch.object(execute, "execute_query") as mock_query,
            patch("sys.argv", ["execute.py"]),
        ):
            mock_query.side_effect = [
                {"success": True, "rows": []},  # connection pool
                {
                    "success": True,
                    "rows": [
                        {
                            "avg_query_time_ms": 45.2,
                            "max_query_time_ms": 250.5,
                            "slow_queries": 12,
                        }
                    ],
                },
                {"success": True, "rows": []},  # recent errors
            ]

            exit_code = main()

            assert exit_code == 0

    @pytest.mark.skip(
        reason="check-database-health script does not support --log-lines parameter (only --tables and --include-sizes)"
    )
    def test_recent_errors_retrieval(self):
        """Test recent error retrieval with log lines limit."""
        with (
            patch.object(execute, "execute_query") as mock_query,
            patch("sys.argv", ["execute.py", "--log-lines", "50"]),
        ):
            mock_query.side_effect = [
                {"success": True, "rows": []},  # connection pool
                {"success": True, "rows": []},  # query performance
                {
                    "success": True,
                    "rows": [
                        {
                            "error_time": "2025-11-20 14:30:00",
                            "error_message": "Connection timeout",
                            "query": "SELECT * FROM agent_routing_decisions",
                        },
                    ],
                },
            ]

            exit_code = main()

            assert exit_code == 0
            # Verify log-lines parameter was used
            # Handle both positional and keyword arguments
            last_call = mock_query.call_args_list[-1]

            # Check if params was passed as keyword argument
            if "params" in last_call.kwargs:
                call_params = last_call.kwargs["params"]
            # Check if params was passed as positional argument (2nd arg)
            elif len(last_call.args) >= 2:
                call_params = last_call.args[1]
            else:
                call_params = None

            assert call_params is not None, (
                "Expected 'params' parameter not found in call (checked both positional and keyword arguments)"
            )
            assert 50 in call_params, (
                "Expected log_lines value (50) not found in params"
            )

    def test_validate_log_lines(self):
        """Test log_lines parameter validation."""
        # Note: validate_log_lines is in check-service-status, not check-database-health
        # This test is skipped until the function is added to check-database-health
        # or moved to the correct test file
        pytest.skip("validate_log_lines not implemented in check-database-health")

    def test_database_unavailable(self):
        """Test handling when database is unavailable."""
        with (
            patch.object(execute, "execute_query") as mock_query,
            patch("sys.argv", ["execute.py"]),
        ):
            mock_query.return_value = {
                "success": False,
                "error": "Connection refused",
                "rows": [],
            }

            exit_code = main()

            # Should handle error gracefully and return non-zero exit code
            assert exit_code == 1, (
                "Expected exit code 1 for database connection failure"
            )
