import asyncio
import tomllib
from importlib.metadata import PackageNotFoundError
from importlib.metadata import version as pkg_version
from pathlib import Path

from ldnode.calculator.service import CalculatorService
from ldnode.connector.service import ConnectorService
from ldnode.loader.service import LoaderService
from ldnode.monitor.service import MonitorService
from ldnode.runner import Runner
from ldnode.runner.config import get_settings


def _resolve_version() -> str:
    try:
        return pkg_version("ldnode")
    except PackageNotFoundError:
        pyproject = Path(__file__).resolve().parents[2] / "pyproject.toml"
        with pyproject.open("rb") as f:
            return tomllib.load(f)["project"]["version"]


def main() -> None:
    asyncio.run(
        Runner.build(
            _resolve_version(),
            get_settings(),
            CalculatorService,
            ConnectorService,
            MonitorService,
            LoaderService,
        )
    )
