"""
CSSL Bridge - Type Stubs for Python API
"""

import threading
from typing import Any, List, Optional, Callable, Dict


class CSSLModule:
    """
    A callable CSSL module that executes code with arguments.

    Created via CSSL.module() - the code is executed each time the module is called,
    with arguments accessible via parameter.get(index).
    """

    def __init__(self, cssl_instance: 'CsslLang', code: str) -> None: ...
    def __call__(self, *args: Any) -> Any:
        """Execute the module code with the given arguments."""
        ...
    def __repr__(self) -> str: ...


class CSSLFunctionModule:
    """
    A CSSL module with accessible functions as methods.

    Created via CSSL.makemodule() - functions defined in the CSSL code
    become callable attributes on this module.
    """

    def __init__(self, cssl_instance: 'CsslLang', code: str) -> None: ...
    def __getattr__(self, name: str) -> Callable[..., Any]:
        """Get a function from the module."""
        ...
    def __dir__(self) -> List[str]:
        """List available functions."""
        ...
    def __repr__(self) -> str: ...


class CsslLang:
    """
    CSSL Language interface for Python.

    Usage:
        from includecpp import CSSL
        cssl = CSSL.CsslLang()
        result = cssl.exec("script.cssl", arg1, arg2)
        cssl.T_exec("async_script.cssl", arg1)  # Threaded
    """

    def __init__(self, output_callback: Optional[Callable[[str, str], None]] = ...) -> None:
        """
        Initialize CSSL runtime.

        Args:
            output_callback: Optional callback for output (text, level)
        """
        ...

    def exec(self, path_or_code: str, *args: Any) -> Any:
        """
        Execute CSSL code or file.

        Args:
            path_or_code: Path to .cssl file or CSSL code string
            *args: Arguments to pass to the script

        Returns:
            Execution result
        """
        ...

    def T_exec(
        self,
        path_or_code: str,
        *args: Any,
        callback: Optional[Callable[[Any], None]] = ...
    ) -> threading.Thread:
        """
        Execute CSSL code asynchronously in a thread.

        Args:
            path_or_code: Path to .cssl file or CSSL code string
            *args: Arguments to pass to the script
            callback: Optional callback when execution completes

        Returns:
            Thread object
        """
        ...

    def wait_all(self, timeout: Optional[float] = ...) -> None:
        """Wait for all async executions to complete."""
        ...

    def get_output(self) -> List[str]:
        """Get output buffer from last execution."""
        ...

    def clear_output(self) -> None:
        """Clear output buffer."""
        ...

    def set_global(self, name: str, value: Any) -> None:
        """Set a global variable in CSSL runtime."""
        ...

    def get_global(self, name: str) -> Any:
        """Get a global variable from CSSL runtime."""
        ...

    def module(self, code: str) -> CSSLModule:
        """
        Create a callable CSSL module from code.

        Usage:
            module = cssl.module('''
                printl(parameter.get(0));
            ''')
            module("Hello")  # Prints "Hello"

        Args:
            code: CSSL code string

        Returns:
            CSSLModule - a callable module
        """
        ...

    def makemodule(self, code: str) -> CSSLFunctionModule:
        """
        Create a CSSL module with accessible functions.

        Usage:
            module = cssl.makemodule('''
                string greet(string name) {
                    return "Hello, " + name + "!";
                }
            ''')
            module.greet("World")  # Returns "Hello, World!"

        Args:
            code: CSSL code string with function definitions

        Returns:
            CSSLFunctionModule - module with callable function attributes
        """
        ...


def get_cssl() -> CsslLang:
    """Get default CSSL instance."""
    ...


def exec(path_or_code: str, *args: Any) -> Any:
    """
    Execute CSSL code or file.

    Usage:
        from includecpp import CSSL
        CSSL.exec("script.cssl", arg1, arg2)
        CSSL.exec("printl('Hello World');")

    Args:
        path_or_code: Path to .cssl file or CSSL code string
        *args: Arguments to pass to the script

    Returns:
        Execution result
    """
    ...


def T_exec(
    path_or_code: str,
    *args: Any,
    callback: Optional[Callable[[Any], None]] = ...
) -> threading.Thread:
    """
    Execute CSSL code asynchronously in a thread.

    Usage:
        from includecpp import CSSL
        CSSL.T_exec("async_script.cssl", arg1, callback=on_done)

    Args:
        path_or_code: Path to .cssl file or CSSL code string
        *args: Arguments to pass to the script
        callback: Optional callback when execution completes

    Returns:
        Thread object
    """
    ...


def set_global(name: str, value: Any) -> None:
    """Set a global variable in CSSL runtime."""
    ...


def get_global(name: str) -> Any:
    """Get a global variable from CSSL runtime."""
    ...


def get_output() -> List[str]:
    """Get output buffer from last execution."""
    ...


def clear_output() -> None:
    """Clear output buffer."""
    ...


# Aliases to avoid conflict with Python builtin exec
def _exec(code: str, *args: Any) -> Any:
    """
    Execute CSSL code directly (alias for exec).

    Supports triple-quoted docstrings for inline CSSL code:

    Usage:
        from includecpp import CSSL
        CSSL._exec('''
            global base = include("abc.cssl-mod");

            void TestFunc() {
                printl("hey");
                @base.RechneWas(4, 5);
            }

            TestFunc();
        ''')

    Args:
        code: CSSL code string (supports triple-quoted docstrings)
        *args: Arguments to pass to the script

    Returns:
        Execution result
    """
    ...


def _T_exec(
    code: str,
    *args: Any,
    callback: Optional[Callable[[Any], None]] = ...
) -> threading.Thread:
    """
    Execute CSSL code asynchronously in a thread (alias for T_exec).

    Args:
        code: CSSL code string
        *args: Arguments to pass to the script
        callback: Optional callback when execution completes

    Returns:
        Thread object
    """
    ...


def module(code: str) -> CSSLModule:
    """
    Create a callable CSSL module from code.

    Usage:
        from includecpp import CSSL
        greet = CSSL.module('''
            printl("Hello, " + parameter.get(0) + "!");
        ''')
        greet("World")  # Prints "Hello, World!"

    Args:
        code: CSSL code string

    Returns:
        CSSLModule - a callable module
    """
    ...


def makemodule(code: str) -> CSSLFunctionModule:
    """
    Create a CSSL module with accessible functions.

    Usage:
        from includecpp import CSSL
        math_mod = CSSL.makemodule('''
            int add(int a, int b) {
                return a + b;
            }
        ''')
        math_mod.add(2, 3)  # Returns 5

    Args:
        code: CSSL code string with function definitions

    Returns:
        CSSLFunctionModule - module with callable function attributes
    """
    ...


__all__: List[str]
