"""
ArhiPlexPy - Python interface for Arhiplex LP/MIP Solver with CP-SAT support

This module provides Python bindings for:
- Arhiplex LP/MIP solver (native extension `arhiplexpy.arhiplexpy`)
- CP-SAT solver from cp_solver / arhitools (`arhiplexpy.arhitools`)

Usage:
    # LP/MIP
    import arhiplexpy
    h = arhiplexpy.Arhiplex()

    # CP-SAT (when built with cp_solver)
    from arhiplexpy.arhitools.sat.python import cp_model
"""

from . import arhiplexpy as _arhiplex_core

_export_names = list(
    getattr(
        _arhiplex_core,
        "__all__",
        [n for n in dir(_arhiplex_core) if not n.startswith("_")],
    )
)
for _name in _export_names:
    globals()[_name] = getattr(_arhiplex_core, _name)
del _name

import importlib
import importlib.util
import sys

# Native pybind submodules (only if built with BUILD_CP_SOLVER / ARHIPLEX_WITH_CP_SOLVER).
_NATIVE_CP_SOLVER = hasattr(_arhiplex_core, "cp_model_helper")

if _NATIVE_CP_SOLVER:
    sys.modules["arhitools.sat.python.cp_model_helper"] = _arhiplex_core.cp_model_helper
    sys.modules["arhitools.util.python.sorted_interval_list"] = _arhiplex_core.sorted_interval_list
    sys.modules["arhitools.util.python.pybind_solve_interrupter"] = (
        _arhiplex_core.pybind_solve_interrupter
    )

# Python package arhiplexpy.arhitools (installed by CMake when BUILD_CP_SOLVER). Avoid
# unconditional `from . import arhitools`: if the subpackage is missing, that can raise a
# confusing "circular import" on some Python versions; use find_spec first.
_ARHITOOLS_PY = False
_arhitools_spec = importlib.util.find_spec(".arhitools", __name__)

if _arhitools_spec is not None:
    try:
        arhitools = importlib.import_module(".arhitools", __name__)
        sys.modules["arhitools"] = arhitools
        globals()["arhitools"] = arhitools
        _ARHITOOLS_PY = True
    except ImportError:
        pass
    except Exception:
        raise

_CP_SOLVER_AVAILABLE = bool(_NATIVE_CP_SOLVER and _ARHITOOLS_PY)

__version__ = "2.0.0"


def has_cp_solver():
    """True if the extension exposes CP-SAT pybind submodules and the arhitools Python tree is installed."""
    return _CP_SOLVER_AVAILABLE


__all__ = _export_names + ["__version__", "has_cp_solver"]
if _CP_SOLVER_AVAILABLE:
    __all__.append("arhitools")

del _arhiplex_core
del _export_names
