import os

from config import Configurator
from edplogutil import wrap_types
from helper import (
    import_pydevd,
)
from mbcontextmessagehandler import (
    MbContextMessageHandler,
    MbContextMessageBus,
)
from message_handler_builder import (
    EventManagerMessageHandlerBuilder,
)
from msgbus import MBEasyContext

# from opentelemetry_edphelper import configure_open_telemetry

from poshelper import config_logger_parse_file_name
from sqlite_repository import SqliteEventRepository
from timeutil import (
    RealClock,
    ConditionSleeper,
)


def main():
    loader_path = os.environ["LOADERCFG"]

    import_pydevd(loader_path, 9145)

    # configure_open_telemetry(
    #     service_name="EventManager",
    # )

    wrap_types(
        clss=[
            RealClock,
            ConditionSleeper,
            MbContextMessageBus,
            SqliteEventRepository,
        ]
    )

    message_bus = MbContextMessageBus(mbcontext=MBEasyContext("CustomEventManager"))

    config_logger_parse_file_name(os.environ["LOG_PATH"])
    config_info = Configurator(loader_path).read_loader_configs()

    message_handler_builder = EventManagerMessageHandlerBuilder(
        config_info=config_info,
        message_bus=message_bus,
        clock=RealClock(),
    )
    message_handler = MbContextMessageHandler(
        message_bus=message_bus,
        service_name=config_info.component_name,
        service_type=config_info.component_name,
        required_services="",
        event_handler_builder=message_handler_builder
    )

    return message_handler.handle_events()
