from importlib.metadata import version as pkg_version

from packaging.version import Version

IMPORT_NAME = "dlthub"
PKG_NAME = "dlthub"
__version__ = pkg_version(PKG_NAME)
PKG_REQUIREMENT = f"{PKG_NAME}=={__version__}"


def ensure_dlt_version() -> None:
    try:
        import dlt
    except ModuleNotFoundError as mod_ex:
        raise ImportError(
            f"{str(mod_ex)}\n`{PKG_NAME}` is a `dlt` plugin and must be installed together with "
            "`dlt`. We recommend to use `hub` extra to install right versions combination:\n\n"
            'pip install "dlt[hub]"'
        )
    dlt_version = dlt.__version__
    installed = Version(__version__)
    dlt_installed = Version(dlt_version)

    if installed.minor != dlt_installed.minor:
        raise ImportError(
            f"`{PKG_NAME}` is a `dlt` plugin and must be installed together with `dlt` with a "
            f"matching version. `dlt` {dlt_installed.major}.{dlt_installed.minor}.x requires "
            f"`{PKG_NAME}` 0.{dlt_installed.minor}.x but you have "
            f"{__version__}. Please install the right version of {PKG_NAME} with:\n\n"
            f'pip install "dlt[hub]=={dlt_version}"\n\n'
            "or if you are upgrading:\n\n"
            f'pip install "dlt[hub]=={dlt_version}" -U {PKG_NAME}'
        )
