import logging
import os

from msgbus import MBEasyContext

from configurator import Configurator
from helper import import_pydevd
from mbcontextmessagehandler import (
    MbContextMessageBus,
    MbContextMessageHandler,
)
from message_handler_builder import BossMessageHandlerBuilder
from poshelper import config_logger_parse_file_name
from repositoryimpl.service import AuthenticateServiceImpl


def main() -> None:

    loader_path = os.environ["LOADERCFG"]

    import_pydevd(
        loader_path=loader_path,
        port=9345,
    )

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

    configurator = Configurator(loader_path=loader_path)
    configurator.load_configs()
    configs = configurator.configs

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

    message_handler_builder = BossMessageHandlerBuilder(
        configs=configs,
        authenticate_service=AuthenticateServiceImpl(configs=configs),
    )
    message_handler = MbContextMessageHandler(
        message_bus=message_bus,
        service_name=configs.service_name,
        service_type=configs.service_name,
        required_services=configs.required_services,
        event_handler_builder=message_handler_builder,
    )

    configurator.load_store_wide_configs(message_bus=message_bus)

    return message_handler.handle_events()
