"""ASA Engine Loader — checks engine availability, guides installation.

The ASA CLI wrapper (distributed via PyPI) does not include the proprietary
engine. The engine is distributed separately via GitHub Releases as a
bytecode wheel.

This module is called before any engine import to provide a clear error
message if the engine is not installed.
"""

import sys

_engine_available: bool | None = None

# Updated per release — points to the matching engine wheel on GitHub Releases
# Engine wheel naming: asa-engine-X.Y.Z (prefix "asa-engine-" to prevent confusion with PyPI wrapper)
ENGINE_RELEASE_URL_TEMPLATE = (
    "https://github.com/janvoldan/asa-cli/releases/download/"
    "v{version}/asa-engine-{version}-py3-none-any.whl"
)


def is_engine_installed() -> bool:
    """Check whether the ASA engine package is available."""
    global _engine_available
    if _engine_available is None:
        try:
            from asa.engine import scanner  # noqa: F401

            _engine_available = True
        except ImportError:
            _engine_available = False
    return _engine_available


def require_engine() -> None:
    """Ensure engine is installed. Exit with helpful message if not."""
    if is_engine_installed():
        return

    from asa import __version__

    url = ENGINE_RELEASE_URL_TEMPLATE.format(version=__version__)
    print()
    print("❌ ASA Engine not installed.")
    print()
    print("   The CLI wrapper is installed, but the engine is distributed separately.")
    print()
    print("   Install the engine:")
    print(f"   pip install {url}")
    print()
    print("   After installation, re-run your command.")
    print()
    sys.exit(1)
