"""
Test docstring completeness for FastMCP tools.

This module verifies that the search_web function has complete
and properly formatted documentation.
"""

import inspect
import pytest
import os
import sys

# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


def test_search_web_has_docstring():
    """Verify that search_web has a docstring."""
    try:
        from server import search_web

        doc = inspect.getdoc(search_web)
        assert doc is not None, "search_web should have a docstring"
        assert len(doc) > 0, "docstring should not be empty"

    except ImportError:
        pytest.skip("server.py not yet created")
    except AttributeError:
        pytest.skip("search_web function not yet defined in server.py")


def test_docstring_length_sufficient():
    """Verify that the docstring is sufficiently detailed (>50 chars)."""
    try:
        from server import search_web

        doc = inspect.getdoc(search_web)
        assert doc is not None, "search_web should have a docstring"
        assert len(doc) > 50, "docstring should be more than 50 characters"

    except ImportError:
        pytest.skip("server.py not yet created")
    except AttributeError:
        pytest.skip("search_web function not yet defined in server.py")


def test_docstring_has_args_section():
    """Verify that the docstring contains an Args section."""
    try:
        from server import search_web

        doc = inspect.getdoc(search_web)
        assert doc is not None, "search_web should have a docstring"

        # Check for Args section (case-insensitive)
        has_args = ('Args:' in doc or
                    'Arguments:' in doc or
                    'Parameters:' in doc)

        assert has_args, "docstring should contain an Args/Arguments/Parameters section"

    except ImportError:
        pytest.skip("server.py not yet created")
    except AttributeError:
        pytest.skip("search_web function not yet defined in server.py")


def test_docstring_has_returns_section():
    """Verify that the docstring contains a Returns section."""
    try:
        from server import search_web

        doc = inspect.getdoc(search_web)
        assert doc is not None, "search_web should have a docstring"

        # Check for Returns section
        has_returns = ('Returns:' in doc or
                       'Return:' in doc)

        assert has_returns, "docstring should contain a Returns/Return section"

    except ImportError:
        pytest.skip("server.py not yet created")
    except AttributeError:
        pytest.skip("search_web function not yet defined in server.py")


def test_docstring_has_examples_section():
    """Verify that the docstring contains an Examples section."""
    try:
        from server import search_web

        doc = inspect.getdoc(search_web)
        assert doc is not None, "search_web should have a docstring"

        # Check for Examples section
        has_examples = ('Examples:' in doc or
                        'Example:' in doc)

        assert has_examples, "docstring should contain an Examples/Example section"

    except ImportError:
        pytest.skip("server.py not yet created")
    except AttributeError:
        pytest.skip("search_web function not yet defined in server.py")


def test_all_parameters_documented():
    """Verify that all parameters are documented in the Args section."""
    try:
        from server import search_web

        doc = inspect.getdoc(search_web)
        sig = inspect.signature(search_web)

        # Get parameter names
        param_names = list(sig.parameters.keys())

        # For each parameter, check if it's mentioned in the docstring
        # This is a basic check - in real implementation we'd parse the Args section
        for param_name in param_names:
            # Check if parameter name appears in docstring
            # (this is a simple check, proper parsing would be more complex)
            assert param_name in doc or param_name.replace('_', '-') in doc, \
                f"parameter '{param_name}' should be documented in the docstring"

    except ImportError:
        pytest.skip("server.py not yet created")
    except AttributeError:
        pytest.skip("search_web function not yet defined in server.py")
