import logging
import os

from configurator import Configurator
from helper import import_pydevd
from mbcontextmessagehandler import (
    MbContextMessageHandler,
    MbContextMessageBus,
)
from message_handler_builder import NReportsMessageHandlerBuilder
from msgbus import MBEasyContext

from poshelper import config_logger_parse_file_name
from realfilesystem import RealFileSystem


def main() -> None:

    loader_path = os.environ["LOADERCFG"]

    config_logger_parse_file_name(loader_path=os.environ["LOG_PATH"])
    logger = logging.getLogger(name=__name__)
    logger.info("Starting component...")

    import_pydevd(
        loader_path=loader_path,
        port=9245,
    )

    configurator = Configurator(
        loader_path=loader_path,
        file_system=RealFileSystem(),
    )
    configs = configurator.get_configs()

    mb_context = MBEasyContext(name=configs.component_name)
    message_bus = MbContextMessageBus(mbcontext=mb_context)

    message_handler_builder = NReportsMessageHandlerBuilder(
        configs=configs,
        message_bus=message_bus,
    )
    message_handler = MbContextMessageHandler(
        message_bus=message_bus,
        service_name=configs.component_name,
        service_type=configs.component_name,
        required_services=configurator.get_required_services(),
        event_handler_builder=message_handler_builder,
    )

    return message_handler.handle_events()
