from typing import Dict, Optional

from dihttpmessageprocessor import AutoMappedDiRouterProcessorBuilder, RouteDefinition
from messageprocessor import MessageProcessor
from simplehttprouter import Route

from ..utils import get_definition_params
from ...interactor import AutoOpenApiYamlBuilderInteractor
from ...interactor.dto import AutoOpenApiYamlBuilderRequest


class DependencyInjectionApiYamlBuilderInteractor(AutoOpenApiYamlBuilderInteractor):
    def __init__(
        self,
        router_processor_builder: Optional[AutoMappedDiRouterProcessorBuilder] = None,
        routes: Optional[Dict[Route, MessageProcessor]] = None,
        config=None,
    ):
        super().__init__(routes=routes, config=config)
        self.router_processor_builder = router_processor_builder

    def execute(self, request: AutoOpenApiYamlBuilderRequest) -> str:
        builder = self.get_builder()
        if self.router_processor_builder is not None:
            for route, definition in self.router_processor_builder.auto_mapped_definition.items():
                builder.add_interactor_path(route_params=get_definition_params(route=route, definition=definition))

            for route, processor_type in self.router_processor_builder.route_definition.items():
                builder.add_processor_path(route=route, processor=processor_type)

        return super().execute(request=request)
