"""
FDAGSRS_search_substances

Search FDA GSRS (Global Substance Registration System) for substances by name, UNII code, InChIKe...
"""

from typing import Any, Optional, Callable
from ._shared_client import get_shared_client


def FDAGSRS_search_substances(
    query: str,
    substance_class: Optional[str | Any] = None,
    limit: Optional[int | Any] = None,
    *,
    stream_callback: Optional[Callable[[str], None]] = None,
    use_cache: bool = False,
    validate: bool = True,
) -> Any:
    """
    Search FDA GSRS (Global Substance Registration System) for substances by name, UNII code, InChIKe...

    Parameters
    ----------
    query : str
        Search query: drug/chemical name, UNII code, InChIKey, or molecular formula. ...
    substance_class : str | Any
        Filter by substance class. Options: 'chemical', 'protein', 'mixture', 'polyme...
    limit : int | Any
        Maximum number of results to return (1-50, default 10).
    stream_callback : Callable, optional
        Callback for streaming output
    use_cache : bool, default False
        Enable caching
    validate : bool, default True
        Validate parameters

    Returns
    -------
    Any
    """
    # Handle mutable defaults to avoid B006 linting error

    # Strip None values so optional parameters don't trigger schema validation errors
    _args = {
        k: v
        for k, v in {
            "query": query,
            "substance_class": substance_class,
            "limit": limit,
        }.items()
        if v is not None
    }
    return get_shared_client().run_one_function(
        {
            "name": "FDAGSRS_search_substances",
            "arguments": _args,
        },
        stream_callback=stream_callback,
        use_cache=use_cache,
        validate=validate,
    )


__all__ = ["FDAGSRS_search_substances"]
